Skip to content

Commit 4a55e89

Browse files
author
dlsmith
committed
DynamicJava: Can handle "native" and "@interface" without choking; uses Class.isAnonymousClass (available in Java 5) when possible rather than testing for a certain class name format; added StandardTypeSystem option to (incorrectly, but matching javac) use boxing when checking for ambiguous method invocations.
git-svn-id: file:///tmp/test-svn/trunk@5171 fe72c1cf-3628-48e9-8b72-1c46755d3cff
1 parent 6ad9127 commit 4a55e89

File tree

9 files changed

+70
-26
lines changed

9 files changed

+70
-26
lines changed

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/ClassChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ private class ClassMemberSignatureVisitor extends MemberSignatureVisitor {
251251
setErrorStrings(node, node.getName());
252252
throw new ExecutionError("abstract.method.body", node);
253253
}
254-
else if (!mods.isAbstract() && node.getBody() == null) {
254+
else if (!mods.isAbstract() && !mods.isNative() && node.getBody() == null) {
255255
setErrorStrings(node, node.getName());
256256
throw new ExecutionError("missing.method.body", node);
257257
}

dynamicjava/src/edu/rice/cs/dynamicjava/interpreter/TreeCompiler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ private void compileMethod(MethodDeclaration ast, boolean isInterface) {
626626
final MethodVisitor mv = _classWriter.visitMethod(access, ast.getName(), methodDescriptor,
627627
methodSig, extractClassNames(exceptions));
628628

629-
if (!Modifier.isAbstract(access)) {
629+
if (!Modifier.isAbstract(access) && !Modifier.isNative(access)) {
630630
String key = ast.getName() + methodDescriptor;
631631
_methods.put(key, ast);
632632

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/ExtendedTypeSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ public class ExtendedTypeSystem extends StandardTypeSystem {
3030
/** Whether the inference algorithm should attempt to pack capture variables that appear as inference results. */
3131
private final boolean _packCaptureVars;
3232

33-
public ExtendedTypeSystem(Options opt) { this(opt, true); }
33+
public ExtendedTypeSystem(Options opt) { this(opt, true, true); }
3434

35-
public ExtendedTypeSystem(Options opt, boolean packCaptureVars) {
36-
super(opt);
35+
public ExtendedTypeSystem(Options opt, boolean packCaptureVars, boolean boxingInMostSpecific) {
36+
super(opt, boxingInMostSpecific);
3737
_packCaptureVars = packCaptureVars;
3838
}
3939

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/JLSTypeSystem.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public class JLSTypeSystem extends StandardTypeSystem {
5050
*/
5151
private final boolean _waitToUseDeclaredBounds;
5252

53-
public JLSTypeSystem(Options opt) { this(opt, true, true, true); }
53+
public JLSTypeSystem(Options opt) { this(opt, true, true, true, true); }
5454

5555
public JLSTypeSystem(Options opt, boolean packCaptureVars, boolean alwaysUseArgumentConstraints,
56-
boolean waitToUseDeclaredBounds) {
57-
super(opt);
56+
boolean waitToUseDeclaredBounds, boolean boxingInMostSpecific) {
57+
super(opt, boxingInMostSpecific);
5858
_packCaptureVars = packCaptureVars;
5959
_alwaysUseArgumentConstraints = alwaysUseArgumentConstraints;
6060
_waitToUseDeclaredBounds = waitToUseDeclaredBounds;

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/Java5Class.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import edu.rice.cs.plt.lambda.Predicate;
1010
import edu.rice.cs.plt.iter.IterUtil;
1111
import edu.rice.cs.plt.recur.PrecomputedRecursionStack;
12+
import edu.rice.cs.plt.reflect.ReflectUtil;
1213
import edu.rice.cs.plt.tuple.Wrapper;
1314

1415
import edu.rice.cs.dynamicjava.symbol.type.*;
@@ -24,6 +25,13 @@ public class Java5Class extends JavaClass {
2425

2526
public Java5Class(Class<?> c) { super(c); }
2627

28+
@Override public String declaredName() {
29+
if (_c.isAnonymousClass()) {
30+
throw new IllegalArgumentException("Anonymous class has no declared name");
31+
}
32+
else { return _c.getSimpleName(); }
33+
}
34+
2735
@Override public Access.Module accessModule() {
2836
Class<?> result = _c;
2937
Class<?> outer = result.getEnclosingClass();

dynamicjava/src/edu/rice/cs/dynamicjava/symbol/StandardTypeSystem.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ public abstract class StandardTypeSystem extends TypeSystem {
3232

3333
private final Options _opt;
3434

35-
protected StandardTypeSystem(Options opt) { _opt = opt; }
35+
/**
36+
* Whether the most specific method test can use boxing to match parameters' types. This contradicts
37+
* the JLS, but matches javac.
38+
*/
39+
private final boolean _boxingInMostSpecific;
40+
41+
protected StandardTypeSystem(Options opt, boolean boxingInMostSpecific) {
42+
_opt = opt;
43+
_boxingInMostSpecific = boxingInMostSpecific;
44+
}
3645

3746
/** Determine if the type is well-formed. */
3847
public abstract boolean isWellFormed(Type t);
@@ -2127,7 +2136,7 @@ else if (subArity > supArity) {
21272136
SignatureMatcher m = makeMatcher(c._f.typeParameters(), EMPTY_TYPE_ITERABLE, supParams,
21282137
IterUtil.mapSnapshot(subParams, EMPTY_EXPRESSION_FOR_TYPE),
21292138
BOTTOM, NONE_TYPE_OPTION);
2130-
return m.matches();
2139+
return m.matches() || _boxingInMostSpecific && m.matchesWithBoxing();
21312140
}
21322141

21332142
}

dynamicjava/src/koala/dynamicjava/parser/grammar.jj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ PARSER_END(Parser)
10821082
try {
10831083
(
10841084
LOOKAHEAD(2) node = keywordStatement(false) // lookahead distinguishes synchronized modifier from statement
1085-
| mods = modifiers()
1085+
| LOOKAHEAD(2) mods = modifiers()
10861086
(
10871087
node = unmodifiedPackageDeclaration(mods, DeclType.REPL)
10881088
| node = unmodifiedTypeDeclaration(mods, DeclType.REPL)
@@ -1174,7 +1174,7 @@ PARSER_END(Parser)
11741174
Token first = getToken(1);
11751175
}
11761176
{
1177-
(
1177+
( LOOKAHEAD(2) (
11781178
"abstract"
11791179
{
11801180
if (flags.contains(Modifier.ABSTRACT)) {
@@ -1295,7 +1295,7 @@ PARSER_END(Parser)
12951295
|
12961296
ann = annotation()
12971297
{ annots.add(ann); }
1298-
)+
1298+
))+
12991299
{ return new ModifierSet(flags, annots, _range(first, token)); }
13001300
}
13011301

@@ -1894,14 +1894,15 @@ PARSER_END(Parser)
18941894
InterfaceDeclaration unmodifiedInterfaceDeclaration(ModifierSet mods, DeclType level) :
18951895
{
18961896
Token id;
1897+
Token isAnnotation = null;
18971898
List<TypeParameter> typeParameters = null;
18981899
List<? extends ReferenceTypeName> impl = null;
18991900
List<Node> list = new LinkedList<Node>();
19001901
List<Node> decl;
19011902
}
19021903
{
19031904
try {
1904-
"interface" id=<IDENTIFIER>
1905+
[ isAnnotation="@"] "interface" id=<IDENTIFIER>
19051906
[ typeParameters = typeParameters() ]
19061907
[ "extends" impl=ReferenceTypeNameList() ]
19071908
"{"
@@ -1918,7 +1919,8 @@ PARSER_END(Parser)
19181919
case REPL: checkModifiers(mods, Modifier.PUBLIC, Modifier.ABSTRACT, Modifier.STRICT); break;
19191920
}
19201921
1921-
return new InterfaceDeclaration(mods, id.image, Option.wrap(typeParameters), impl, list, _range(mods, token));
1922+
return new InterfaceDeclaration(mods, isAnnotation != null, id.image, Option.wrap(typeParameters), impl, list,
1923+
_range(mods, token));
19221924
}
19231925
}
19241926
catch(ParseException pe) {
@@ -2135,7 +2137,7 @@ PARSER_END(Parser)
21352137
(
21362138
LOOKAHEAD(2) node = keywordStatement(true) // lookahead distinguishes synchronized modifier from statement
21372139
|
2138-
mods = modifiers()
2140+
LOOKAHEAD(2) mods = modifiers()
21392141
(
21402142
node = unmodifiedTypeDeclaration(mods, DeclType.LOCAL)
21412143
| LOOKAHEAD(variableLookahead()) nodes = unmodifiedVariableDeclaration(mods, DeclType.LOCAL)

dynamicjava/src/koala/dynamicjava/parser/wrapper/ParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void testClassDeclaration() throws ParseException {
134134

135135
public void testInterfaceDeclaration() throws ParseException {
136136
List<Node> body = new LinkedList<Node>();
137-
expectedAST.add(new InterfaceDeclaration(ModifierSet.make(), "i", null, body, SourceInfo.NONE));
137+
expectedAST.add(new InterfaceDeclaration(ModifierSet.make(), false, "i", null, body, SourceInfo.NONE));
138138
verifyOutput("interface i{}", expectedAST);
139139
}
140140

dynamicjava/src/koala/dynamicjava/tree/InterfaceDeclaration.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,80 @@
4343
*/
4444

4545
public class InterfaceDeclaration extends TypeDeclaration {
46+
47+
private boolean annotation;
48+
4649
/**
4750
* Creates a new interface declaration
4851
* @param mods the modifiers
52+
* @param ann whether this is an annotation
4953
* @param name the name of the interface to declare
5054
* @param tparams type parameters
5155
* @param impl the list of implemented interfaces. Can be null.
5256
* @param body the list of fields declarations
5357
*/
54-
public InterfaceDeclaration(ModifierSet mods, String name, Option<List<TypeParameter>> tparams,
58+
public InterfaceDeclaration(ModifierSet mods, boolean ann, String name, Option<List<TypeParameter>> tparams,
5559
List<? extends ReferenceTypeName> impl, List<Node> body) {
56-
this(mods, name, tparams, impl, body, SourceInfo.NONE);
60+
this(mods, ann, name, tparams, impl, body, SourceInfo.NONE);
5761
}
5862

5963
/**
6064
* Creates a new interface declaration
6165
* @param mods the modifiers
66+
* @param ann whether this is an annotation
6267
* @param name the name of the interface to declare
6368
* @param impl the list of implemented interfaces. Can be null.
6469
* @param body the list of fields declarations
6570
*/
66-
public InterfaceDeclaration(ModifierSet mods, String name, List<? extends ReferenceTypeName> impl, List<Node> body) {
67-
this(mods, name, Option.<List<TypeParameter>>none(), impl, body, SourceInfo.NONE);
71+
public InterfaceDeclaration(ModifierSet mods, boolean ann, String name,
72+
List<? extends ReferenceTypeName> impl, List<Node> body) {
73+
this(mods, ann, name, Option.<List<TypeParameter>>none(), impl, body, SourceInfo.NONE);
6874
}
6975

7076
/**
7177
* Creates a new interface declaration
7278
* @param mods the modifiers
79+
* @param ann whether this is an annotation
7380
* @param name the name of the interface to declare
7481
* @param tparams type parameters
7582
* @param impl the list of implemented interfaces. Can be null.
7683
* @param body the list of fields declarations
7784
*/
78-
public InterfaceDeclaration(ModifierSet mods, String name, Option<List<TypeParameter>> tparams,
85+
public InterfaceDeclaration(ModifierSet mods, boolean ann, String name, Option<List<TypeParameter>> tparams,
7986
List<? extends ReferenceTypeName> impl, List<Node> body, SourceInfo si) {
8087
super(mods, name, tparams, impl, body, si);
88+
annotation = ann;
8189
}
8290

8391
/**
8492
* Creates a new interface declaration
8593
* @param mods the modifiers
94+
* @param ann whether this is an annotation
8695
* @param name the name of the interface to declare
8796
* @param impl the list of implemented interfaces. Can be null.
8897
* @param body the list of fields declarations
8998
*/
90-
public InterfaceDeclaration(ModifierSet mods, String name, List<? extends ReferenceTypeName> impl, List<Node> body,
91-
SourceInfo si) {
92-
this(mods, name, Option.<List<TypeParameter>>none(), impl, body, si);
99+
public InterfaceDeclaration(ModifierSet mods, boolean ann, String name,
100+
List<? extends ReferenceTypeName> impl, List<Node> body, SourceInfo si) {
101+
this(mods, ann, name, Option.<List<TypeParameter>>none(), impl, body, si);
102+
}
103+
104+
/**
105+
* Whether this is an annotation declaration.
106+
*/
107+
public boolean isAnnotation() {
108+
return annotation;
93109
}
94110

111+
/**
112+
* Sets whether this is an annotation declaration.
113+
* @exception IllegalArgumentException if s is null
114+
*/
115+
public void setIsAnnotation(boolean ann) {
116+
annotation = ann;
117+
}
118+
119+
95120
/**
96121
* Allows a visitor to traverse the tree
97122
* @param visitor the visitor to accept
@@ -107,6 +132,6 @@ public String toString() {
107132
}
108133

109134
protected String toStringHelper() {
110-
return getModifiers()+" "+getName()+" "+getTypeParams()+" "+getInterfaces()+" "+getMembers();
135+
return getModifiers()+" "+isAnnotation()+" "+getName()+" "+getTypeParams()+" "+getInterfaces()+" "+getMembers();
111136
}
112137
}

0 commit comments

Comments
 (0)