|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.javalanglevels; |
|
38 |
| |
|
39 |
| import edu.rice.cs.javalanglevels.tree.*; |
|
40 |
| import edu.rice.cs.javalanglevels.parser.*; |
|
41 |
| import edu.rice.cs.plt.reflect.JavaVersion; |
|
42 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
43 |
| import java.util.*; |
|
44 |
| import java.io.*; |
|
45 |
| |
|
46 |
| import junit.framework.TestCase; |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class InterfaceBodyIntermediateVisitor extends IntermediateVisitor { |
|
53 |
| |
|
54 |
| |
|
55 |
| private SymbolData _enclosing; |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
22
| public InterfaceBodyIntermediateVisitor(SymbolData sd,
|
|
67 |
| File file, |
|
68 |
| String packageName, |
|
69 |
| LinkedList<String> importedFiles, |
|
70 |
| LinkedList<String> importedPackages, |
|
71 |
| HashSet<String> classesInThisFile, |
|
72 |
| Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>> continuations, |
|
73 |
| LinkedList<Command> fixUps) { |
|
74 |
22
| super(file, packageName, sd.getName(), importedFiles, importedPackages, classesInThisFile, continuations, fixUps);
|
|
75 |
22
| _enclosing = sd;
|
|
76 |
| } |
|
77 |
| |
|
78 |
| |
|
79 |
0
| public Void forStatementDoFirst(Statement that) {
|
|
80 |
0
| _addError("Statements cannot appear outside of method bodies", that);
|
|
81 |
0
| return null;
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
1
| public Void forConcreteMethodDefDoFirst(ConcreteMethodDef that) {
|
|
86 |
1
| _addError("You cannot have concrete methods definitions in interfaces", that);
|
|
87 |
1
| return null;
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
1
| public Void forInstanceInitializerDoFirst(InstanceInitializer that) {
|
|
92 |
1
| _addError("This open brace must mark the beginning of an interface body", that);
|
|
93 |
1
| return null;
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
1
| public Void forVariableDeclarationDoFirst(VariableDeclaration that) {
|
|
98 |
1
| _addError("You cannot have fields in interfaces at the Intermediate level", that);
|
|
99 |
1
| return null;
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
2
| public Void forSuperReferenceDoFirst(SuperReference that) {
|
|
104 |
2
| _addAndIgnoreError("The field 'super' does not exist in interfaces. Only classes have a 'super' field", that);
|
|
105 |
2
| return null;
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
2
| public Void forThisReferenceDoFirst(ThisReference that) {
|
|
110 |
2
| _addAndIgnoreError("The field 'this' does not exist in interfaces. Only classes have a 'this' field.", that);
|
|
111 |
2
| return null;
|
|
112 |
| } |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
21
| public Void forAbstractMethodDef(AbstractMethodDef that) {
|
|
119 |
21
| forAbstractMethodDefDoFirst(that);
|
|
120 |
0
| if (_checkError()) return null;
|
|
121 |
| |
|
122 |
21
| MethodData md = createMethodData(that, _enclosing);
|
|
123 |
| |
|
124 |
| |
|
125 |
21
| if (md.hasModifier("private")) {
|
|
126 |
1
| _addAndIgnoreError("Interface methods cannot be private. They must be public.", that.getMav());
|
|
127 |
| } |
|
128 |
21
| if (md.hasModifier("protected")) {
|
|
129 |
1
| _addAndIgnoreError("Interface methods cannot be protected. They must be public.", that.getMav());
|
|
130 |
| } |
|
131 |
| |
|
132 |
| |
|
133 |
21
| md.addModifier("public");
|
|
134 |
21
| md.addModifier("abstract");
|
|
135 |
21
| String className = getUnqualifiedClassName(_enclosing.getName());
|
|
136 |
21
| if (className.equals(md.getName())) {
|
|
137 |
1
| _addAndIgnoreError("Only constructors can have the same name as the class they appear in, " +
|
|
138 |
| "and constructors cannot appear in interfaces.", that); |
|
139 |
| } |
|
140 |
20
| else _enclosing.addMethod(md);
|
|
141 |
21
| return null;
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
1
| public Void forConstructorDefDoFirst(ConstructorDef that) {
|
|
146 |
1
| _addAndIgnoreError("Constructor definitions cannot appear in interfaces", that);
|
|
147 |
1
| return null;
|
|
148 |
| } |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
0
| protected VariableData[] _variableDeclaration2VariableData(VariableDeclaration vd, Data enclosingData) {
|
|
156 |
0
| VariableData[] vds = super._variableDeclaration2VariableData(vd, enclosingData);
|
|
157 |
0
| for (int i = 0; i < vds.length; i++) {
|
|
158 |
0
| vds[i].setFinalAndStatic();
|
|
159 |
| } |
|
160 |
0
| return vds;
|
|
161 |
| } |
|
162 |
| |
|
163 |
| |
|
164 |
0
| public Void forComplexAnonymousClassInstantiation(ComplexAnonymousClassInstantiation that) {
|
|
165 |
0
| complexAnonymousClassInstantiationHelper(that, _enclosing);
|
|
166 |
0
| return null;
|
|
167 |
| } |
|
168 |
| |
|
169 |
| |
|
170 |
0
| public Void forSimpleAnonymousClassInstantiation(SimpleAnonymousClassInstantiation that) {
|
|
171 |
0
| simpleAnonymousClassInstantiationHelper(that, _enclosing);
|
|
172 |
0
| return null;
|
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
| public static class InterfaceBodyIntermediateVisitorTest extends TestCase { |
|
177 |
| |
|
178 |
| private InterfaceBodyIntermediateVisitor _ibiv; |
|
179 |
| |
|
180 |
| private SymbolData _sd1; |
|
181 |
| private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"}); |
|
182 |
| private ModifiersAndVisibility _protectedMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"}); |
|
183 |
| private ModifiersAndVisibility _privateMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"private"}); |
|
184 |
| private ModifiersAndVisibility _packageMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]); |
|
185 |
| private ModifiersAndVisibility _abstractMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"abstract"}); |
|
186 |
| private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"}); |
|
187 |
| |
|
188 |
| |
|
189 |
0
| public InterfaceBodyIntermediateVisitorTest() {
|
|
190 |
0
| this("");
|
|
191 |
| } |
|
192 |
10
| public InterfaceBodyIntermediateVisitorTest(String name) {
|
|
193 |
10
| super(name);
|
|
194 |
| } |
|
195 |
| |
|
196 |
10
| public void setUp() {
|
|
197 |
10
| _sd1 = new SymbolData("i.like.monkey");
|
|
198 |
10
| _sd1.setIsContinuation(false);
|
|
199 |
10
| _sd1.setInterface(false);
|
|
200 |
10
| _sd1.setPackage("");
|
|
201 |
10
| _sd1.setTypeParameters(new TypeParameter[0]);
|
|
202 |
10
| _sd1.setInterfaces(new ArrayList<SymbolData>());
|
|
203 |
| |
|
204 |
10
| errors = new LinkedList<Pair<String, JExpressionIF>>();
|
|
205 |
10
| LanguageLevelConverter.symbolTable.clear();
|
|
206 |
10
| LanguageLevelConverter._newSDs.clear();
|
|
207 |
10
| LanguageLevelConverter.OPT = new Options(JavaVersion.JAVA_5, IterUtil.make(new File("lib/buildlib/junit.jar")));
|
|
208 |
10
| visitedFiles = new LinkedList<Pair<LanguageLevelVisitor, edu.rice.cs.javalanglevels.tree.SourceFile>>();
|
|
209 |
| |
|
210 |
10
| _ibiv =
|
|
211 |
| new InterfaceBodyIntermediateVisitor(_sd1, |
|
212 |
| new File(""), |
|
213 |
| "", |
|
214 |
| new LinkedList<String>(), |
|
215 |
| new LinkedList<String>(), |
|
216 |
| new HashSet<String>(), |
|
217 |
| new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>(), |
|
218 |
| new LinkedList<Command>()); |
|
219 |
10
| _ibiv._classesInThisFile = new HashSet<String>();
|
|
220 |
10
| _ibiv.continuations = new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>();
|
|
221 |
10
| _ibiv._importedPackages.addFirst("java.lang");
|
|
222 |
10
| _ibiv._enclosingClassName = "i.like.monkey";
|
|
223 |
10
| _ibiv.symbolTable.put("i.like.monkey", _sd1);
|
|
224 |
10
| _errorAdded = false;
|
|
225 |
| } |
|
226 |
| |
|
227 |
1
| public void testForConcreteMethodDefDoFirst() {
|
|
228 |
| |
|
229 |
1
| ConcreteMethodDef cmd = new ConcreteMethodDef(SourceInfo.NONE,
|
|
230 |
| _publicMav, |
|
231 |
| new TypeParameter[0], |
|
232 |
| new PrimitiveType(SourceInfo.NONE, "int"), |
|
233 |
| new Word(SourceInfo.NONE, "methodName"), |
|
234 |
| new FormalParameter[0], |
|
235 |
| new ReferenceType[0], |
|
236 |
| new BracedBody(SourceInfo.NONE, new BodyItemI[0])); |
|
237 |
1
| cmd.visit(_ibiv);
|
|
238 |
1
| assertEquals("There should not be 1 error", 1, errors.size());
|
|
239 |
1
| assertEquals("The error message should be correct", "You cannot have concrete methods definitions in interfaces", errors.getLast().getFirst());
|
|
240 |
| |
|
241 |
| } |
|
242 |
| |
|
243 |
1
| public void testForAbstractMethodDefDoFirst() {
|
|
244 |
| |
|
245 |
1
| _ibiv._enclosing.setMav(_abstractMav);
|
|
246 |
1
| AbstractMethodDef amd2 = new AbstractMethodDef(SourceInfo.NONE,
|
|
247 |
| _abstractMav, |
|
248 |
| new TypeParameter[0], |
|
249 |
| new PrimitiveType(SourceInfo.NONE, "double"), |
|
250 |
| new Word(SourceInfo.NONE, "methodName"), |
|
251 |
| new FormalParameter[0], |
|
252 |
| new ReferenceType[0]); |
|
253 |
1
| amd2.visit(_ibiv);
|
|
254 |
1
| assertEquals("There should be no errors", 0, errors.size());
|
|
255 |
1
| assertTrue("The method def should be public", _ibiv._enclosing.getMethods().get(0).hasModifier("public"));
|
|
256 |
| |
|
257 |
| } |
|
258 |
| |
|
259 |
1
| public void testForInstanceInitializerDoFirst() {
|
|
260 |
1
| InstanceInitializer ii = new InstanceInitializer(SourceInfo.NONE,
|
|
261 |
| new Block(SourceInfo.NONE, |
|
262 |
| new BracedBody(SourceInfo.NONE, new BodyItemI[0]))); |
|
263 |
1
| ii.visit(_ibiv);
|
|
264 |
1
| assertEquals("There should be one error.", 1, errors.size());
|
|
265 |
1
| assertEquals("The error message should be correct.", "This open brace must mark the beginning of an interface body", errors.get(0).getFirst());
|
|
266 |
| } |
|
267 |
| |
|
268 |
1
| public void testForSimpleThisReferenceDoFirst() {
|
|
269 |
1
| SimpleThisReference tl = new SimpleThisReference(SourceInfo.NONE);
|
|
270 |
1
| tl.visit(_ibiv);
|
|
271 |
1
| assertEquals("There should be one error", 1, errors.size());
|
|
272 |
1
| assertEquals("The error message should be correct", "The field 'this' does not exist in interfaces. Only classes have a 'this' field.", errors.get(0).getFirst());
|
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
1
| public void testForComplexThisReferenceDoFirst() {
|
|
277 |
1
| ComplexThisReference tl = new ComplexThisReference(SourceInfo.NONE, new NullLiteral(SourceInfo.NONE));
|
|
278 |
1
| tl.visit(_ibiv);
|
|
279 |
1
| assertEquals("There should be one error", 1, errors.size());
|
|
280 |
1
| assertEquals("The error message should be correct", "The field 'this' does not exist in interfaces. Only classes have a 'this' field.", errors.get(0).getFirst());
|
|
281 |
| |
|
282 |
| } |
|
283 |
| |
|
284 |
1
| public void testForSimpleSuperReferenceDoFirst() {
|
|
285 |
1
| SimpleSuperReference sr = new SimpleSuperReference(SourceInfo.NONE);
|
|
286 |
1
| sr.visit(_ibiv);
|
|
287 |
1
| assertEquals("There should be one error", 1, errors.size());
|
|
288 |
1
| assertEquals("The error message should be correct", "The field 'super' does not exist in interfaces. Only classes have a 'super' field", errors.get(0).getFirst());
|
|
289 |
| } |
|
290 |
| |
|
291 |
1
| public void testForComplexSuperReferenceDoFirst() {
|
|
292 |
1
| ComplexSuperReference cr = new ComplexSuperReference(SourceInfo.NONE, new NullLiteral(SourceInfo.NONE));
|
|
293 |
1
| cr.visit(_ibiv);
|
|
294 |
1
| assertEquals("There should be one error", 1, errors.size());
|
|
295 |
1
| assertEquals("The error message should be correct", "The field 'super' does not exist in interfaces. Only classes have a 'super' field", errors.get(0).getFirst());
|
|
296 |
| } |
|
297 |
| |
|
298 |
| |
|
299 |
1
| public void testForVariableDeclarationDoFirst() {
|
|
300 |
| |
|
301 |
1
| VariableDeclaration vdecl = new VariableDeclaration(SourceInfo.NONE,
|
|
302 |
| _packageMav, |
|
303 |
| new VariableDeclarator[] { |
|
304 |
| new UninitializedVariableDeclarator(SourceInfo.NONE, |
|
305 |
| new PrimitiveType(SourceInfo.NONE, "double"), |
|
306 |
| new Word (SourceInfo.NONE, "field1")), |
|
307 |
| new UninitializedVariableDeclarator(SourceInfo.NONE, |
|
308 |
| new PrimitiveType(SourceInfo.NONE, "boolean"), |
|
309 |
| new Word (SourceInfo.NONE, "field2"))}); |
|
310 |
1
| vdecl.visit(_ibiv);
|
|
311 |
1
| assertEquals("There should be one error", 1, errors.size());
|
|
312 |
1
| assertEquals("The error message should be correct", "You cannot have fields in interfaces at the Intermediate level", errors.getLast().getFirst());
|
|
313 |
| } |
|
314 |
| |
|
315 |
1
| public void testForAbstractMethodDef() {
|
|
316 |
| |
|
317 |
1
| MethodDef mdef = new AbstractMethodDef(SourceInfo.NONE,
|
|
318 |
| _abstractMav, |
|
319 |
| new TypeParameter[0], |
|
320 |
| new PrimitiveType(SourceInfo.NONE, "int"), |
|
321 |
| new Word(SourceInfo.NONE, "methodName"), |
|
322 |
| new FormalParameter[0], |
|
323 |
| new ReferenceType[0]); |
|
324 |
1
| _ibiv._enclosing.setMav(_abstractMav);
|
|
325 |
| |
|
326 |
1
| mdef.visit(_ibiv);
|
|
327 |
1
| assertEquals("There should not be any errors.", 0, errors.size());
|
|
328 |
| |
|
329 |
| |
|
330 |
1
| mdef = new AbstractMethodDef(SourceInfo.NONE,
|
|
331 |
| _abstractMav, |
|
332 |
| new TypeParameter[0], |
|
333 |
| new PrimitiveType(SourceInfo.NONE, "int"), |
|
334 |
| new Word(SourceInfo.NONE, "monkey"), |
|
335 |
| new FormalParameter[0], |
|
336 |
| new ReferenceType[0]); |
|
337 |
1
| mdef.visit(_ibiv);
|
|
338 |
1
| assertEquals("There should be one error.", 1, errors.size());
|
|
339 |
1
| assertEquals("The error message should be correct.",
|
|
340 |
| "Only constructors can have the same name as the class they appear in, and constructors cannot appear in interfaces.", |
|
341 |
| errors.get(0).getFirst()); |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
1
| AbstractMethodDef amd3 = new AbstractMethodDef(SourceInfo.NONE,
|
|
346 |
| _publicMav, |
|
347 |
| new TypeParameter[0], |
|
348 |
| new PrimitiveType(SourceInfo.NONE, "double"), |
|
349 |
| new Word(SourceInfo.NONE, "methodName2"), |
|
350 |
| new FormalParameter[0], |
|
351 |
| new ReferenceType[0]); |
|
352 |
1
| amd3.visit(_ibiv);
|
|
353 |
1
| assertEquals("There should still be one error", 1, errors.size());
|
|
354 |
1
| assertTrue("The method def should be public", _ibiv._enclosing.getMethods().get(1).hasModifier("public"));
|
|
355 |
| |
|
356 |
| |
|
357 |
1
| AbstractMethodDef amd4 = new AbstractMethodDef(SourceInfo.NONE,
|
|
358 |
| _privateMav, |
|
359 |
| new TypeParameter[0], |
|
360 |
| new PrimitiveType(SourceInfo.NONE, "double"), |
|
361 |
| new Word(SourceInfo.NONE, "methodName3"), |
|
362 |
| new FormalParameter[0], |
|
363 |
| new ReferenceType[0]); |
|
364 |
1
| amd4.visit(_ibiv);
|
|
365 |
1
| assertEquals("There should be two errors", 2, errors.size());
|
|
366 |
1
| assertEquals("The error message should be correct","Interface methods cannot be private. They must be public." , errors.get(1).getFirst());
|
|
367 |
| |
|
368 |
| |
|
369 |
1
| AbstractMethodDef amd5 = new AbstractMethodDef(SourceInfo.NONE,
|
|
370 |
| _protectedMav, |
|
371 |
| new TypeParameter[0], |
|
372 |
| new PrimitiveType(SourceInfo.NONE, "double"), |
|
373 |
| new Word(SourceInfo.NONE, "methodName4"), |
|
374 |
| new FormalParameter[0], |
|
375 |
| new ReferenceType[0]); |
|
376 |
1
| amd5.visit(_ibiv);
|
|
377 |
1
| assertEquals("There should be three errors", 3, errors.size());
|
|
378 |
1
| assertEquals("The error message should be correct","Interface methods cannot be protected. They must be public." , errors.get(2).getFirst());
|
|
379 |
| } |
|
380 |
| |
|
381 |
| |
|
382 |
1
| public void testForConstructorDef() {
|
|
383 |
| |
|
384 |
1
| ConstructorDef cd = new ConstructorDef(SourceInfo.NONE, new Word(SourceInfo.NONE, "MyClass"), _publicMav, new FormalParameter[0], new ReferenceType[0],
|
|
385 |
| new BracedBody(SourceInfo.NONE, new BodyItemI[0])); |
|
386 |
| |
|
387 |
| |
|
388 |
1
| cd.visit(_ibiv);
|
|
389 |
1
| assertEquals("There should now be one error", 1, errors.size());
|
|
390 |
1
| assertEquals("The error message should be correct", "Constructor definitions cannot appear in interfaces", errors.get(0).getFirst());
|
|
391 |
| |
|
392 |
| } |
|
393 |
| } |
|
394 |
| } |