|
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.JExprParser; |
|
41 |
| import java.util.*; |
|
42 |
| import java.io.File; |
|
43 |
| import edu.rice.cs.plt.reflect.JavaVersion; |
|
44 |
| import edu.rice.cs.plt.iter.*; |
|
45 |
| |
|
46 |
| import junit.framework.TestCase; |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| public class LValueTypeChecker extends JExpressionIFAbstractVisitor<TypeData> { |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| private final TestAssignable _testAssignableInstance; |
|
57 |
| |
|
58 |
| |
|
59 |
| private final SpecialTypeChecker _bob; |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
65
| public LValueTypeChecker(SpecialTypeChecker bob) {
|
|
65 |
65
| _testAssignableInstance = new TestAssignable(bob._data, bob._file, bob._package, bob._importedFiles, bob._importedPackages, bob._vars, bob._thrown);
|
|
66 |
65
| _bob = bob;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
3
| public TypeData defaultCase(JExpressionIF that) {
|
|
71 |
3
| _bob._addError("You cannot assign a value to an expression of this kind. Values can only be assigned to fields or variables", that);
|
|
72 |
3
| return null;
|
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
1
| public TypeData forIncrementExpression(IncrementExpression that) {
|
|
77 |
1
| _bob._addError("You cannot assign a value to an increment expression", that);
|
|
78 |
1
| return null;
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
83
| public TypeData forNameReference(NameReference that) {
|
|
83 |
83
| return that.visit(_testAssignableInstance);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
4
| public TypeData forArrayAccess(ArrayAccess that) {
|
|
88 |
4
| return that.visit(_testAssignableInstance);
|
|
89 |
| } |
|
90 |
| |
|
91 |
| |
|
92 |
5
| public TypeData forParenthesized(Parenthesized that) {
|
|
93 |
5
| return that.getValue().visit(this);
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| private class TestAssignable extends ExpressionTypeChecker { |
|
98 |
| |
|
99 |
65
| public TestAssignable(Data data, File file, String packageName, LinkedList<String> importedFiles, LinkedList<String> importedPackages, LinkedList<VariableData> vars, LinkedList<Pair<SymbolData, JExpression>> thrown) {
|
|
100 |
65
| super(data, file, packageName, importedFiles, importedPackages, vars, thrown);
|
|
101 |
| } |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
63
| public TypeData forSimpleNameReference(SimpleNameReference that) {
|
|
110 |
63
| Word myWord = that.getName();
|
|
111 |
63
| myWord.visit(this);
|
|
112 |
| |
|
113 |
63
| VariableData reference = getFieldOrVariable(myWord.getText(), _data, _data.getSymbolData(), that, _vars, true, true);
|
|
114 |
63
| if (reference != null) {
|
|
115 |
56
| if (! canBeAssigned(reference)) {
|
|
116 |
3
| _addError("You cannot assign a value to " + reference.getName() + " because it is immutable and has already been given a value", that.getName());
|
|
117 |
| } |
|
118 |
40
| else if (reference.gotValue()) { _bob.thingsThatHaveBeenAssigned.add(reference);}
|
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
56
| if (! reference.hasModifier("static") && inStaticMethod()) {
|
|
123 |
1
| _addError("Non static field or variable " + reference.getName() + " cannot be referenced from a static context", that);
|
|
124 |
| } |
|
125 |
| |
|
126 |
56
| return reference.getType().getInstanceData();
|
|
127 |
| } |
|
128 |
| |
|
129 |
7
| SymbolData classR = findClassReference(null, myWord.getText(), that);
|
|
130 |
1
| if (classR == SymbolData.AMBIGUOUS_REFERENCE) {return null;}
|
|
131 |
6
| if (classR != null) {
|
|
132 |
3
| if (checkAccess(that, classR.getMav(), classR.getName(), classR, _data.getSymbolData(), "class or interface", false)) {
|
|
133 |
2
| return classR;
|
|
134 |
| } |
|
135 |
| } |
|
136 |
| |
|
137 |
4
| PackageData packageD = new PackageData(myWord.getText());
|
|
138 |
4
| return packageD;
|
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
20
| public TypeData forComplexNameReference(ComplexNameReference that) {
|
|
149 |
20
| ExpressionTypeChecker etc = new ExpressionTypeChecker(_data, _file, _package, _importedFiles, _importedPackages, _vars, _thrown);
|
|
150 |
20
| TypeData lhs = that.getEnclosing().visit(etc);
|
|
151 |
20
| _bob.thingsThatHaveBeenAssigned.addAll(etc.thingsThatHaveBeenAssigned);
|
|
152 |
20
| Word myWord = that.getName();
|
|
153 |
| |
|
154 |
| |
|
155 |
20
| if (lhs instanceof PackageData) {
|
|
156 |
2
| SymbolData classRef = findClassReference(lhs, myWord.getText(), that);
|
|
157 |
1
| if (classRef != null) {return classRef;}
|
|
158 |
1
| return new PackageData((PackageData) lhs, myWord.getText());
|
|
159 |
| } |
|
160 |
| |
|
161 |
| |
|
162 |
18
| VariableData reference = getFieldOrVariable(myWord.getText(), lhs.getSymbolData(), _data.getSymbolData(), that);
|
|
163 |
18
| if (reference != null) {
|
|
164 |
12
| if (lhs instanceof SymbolData) {
|
|
165 |
| |
|
166 |
3
| if (! reference.hasModifier("static")) {
|
|
167 |
1
| _addError("Non-static variable " + Data.dollarSignsToDots(reference.getName()) + " cannot be accessed from the static context " + Data.dollarSignsToDots(lhs.getName()) + ". Perhaps you meant to instantiate an instance of " + Data.dollarSignsToDots(lhs.getName()), that);
|
|
168 |
1
| return reference.getType().getInstanceData();
|
|
169 |
| } |
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
11
| if (!canBeAssigned(reference)) {
|
|
174 |
2
| _addError("You cannot assign a value to " + Data.dollarSignsToDots(reference.getName()) + " because it is immutable and has already been given a value", that.getName());
|
|
175 |
| } |
|
176 |
| |
|
177 |
3
| else if (reference.gotValue()) { _bob.thingsThatHaveBeenAssigned.add(reference);}
|
|
178 |
| |
|
179 |
| |
|
180 |
11
| return reference.getType().getInstanceData();
|
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
6
| SymbolData sd = getSymbolData(true, myWord.getText(), lhs.getSymbolData(), that, false);
|
|
185 |
6
| if (sd != null && sd != SymbolData.AMBIGUOUS_REFERENCE) {
|
|
186 |
| |
|
187 |
1
| if (!checkAccess(that, sd.getMav(), sd.getName(), sd, _data.getSymbolData(), "class or interface")) {return null;}
|
|
188 |
| |
|
189 |
3
| if (!sd.hasModifier("static")) {
|
|
190 |
2
| _addError("Non-static inner class " + Data.dollarSignsToDots(sd.getName()) + " cannot be accessed from this context. Perhaps you meant to instantiate it", that);
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
1
| else if (lhs instanceof InstanceData) {
|
|
195 |
1
| _addError("You cannot reference the static inner class " + Data.dollarSignsToDots(sd.getName()) + " from an instance of " + Data.dollarSignsToDots(lhs.getName()), that);
|
|
196 |
| } |
|
197 |
3
| return sd;
|
|
198 |
| } |
|
199 |
| |
|
200 |
1
| if (sd != SymbolData.AMBIGUOUS_REFERENCE) {_addError("Could not resolve " + myWord.getText() + " from the context of " + Data.dollarSignsToDots(lhs.getName()), that);}
|
|
201 |
2
| return null;
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
4
| public TypeData forArrayAccess(ArrayAccess that) {
|
|
208 |
4
| ExpressionTypeChecker etc = new ExpressionTypeChecker(_data, _file, _package, _importedFiles, _importedPackages, _vars, _thrown);
|
|
209 |
4
| TypeData lhs = that.getArray().visit(etc);
|
|
210 |
4
| _bob.thingsThatHaveBeenAssigned.addAll(etc.thingsThatHaveBeenAssigned);
|
|
211 |
| |
|
212 |
4
| ExpressionTypeChecker indexTC = new ExpressionTypeChecker(_data, _file, _package, _importedFiles, _importedPackages, _vars, _thrown);
|
|
213 |
4
| TypeData index = that.getIndex().visit(indexTC);
|
|
214 |
4
| _bob.thingsThatHaveBeenAssigned.addAll(indexTC.thingsThatHaveBeenAssigned);
|
|
215 |
| |
|
216 |
| |
|
217 |
4
| return forArrayAccessOnly(that, lhs, index);
|
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
67
| protected boolean canBeAssigned(VariableData vd) {
|
|
222 |
67
| return !vd.isFinal() || !vd.hasValue();
|
|
223 |
| } |
|
224 |
| |
|
225 |
| } |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| public static class LValueTypeCheckerTest extends TestCase { |
|
231 |
| |
|
232 |
| private LValueTypeChecker _lvtc; |
|
233 |
| LValueTypeChecker.TestAssignable _ta; |
|
234 |
| |
|
235 |
| |
|
236 |
| private SymbolData _sd1; |
|
237 |
| private SymbolData _sd2; |
|
238 |
| private SymbolData _sd3; |
|
239 |
| private SymbolData _sd4; |
|
240 |
| private SymbolData _sd5; |
|
241 |
| private SymbolData _sd6; |
|
242 |
| private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"}); |
|
243 |
| private ModifiersAndVisibility _protectedMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"}); |
|
244 |
| private ModifiersAndVisibility _privateMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"private"}); |
|
245 |
| private ModifiersAndVisibility _packageMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]); |
|
246 |
| private ModifiersAndVisibility _abstractMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"abstract"}); |
|
247 |
| private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"}); |
|
248 |
| private ModifiersAndVisibility _finalPublicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final", "public"}); |
|
249 |
| private ModifiersAndVisibility _publicAbstractMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public", "abstract"}); |
|
250 |
| private ModifiersAndVisibility _publicStaticMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public", "static"}); |
|
251 |
| |
|
252 |
| |
|
253 |
0
| public LValueTypeCheckerTest() {
|
|
254 |
0
| this("");
|
|
255 |
| } |
|
256 |
6
| public LValueTypeCheckerTest(String name) {
|
|
257 |
6
| super(name);
|
|
258 |
| } |
|
259 |
| |
|
260 |
6
| public void setUp() {
|
|
261 |
6
| _sd1 = new SymbolData("i.like.monkey");
|
|
262 |
6
| _sd2 = new SymbolData("i.like.giraffe");
|
|
263 |
6
| _sd3 = new SymbolData("zebra");
|
|
264 |
6
| _sd4 = new SymbolData("u.like.emu");
|
|
265 |
6
| _sd5 = new SymbolData("");
|
|
266 |
6
| _sd6 = new SymbolData("cebu");
|
|
267 |
6
| _lvtc =
|
|
268 |
| new LValueTypeChecker(new SpecialTypeChecker(_sd1, new File(""), "", new LinkedList<String>(), new LinkedList<String>(), |
|
269 |
| new LinkedList<VariableData>(), new LinkedList<Pair<SymbolData, JExpression>>())); |
|
270 |
6
| _ta = _lvtc._testAssignableInstance;
|
|
271 |
6
| _lvtc._bob.errors = new LinkedList<Pair<String, JExpressionIF>>();
|
|
272 |
6
| LanguageLevelConverter.symbolTable.clear();
|
|
273 |
| |
|
274 |
6
| _lvtc._bob._importedPackages.addFirst("java.lang");
|
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
1
| public void testDefaultCase() {
|
|
280 |
| |
|
281 |
1
| new NullLiteral(SourceInfo.NONE).visit(_lvtc);
|
|
282 |
1
| assertEquals("Should be 1 error", 1, _lvtc._bob.errors.size());
|
|
283 |
1
| assertEquals("Error message should be correct", "You cannot assign a value to an expression of this kind. Values can only be assigned to fields or variables",
|
|
284 |
| _lvtc._bob.errors.getLast().getFirst()); |
|
285 |
| |
|
286 |
| |
|
287 |
1
| new PlusExpression(SourceInfo.NONE, new IntegerLiteral(SourceInfo.NONE, 21), new IntegerLiteral(SourceInfo.NONE, 22)).visit(_lvtc);
|
|
288 |
1
| assertEquals("Should be 2 errors", 2, _lvtc._bob.errors.size());
|
|
289 |
1
| assertEquals("Error message should be correct", "You cannot assign a value to an expression of this kind. Values can only be assigned to fields or variables",
|
|
290 |
| _lvtc._bob.errors.getLast().getFirst()); |
|
291 |
| } |
|
292 |
| |
|
293 |
| |
|
294 |
1
| public void testForIncrementExpression() {
|
|
295 |
| |
|
296 |
1
| PositivePrefixIncrementExpression p = new PositivePrefixIncrementExpression(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "bob")));
|
|
297 |
1
| assertEquals("Should return null", null, p.visit(_lvtc));
|
|
298 |
1
| assertEquals("Should be 1 error", 1, _lvtc._bob.errors.size());
|
|
299 |
1
| assertEquals("Error message should be correct", "You cannot assign a value to an increment expression", _lvtc._bob.errors.getLast().getFirst());
|
|
300 |
| } |
|
301 |
| |
|
302 |
1
| public void testForSimpleNameReference() {
|
|
303 |
| |
|
304 |
1
| SimpleNameReference var = new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "variable1"));
|
|
305 |
1
| VariableData varData = new VariableData("variable1", _publicMav, SymbolData.INT_TYPE, false, _ta._data);
|
|
306 |
1
| _ta._vars.add(varData);
|
|
307 |
| |
|
308 |
| |
|
309 |
1
| assertEquals("Should return int instance", SymbolData.INT_TYPE.getInstanceData(), var.visit(_lvtc));
|
|
310 |
1
| assertEquals("Should be 0 errors", 0, _lvtc._bob.errors.size());
|
|
311 |
| |
|
312 |
| |
|
313 |
1
| varData.gotValue();
|
|
314 |
1
| assertEquals("Should return int instance", SymbolData.INT_TYPE.getInstanceData(), var.visit(_lvtc));
|
|
315 |
1
| assertEquals("Should still be 0 errors", 0, _lvtc._bob.errors.size());
|
|
316 |
| |
|
317 |
| |
|
318 |
1
| varData.setMav(_finalMav);
|
|
319 |
1
| assertEquals("Should return int instance", SymbolData.INT_TYPE.getInstanceData(), var.visit(_lvtc));
|
|
320 |
1
| assertEquals("Should be 1 error", 1, _lvtc._bob.errors.size());
|
|
321 |
1
| assertEquals("Error message should be correct", "You cannot assign a value to variable1 because it is immutable and has already been given a value", _lvtc._bob.errors.getLast().getFirst());
|
|
322 |
1
| varData.setMav(_publicMav);
|
|
323 |
| |
|
324 |
| |
|
325 |
1
| MethodData newContext = new MethodData("method", _publicStaticMav, new TypeParameter[0], SymbolData.INT_TYPE, new VariableData[0], new String[0], _sd1, new NullLiteral(SourceInfo.NONE));
|
|
326 |
1
| _ta._data = newContext;
|
|
327 |
1
| assertEquals("Should return int instance", SymbolData.INT_TYPE.getInstanceData(), var.visit(_lvtc));
|
|
328 |
1
| assertEquals("Should be 2 errors", 2, _lvtc._bob.errors.size());
|
|
329 |
1
| assertEquals("Error message should be correct", "Non static field or variable variable1 cannot be referenced from a static context", _lvtc._bob.errors.getLast().getFirst());
|
|
330 |
1
| _ta._data = _sd1;
|
|
331 |
| |
|
332 |
| |
|
333 |
1
| _ta._vars = new LinkedList<VariableData>();
|
|
334 |
1
| _sd1.setSuperClass(_sd2);
|
|
335 |
1
| _sd2.addVar(varData);
|
|
336 |
1
| assertEquals("Should return int instance", SymbolData.INT_TYPE.getInstanceData(), var.visit(_lvtc));
|
|
337 |
1
| assertEquals("Should still be 2 errors", 2, _lvtc._bob.errors.size());
|
|
338 |
| |
|
339 |
| |
|
340 |
1
| SimpleNameReference className = new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "Frog"));
|
|
341 |
1
| SymbolData frog = new SymbolData("Frog");
|
|
342 |
1
| frog.setIsContinuation(false);
|
|
343 |
1
| _lvtc._bob.symbolTable.put("Frog", frog);
|
|
344 |
| |
|
345 |
| |
|
346 |
1
| TypeData result = className.visit(_lvtc);
|
|
347 |
1
| assertTrue("Result should be a PackageData since Frog is not accessible", result instanceof PackageData);
|
|
348 |
1
| assertEquals("Should have correct name", "Frog", result.getName());
|
|
349 |
1
| assertEquals("Should still be 2 errors", 2, _lvtc._bob.errors.size());
|
|
350 |
| |
|
351 |
| |
|
352 |
1
| frog.setMav(_publicMav);
|
|
353 |
1
| assertEquals("Should return Frog", frog, className.visit(_lvtc));
|
|
354 |
1
| assertEquals("Should still be 2 errors", 2, _lvtc._bob.errors.size());
|
|
355 |
| |
|
356 |
| |
|
357 |
1
| SimpleNameReference fake = new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "notRealReference"));
|
|
358 |
1
| assertEquals("Should return package data", "notRealReference", (fake.visit(_lvtc)).getName());
|
|
359 |
1
| assertEquals("Should still be just 2 errors", 2, _lvtc._bob.errors.size());
|
|
360 |
| |
|
361 |
| |
|
362 |
1
| SimpleNameReference ambigRef = new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "ambigThing"));
|
|
363 |
| |
|
364 |
1
| SymbolData interfaceD = new SymbolData("interface");
|
|
365 |
1
| interfaceD.setIsContinuation(false);
|
|
366 |
1
| interfaceD.setInterface(true);
|
|
367 |
1
| interfaceD.setMav(_publicMav);
|
|
368 |
| |
|
369 |
1
| SymbolData classD = new SymbolData("superClass");
|
|
370 |
1
| classD.setIsContinuation(false);
|
|
371 |
1
| classD.setMav(_publicMav);
|
|
372 |
| |
|
373 |
1
| SymbolData ambigThingI = new SymbolData("ambigThing");
|
|
374 |
1
| ambigThingI.setIsContinuation(false);
|
|
375 |
1
| ambigThingI.setInterface(true);
|
|
376 |
1
| interfaceD.addInnerInterface(ambigThingI);
|
|
377 |
1
| ambigThingI.setOuterData(interfaceD);
|
|
378 |
1
| ambigThingI.setMav(_publicStaticMav);
|
|
379 |
| |
|
380 |
1
| SymbolData ambigThingC = new SymbolData("ambigThing");
|
|
381 |
1
| ambigThingC.setIsContinuation(false);
|
|
382 |
1
| classD.addInnerClass(ambigThingC);
|
|
383 |
1
| ambigThingC.setOuterData(classD);
|
|
384 |
1
| ambigThingC.setMav(_publicStaticMav);
|
|
385 |
| |
|
386 |
1
| _sd6.addInterface(interfaceD);
|
|
387 |
1
| _sd6.setSuperClass(classD);
|
|
388 |
| |
|
389 |
1
| _sd6.setMav(_publicMav);
|
|
390 |
1
| _sd6.setIsContinuation(false);
|
|
391 |
| |
|
392 |
1
| _ta._data = _sd6;
|
|
393 |
| |
|
394 |
1
| assertEquals("Should return null", null, ambigRef.visit(_lvtc));
|
|
395 |
1
| assertEquals("Should be 3 errors", 3, _lvtc._bob.errors.size());
|
|
396 |
1
| assertEquals("Error message should be correct", "Ambiguous reference to class or interface ambigThing", _lvtc._bob.errors.getLast().getFirst());
|
|
397 |
| } |
|
398 |
| |
|
399 |
1
| public void testForComplexNameReference() {
|
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
1
| ComplexNameReference ref1 = new ComplexNameReference(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "java")), new Word(SourceInfo.NONE, "lang"));
|
|
404 |
1
| assertEquals("Should return correct package data", "java.lang", ref1.visit(_lvtc).getName());
|
|
405 |
1
| assertEquals("Should be no errors", 0, _lvtc._bob.errors.size());
|
|
406 |
| |
|
407 |
| |
|
408 |
1
| ComplexNameReference ref2 = new ComplexNameReference(SourceInfo.NONE, ref1, new Word(SourceInfo.NONE, "String"));
|
|
409 |
1
| SymbolData string = new SymbolData("java.lang.String");
|
|
410 |
1
| string.setPackage("java.lang");
|
|
411 |
1
| string.setMav(_publicMav);
|
|
412 |
1
| string.setIsContinuation(false);
|
|
413 |
1
| _lvtc._bob.symbolTable.put("java.lang.String", string);
|
|
414 |
| |
|
415 |
1
| assertEquals("Should return string", string, ref2.visit(_lvtc));
|
|
416 |
| |
|
417 |
1
| assertEquals("Should still be no errors", 0, _lvtc._bob.errors.size());
|
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
1
| VariableData myVar = new VariableData("myVar", _publicStaticMav, SymbolData.DOUBLE_TYPE, true, string);
|
|
424 |
1
| string.addVar(myVar);
|
|
425 |
1
| ComplexNameReference varRef1 = new ComplexNameReference(SourceInfo.NONE, ref2, new Word(SourceInfo.NONE, "myVar"));
|
|
426 |
| |
|
427 |
| |
|
428 |
1
| assertEquals("Should return Double_Type instance", SymbolData.DOUBLE_TYPE.getInstanceData(), varRef1.visit(_lvtc));
|
|
429 |
1
| assertEquals("There should still be no errors", 0, _lvtc._bob.errors.size());
|
|
430 |
| |
|
431 |
| |
|
432 |
1
| myVar.lostValue();
|
|
433 |
1
| assertEquals("Should return Double_Type instance", SymbolData.DOUBLE_TYPE.getInstanceData(), varRef1.visit(_lvtc));
|
|
434 |
1
| assertEquals("There should still be no errors", 0, _lvtc._bob.errors.size());
|
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
1
| myVar.setMav(_publicMav);
|
|
439 |
1
| assertEquals("Should return double instance", SymbolData.DOUBLE_TYPE.getInstanceData(), varRef1.visit(_lvtc));
|
|
440 |
1
| assertEquals("Should be 1 error", 1, _lvtc._bob.errors.size());
|
|
441 |
1
| assertEquals("Error message should be correct", "Non-static variable myVar cannot be accessed from the static context java.lang.String. Perhaps you meant to instantiate an instance of java.lang.String", _lvtc._bob.errors.getLast().getFirst());
|
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
1
| VariableData stringVar = new VariableData("s", _publicMav, string, true, _lvtc._bob._data);
|
|
446 |
1
| _ta._vars.add(stringVar);
|
|
447 |
1
| ComplexNameReference varRef2 = new ComplexNameReference(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "s")), new Word(SourceInfo.NONE, "myVar"));
|
|
448 |
1
| assertEquals("Should return double instance", SymbolData.DOUBLE_TYPE.getInstanceData(), varRef2.visit(_lvtc));
|
|
449 |
1
| assertEquals("Should still just be 1 error", 1, _lvtc._bob.errors.size());
|
|
450 |
| |
|
451 |
| |
|
452 |
1
| myVar.setMav(_finalPublicMav);
|
|
453 |
1
| myVar.gotValue();
|
|
454 |
1
| assertEquals("Should return double instance", SymbolData.DOUBLE_TYPE.getInstanceData(), varRef2.visit(_lvtc));
|
|
455 |
1
| assertEquals("Should be 2 errors", 2, _lvtc._bob.errors.size());
|
|
456 |
1
| assertEquals("Error message should be correct", "You cannot assign a value to myVar because it is immutable and has already been given a value", _lvtc._bob.errors.getLast().getFirst());
|
|
457 |
1
| myVar.setMav(_publicMav);
|
|
458 |
1
| myVar.lostValue();
|
|
459 |
| |
|
460 |
| |
|
461 |
1
| string.setVars(new LinkedList<VariableData>());
|
|
462 |
1
| string.setSuperClass(_sd2);
|
|
463 |
1
| _sd2.addVar(myVar);
|
|
464 |
1
| assertEquals("Should return double instance", SymbolData.DOUBLE_TYPE.getInstanceData(), varRef2.visit(_lvtc));
|
|
465 |
1
| assertEquals("Should still be 2 errors", 2, _lvtc._bob.errors.size());
|
|
466 |
| |
|
467 |
| |
|
468 |
1
| VariableData vd1 = new VariableData("Mojo", _publicMav, SymbolData.INT_TYPE, true, _sd1);
|
|
469 |
1
| VariableData vd2 = new VariableData("Santa's Little Helper", _publicMav, _sd1, true, _sd2);
|
|
470 |
1
| VariableData vd3 = new VariableData("Snowball1", _publicMav, _sd2, true, _sd3);
|
|
471 |
1
| _sd3.addVar(vd3);
|
|
472 |
1
| _sd2.addVar(vd2);
|
|
473 |
1
| _sd1.addVar(vd1);
|
|
474 |
| |
|
475 |
1
| ComplexNameReference varRef3 = new ComplexNameReference(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "Snowball1")),
|
|
476 |
| new Word(SourceInfo.NONE, "Santa's Little Helper")); |
|
477 |
1
| ComplexNameReference varRef4 = new ComplexNameReference(SourceInfo.NONE, varRef3, new Word(SourceInfo.NONE, "Mojo"));
|
|
478 |
| |
|
479 |
1
| Data oldData = _lvtc._bob._data;
|
|
480 |
1
| _lvtc._bob._data = _sd3;
|
|
481 |
1
| _lvtc._bob._vars.add(vd3);
|
|
482 |
| |
|
483 |
1
| TypeData result = varRef4.visit(_lvtc);
|
|
484 |
1
| assertEquals("Should return int instance", SymbolData.INT_TYPE.getInstanceData(), result);
|
|
485 |
1
| assertEquals("Should still be 2 errors", 2, _lvtc._bob.errors.size());
|
|
486 |
| |
|
487 |
1
| _lvtc._bob._data = oldData;
|
|
488 |
| |
|
489 |
| |
|
490 |
| |
|
491 |
| |
|
492 |
1
| SymbolData inner = new SymbolData("java.lang.String$Inner");
|
|
493 |
1
| inner.setPackage("java.lang");
|
|
494 |
1
| inner.setIsContinuation(false);
|
|
495 |
1
| inner.setOuterData(string);
|
|
496 |
1
| string.addInnerClass(inner);
|
|
497 |
| |
|
498 |
| |
|
499 |
1
| ComplexNameReference innerRef0 = new ComplexNameReference(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "s")), new Word(SourceInfo.NONE, "Inner"));
|
|
500 |
1
| assertEquals("Should return null", null, innerRef0.visit(_lvtc));
|
|
501 |
1
| assertEquals("Should be 3 errors", 3, _lvtc._bob.errors.size());
|
|
502 |
1
| assertEquals("Error message should be correct", "The class or interface java.lang.String.Inner is package protected because there is no access specifier and cannot be accessed from i.like.monkey", _lvtc._bob.errors.getLast().getFirst());
|
|
503 |
| |
|
504 |
1
| inner.setMav(_publicMav);
|
|
505 |
| |
|
506 |
| |
|
507 |
1
| ComplexNameReference innerRef1 = new ComplexNameReference(SourceInfo.NONE, ref2, new Word(SourceInfo.NONE, "Inner"));
|
|
508 |
1
| assertEquals("Should return inner", inner, innerRef1.visit(_lvtc));
|
|
509 |
1
| assertEquals("Should be 4 errors", 4, _lvtc._bob.errors.size());
|
|
510 |
1
| assertEquals("Error message should be correct", "Non-static inner class java.lang.String.Inner cannot be accessed from this context. Perhaps you meant to instantiate it", _lvtc._bob.errors.getLast().getFirst());
|
|
511 |
| |
|
512 |
| |
|
513 |
1
| ComplexNameReference innerRef2 = new ComplexNameReference(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "s")), new Word(SourceInfo.NONE, "Inner"));
|
|
514 |
1
| assertEquals("Should return inner", inner, innerRef2.visit(_lvtc));
|
|
515 |
1
| assertEquals("Should be 5 errors", 5, _lvtc._bob.errors.size());
|
|
516 |
1
| assertEquals("Error message should be correct", "Non-static inner class java.lang.String.Inner cannot be accessed from this context. Perhaps you meant to instantiate it", _lvtc._bob.errors.getLast().getFirst());
|
|
517 |
| |
|
518 |
| |
|
519 |
1
| inner.setMav(_publicStaticMav);
|
|
520 |
1
| assertEquals("Should return inner", inner, innerRef2.visit(_lvtc));
|
|
521 |
1
| assertEquals("Should be 6 errors", 6, _lvtc._bob.errors.size());
|
|
522 |
1
| assertEquals("Error message should be correct", "You cannot reference the static inner class java.lang.String.Inner from an instance of java.lang.String", _lvtc._bob.errors.getLast().getFirst());
|
|
523 |
| |
|
524 |
| |
|
525 |
| |
|
526 |
1
| ComplexNameReference noSense = new ComplexNameReference(SourceInfo.NONE, ref2, new Word(SourceInfo.NONE, "nonsense"));
|
|
527 |
1
| assertEquals("Should return null", null, noSense.visit(_lvtc));
|
|
528 |
1
| assertEquals("Should be 7 errors", 7, _lvtc._bob.errors.size());
|
|
529 |
1
| assertEquals("Error message should be correct", "Could not resolve nonsense from the context of java.lang.String", _lvtc._bob.errors.getLast().getFirst());
|
|
530 |
| |
|
531 |
| |
|
532 |
1
| ComplexNameReference ambigRef = new ComplexNameReference(SourceInfo.NONE, new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "cebu")), new Word(SourceInfo.NONE, "ambigThing"));
|
|
533 |
| |
|
534 |
1
| SymbolData interfaceD = new SymbolData("interface");
|
|
535 |
1
| interfaceD.setIsContinuation(false);
|
|
536 |
1
| interfaceD.setInterface(true);
|
|
537 |
1
| interfaceD.setMav(_publicMav);
|
|
538 |
| |
|
539 |
1
| SymbolData classD = new SymbolData("superClass");
|
|
540 |
1
| classD.setIsContinuation(false);
|
|
541 |
1
| classD.setMav(_publicMav);
|
|
542 |
| |
|
543 |
1
| SymbolData ambigThingI = new SymbolData("ambigThing");
|
|
544 |
1
| ambigThingI.setIsContinuation(false);
|
|
545 |
1
| ambigThingI.setInterface(true);
|
|
546 |
1
| interfaceD.addInnerInterface(ambigThingI);
|
|
547 |
1
| ambigThingI.setOuterData(interfaceD);
|
|
548 |
1
| ambigThingI.setMav(_publicStaticMav);
|
|
549 |
| |
|
550 |
1
| SymbolData ambigThingC = new SymbolData("ambigThing");
|
|
551 |
1
| ambigThingC.setIsContinuation(false);
|
|
552 |
1
| classD.addInnerClass(ambigThingC);
|
|
553 |
1
| ambigThingC.setOuterData(classD);
|
|
554 |
1
| ambigThingC.setMav(_publicStaticMav);
|
|
555 |
| |
|
556 |
1
| _sd6.addInterface(interfaceD);
|
|
557 |
1
| _sd6.setSuperClass(classD);
|
|
558 |
| |
|
559 |
1
| _lvtc._bob.symbolTable.put("cebu", _sd6);
|
|
560 |
1
| _sd6.setMav(_publicMav);
|
|
561 |
1
| _sd6.setIsContinuation(false);
|
|
562 |
| |
|
563 |
1
| assertEquals("Should return null", null, ambigRef.visit(_lvtc));
|
|
564 |
1
| assertEquals("Should be 8 errors", 8, _lvtc._bob.errors.size());
|
|
565 |
1
| assertEquals("Error message should be correct", "Ambiguous reference to class or interface ambigThing", _lvtc._bob.errors.getLast().getFirst());
|
|
566 |
| |
|
567 |
| |
|
568 |
| } |
|
569 |
| |
|
570 |
1
| public void testForArrayAccess() {
|
|
571 |
1
| ArrayData intArray =
|
|
572 |
| new ArrayData(SymbolData.INT_TYPE, |
|
573 |
| new LanguageLevelVisitor(_lvtc._bob._file, |
|
574 |
| _lvtc._bob._package, |
|
575 |
| null, |
|
576 |
| _lvtc._bob._importedFiles, |
|
577 |
| _lvtc._bob._importedPackages, |
|
578 |
| new HashSet<String>(), |
|
579 |
| new Hashtable<String, Triple<SourceInfo, LanguageLevelVisitor, SymbolData>>(), |
|
580 |
| new LinkedList<Command>()), |
|
581 |
| SourceInfo.NONE); |
|
582 |
1
| VariableData variable1 = new VariableData("variable1", _publicMav, intArray, true, _ta._data);
|
|
583 |
1
| _ta._vars.add(variable1);
|
|
584 |
| |
|
585 |
1
| VariableData intVar = new VariableData("intVar", _publicMav, SymbolData.INT_TYPE, true, _ta._data);
|
|
586 |
1
| _ta._vars.add(intVar);
|
|
587 |
| |
|
588 |
1
| MethodData makeArray = new MethodData("makeArray", _privateMav, new TypeParameter[0], intArray, new VariableData[0], new String[0], _ta._data.getSymbolData(), new NullLiteral(SourceInfo.NONE));
|
|
589 |
1
| _ta._data.getSymbolData().addMethod(makeArray);
|
|
590 |
| |
|
591 |
| |
|
592 |
| |
|
593 |
1
| ArrayAccess a1 = new ArrayAccess(SourceInfo.NONE,
|
|
594 |
| new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "variable1")), |
|
595 |
| new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "intVar"))); |
|
596 |
| |
|
597 |
1
| assertEquals("should return int", SymbolData.INT_TYPE.getInstanceData(), a1.visit(_lvtc));
|
|
598 |
1
| assertEquals("Should be 0 errors", 0, _lvtc._bob.errors.size());
|
|
599 |
| |
|
600 |
| |
|
601 |
| |
|
602 |
| |
|
603 |
1
| ArrayAccess a2 = new ArrayAccess(SourceInfo.NONE,
|
|
604 |
| new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "variable1")), |
|
605 |
| new PlusExpression(SourceInfo.NONE, |
|
606 |
| new IntegerLiteral(SourceInfo.NONE, 12), |
|
607 |
| new IntegerLiteral(SourceInfo.NONE, 22))); |
|
608 |
| |
|
609 |
1
| assertEquals("should return int", SymbolData.INT_TYPE.getInstanceData(), a2.visit(_lvtc));
|
|
610 |
1
| assertEquals("Should be 0 errors", 0, _lvtc._bob.errors.size());
|
|
611 |
| |
|
612 |
| |
|
613 |
| |
|
614 |
1
| ArrayAccess a3 = new ArrayAccess(SourceInfo.NONE,
|
|
615 |
| new SimpleMethodInvocation(SourceInfo.NONE, |
|
616 |
| new Word(SourceInfo.NONE, "makeArray"), |
|
617 |
| new ParenthesizedExpressionList(SourceInfo.NONE, new Expression[0])), |
|
618 |
| new SimpleNameReference(SourceInfo.NONE, new Word(SourceInfo.NONE, "intVar"))); |
|
619 |
1
| assertEquals("should return int", SymbolData.INT_TYPE.getInstanceData(), a3.visit(_lvtc));
|
|
620 |
1
| assertEquals("Should be 0 errors", 0, _lvtc._bob.errors.size());
|
|
621 |
| } |
|
622 |
| |
|
623 |
1
| public void testForParenthesized() {
|
|
624 |
| |
|
625 |
1
| VariableData x = new VariableData("x", _publicMav, SymbolData.INT_TYPE, false, _ta._data);
|
|
626 |
1
| _ta._vars.add(x);
|
|
627 |
| |
|
628 |
| |
|
629 |
1
| Parenthesized p1 = new Parenthesized(SourceInfo.NONE,
|
|
630 |
| new Parenthesized(SourceInfo.NONE, |
|
631 |
| new Parenthesized(SourceInfo.NONE, |
|
632 |
| new SimpleNameReference(SourceInfo.NONE, |
|
633 |
| new Word(SourceInfo.NONE, "x"))))); |
|
634 |
| |
|
635 |
1
| assertEquals("should return int", SymbolData.INT_TYPE.getInstanceData(), p1.visit(_lvtc));
|
|
636 |
1
| assertEquals("Should be 0 errors", 0, _lvtc._bob.errors.size());
|
|
637 |
| |
|
638 |
| |
|
639 |
1
| Parenthesized p2 = new Parenthesized(SourceInfo.NONE,
|
|
640 |
| new Parenthesized(SourceInfo.NONE, |
|
641 |
| new IntegerLiteral(SourceInfo.NONE, 1))); |
|
642 |
1
| assertEquals("should return null", null, p2.visit(_lvtc));
|
|
643 |
1
| assertEquals("Should be 1 error", 1, _lvtc._bob.errors.size());
|
|
644 |
1
| assertEquals("Error message should be correct", "You cannot assign a value to an expression of this kind. Values can only be assigned to fields or variables",
|
|
645 |
| _lvtc._bob.errors.getLast().getFirst()); |
|
646 |
| |
|
647 |
| } |
|
648 |
| |
|
649 |
| } |
|
650 |
| } |