|
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 java.util.*; |
|
41 |
| import junit.framework.TestCase; |
|
42 |
| import edu.rice.cs.javalanglevels.parser.JExprParser; |
|
43 |
| |
|
44 |
| |
|
45 |
| public abstract class Data { |
|
46 |
| |
|
47 |
| |
|
48 |
| protected String _name; |
|
49 |
| |
|
50 |
| |
|
51 |
| protected LinkedList<VariableData> _vars; |
|
52 |
| |
|
53 |
| |
|
54 |
| protected LinkedList<Data> _enclosingData; |
|
55 |
| |
|
56 |
| |
|
57 |
| protected ModifiersAndVisibility _modifiersAndVisibility; |
|
58 |
| |
|
59 |
| |
|
60 |
| protected Data _outerData; |
|
61 |
| |
|
62 |
| |
|
63 |
| protected LinkedList<SymbolData> _innerClasses; |
|
64 |
| |
|
65 |
| |
|
66 |
| protected LinkedList<BlockData> _blocks; |
|
67 |
| |
|
68 |
| |
|
69 |
| protected Iterator<BlockData> _blockIterator; |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
935432
| public Data(Data outerData) {
|
|
74 |
935432
| _name = "";
|
|
75 |
935432
| _modifiersAndVisibility = null;
|
|
76 |
935432
| _vars = new LinkedList<VariableData>();
|
|
77 |
935432
| _enclosingData = new LinkedList<Data>();
|
|
78 |
935432
| _outerData = outerData;
|
|
79 |
935432
| if (outerData != null) {
|
|
80 |
803937
| _enclosingData.addLast(_outerData);
|
|
81 |
| } |
|
82 |
935432
| _innerClasses = new LinkedList<SymbolData>();
|
|
83 |
935432
| _blocks = new LinkedList<BlockData>();
|
|
84 |
935432
| _blockIterator = null;
|
|
85 |
| } |
|
86 |
| |
|
87 |
| |
|
88 |
261181
| public String getName() { return _name; }
|
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
1
| void setName(String name) { _name = name; }
|
|
94 |
| |
|
95 |
19
| public Boolean isAnonymousClass() {
|
|
96 |
19
| int lastIndex = _name.lastIndexOf('$');
|
|
97 |
19
| try { return (lastIndex < 0) && Integer.parseInt(_name.substring(lastIndex+1)) >= 0; }
|
|
98 |
19
| catch(NumberFormatException e) { return false; }
|
|
99 |
| } |
|
100 |
| |
|
101 |
19
| public Boolean isDoublyAnonymous() {
|
|
102 |
19
| if (! isAnonymousClass()) return false;
|
|
103 |
0
| for (Data d: getEnclosingData()) {
|
|
104 |
0
| if (d.isAnonymousClass()) return true;
|
|
105 |
| } |
|
106 |
0
| return false;
|
|
107 |
| } |
|
108 |
| |
|
109 |
| |
|
110 |
114
| void setVars(LinkedList<VariableData> vars) { _vars = vars; }
|
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
67
| public VariableData getVar(String name) {
|
|
117 |
67
| Iterator<VariableData> iter = _vars.iterator();
|
|
118 |
67
| while (iter.hasNext()) {
|
|
119 |
82
| VariableData vd = iter.next();
|
|
120 |
82
| if (vd.getName().equals(name)) {
|
|
121 |
53
| return vd;
|
|
122 |
| } |
|
123 |
| } |
|
124 |
14
| return null;
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
2466
| public LinkedList<VariableData> getVars() { return _vars; }
|
|
129 |
| |
|
130 |
| |
|
131 |
355
| public LinkedList<Data> getEnclosingData() { return _enclosingData; }
|
|
132 |
| |
|
133 |
| |
|
134 |
104119
| public void addEnclosingData(Data enclosingData) {
|
|
135 |
104119
| assert enclosingData != null;
|
|
136 |
104119
| if (!_enclosingData.contains(enclosingData)) {
|
|
137 |
104044
| _enclosingData.addFirst(enclosingData);
|
|
138 |
| } |
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
1
| public void setEnclosingData(LinkedList<Data> d) { _enclosingData = d; }
|
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
293638
| private boolean _repeatedName (VariableData vr) {
|
|
150 |
293638
| Iterator<VariableData> iter = _vars.iterator();
|
|
151 |
293638
| while (iter.hasNext()) {
|
|
152 |
4374925
| VariableData next = iter.next();
|
|
153 |
4374925
| if (vr.getName().equals(next.getName())) {
|
|
154 |
17
| return true;
|
|
155 |
| } |
|
156 |
| } |
|
157 |
293621
| return false;
|
|
158 |
| } |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
293384
| public boolean addVar(VariableData var) {
|
|
166 |
293384
| if (! _repeatedName(var)) {
|
|
167 |
293373
| _vars.addLast(var);
|
|
168 |
293373
| return true;
|
|
169 |
| } |
|
170 |
11
| else return false;
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
298
| public boolean addVars(VariableData[] vars) {
|
|
179 |
298
| boolean success = true;
|
|
180 |
298
| for (int i = 0; i < vars.length; i++) {
|
|
181 |
| |
|
182 |
211
| if (! _repeatedName(vars[i])) {
|
|
183 |
208
| _vars.addLast(vars[i]);
|
|
184 |
| } |
|
185 |
3
| else success = false;
|
|
186 |
| } |
|
187 |
298
| return success;
|
|
188 |
| } |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
40
| public boolean addFinalVars(VariableData[] vars) {
|
|
197 |
40
| boolean success = true;
|
|
198 |
40
| for (int i = 0; i < vars.length; i++) {
|
|
199 |
40
| if (! _repeatedName(vars[i])) {
|
|
200 |
6
| if (! vars[i].isFinal()) vars[i].setFinal();
|
|
201 |
38
| _vars.addLast(vars[i]);
|
|
202 |
| } |
|
203 |
2
| else { success = false; }
|
|
204 |
| } |
|
205 |
40
| return success;
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
286679
| public ModifiersAndVisibility getMav() { return _modifiersAndVisibility; }
|
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
64030
| public void setMav(ModifiersAndVisibility modifiersAndVisibility) {
|
|
215 |
64030
| _modifiersAndVisibility = modifiersAndVisibility;
|
|
216 |
| } |
|
217 |
| |
|
218 |
| |
|
219 |
| public abstract SymbolData getSymbolData(); |
|
220 |
| |
|
221 |
| |
|
222 |
155624
| public Data getOuterData() { return _outerData; }
|
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
106
| public void setOuterData(Data outerData) {
|
|
228 |
106
| if (outerData == null) {
|
|
229 |
0
| assert _outerData == null;
|
|
230 |
0
| return;
|
|
231 |
| } |
|
232 |
106
| if (_outerData == null || _outerData.equals(outerData)) {
|
|
233 |
106
| _outerData = outerData;
|
|
234 |
106
| if (! _enclosingData.contains(outerData)) _enclosingData.addLast(outerData);
|
|
235 |
| } |
|
236 |
| else { |
|
237 |
0
| throw new RuntimeException("Internal Program Error: Trying to reset an outer data to " + outerData.getName() +
|
|
238 |
| " for " + getName() + " that has already been set to " + _outerData.getName() + |
|
239 |
| ". Please report this bug."); |
|
240 |
| } |
|
241 |
| } |
|
242 |
| |
|
243 |
| |
|
244 |
3778
| public boolean isOuterData(Data d) {
|
|
245 |
3778
| Data outerData = _outerData;
|
|
246 |
3778
| while ((outerData != null) && ! LanguageLevelVisitor.isJavaLibraryClass(outerData.getName())) {
|
|
247 |
91
| if (outerData == d) return true;
|
|
248 |
197
| outerData = outerData.getOuterData();
|
|
249 |
| } |
|
250 |
3687
| return false;
|
|
251 |
| } |
|
252 |
| |
|
253 |
| |
|
254 |
0
| public SymbolData getEnclosingClass() {
|
|
255 |
0
| Data next = _outerData;
|
|
256 |
| |
|
257 |
0
| while (next != null) {
|
|
258 |
0
| if (next instanceof SymbolData) return (SymbolData) next;
|
|
259 |
0
| next = next.getOuterData();
|
|
260 |
| } |
|
261 |
0
| return null;
|
|
262 |
| } |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
341
| public static String dollarSignsToDots(String s) { return s.replace('$', '.'); }
|
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
0
| public static String dotsToDollarSigns(String s) { return s.replace('.', '$'); }
|
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
24
| public SymbolData getNextAnonymousInnerClass() {
|
|
283 |
24
| String name = getSymbolData().getName() + '$' + getSymbolData().preincrementAnonymousInnerClassNum();
|
|
284 |
| |
|
285 |
24
| LinkedList<SymbolData> myDatas = getInnerClasses();
|
|
286 |
24
| SymbolData myData = null;
|
|
287 |
| |
|
288 |
24
| for (int i = 0; i < myDatas.size(); i++) {
|
|
289 |
46
| if (myDatas.get(i).getName().equals(name)) {
|
|
290 |
22
| myData = myDatas.get(i);
|
|
291 |
22
| break;
|
|
292 |
| } |
|
293 |
| } |
|
294 |
24
| return myData;
|
|
295 |
| } |
|
296 |
| |
|
297 |
| |
|
298 |
17
| public void resetBlockIterator() { _blockIterator = null; }
|
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
93
| public BlockData getNextBlock() {
|
|
304 |
56
| if (_blockIterator == null) { _blockIterator = _blocks.iterator(); }
|
|
305 |
| |
|
306 |
93
| if (_blockIterator.hasNext()) { return _blockIterator.next(); }
|
|
307 |
0
| else { return null; }
|
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
111
| public void addBlock(BlockData b) { _blocks.add(b); }
|
|
312 |
| |
|
313 |
| |
|
314 |
10
| public void removeAllBlocks() { _blocks.clear(); }
|
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
| |
|
320 |
| |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
3234
| public SymbolData getInnerClassOrInterface(String relName) {
|
|
329 |
| |
|
330 |
3234
| int firstIndexOfDot = relName.indexOf('.');
|
|
331 |
3234
| int firstIndexOfDollar = relName.indexOf("$");
|
|
332 |
1714
| if (firstIndexOfDot == -1) firstIndexOfDot = firstIndexOfDollar;
|
|
333 |
3
| else if (firstIndexOfDollar >= 0 && firstIndexOfDollar < firstIndexOfDot) firstIndexOfDot = firstIndexOfDollar;
|
|
334 |
| |
|
335 |
| |
|
336 |
3234
| SymbolData privateResult = null;
|
|
337 |
3234
| SymbolData result = getInnerClassOrInterfaceHelper(relName, firstIndexOfDot);
|
|
338 |
3234
| if (relName.equals("MyInner")) {
|
|
339 |
| |
|
340 |
| |
|
341 |
| |
|
342 |
| } |
|
343 |
3234
| if (result != null) {
|
|
344 |
| |
|
345 |
103
| SymbolData outerPiece;
|
|
346 |
103
| if (firstIndexOfDot > 0) {
|
|
347 |
7
| outerPiece = getInnerClassOrInterfaceHelper(relName.substring(0, firstIndexOfDot), -1);
|
|
348 |
| } |
|
349 |
96
| else { outerPiece = result; }
|
|
350 |
95
| if (TypeChecker.checkAccess(outerPiece.getMav(), outerPiece, getSymbolData())) return result;
|
|
351 |
| else { |
|
352 |
8
| privateResult = result;
|
|
353 |
8
| result = null;
|
|
354 |
| } |
|
355 |
| } |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
3139
| if (_outerData != null) {
|
|
360 |
1352
| result = _outerData.getInnerClassOrInterface(relName);
|
|
361 |
| |
|
362 |
7
| if (result != null) return result;
|
|
363 |
| } |
|
364 |
| |
|
365 |
3132
| return privateResult;
|
|
366 |
| } |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
1270
| protected SymbolData getInnerClassOrInterfaceHelper(String relName, int firstIndexOfDot) {
|
|
377 |
1270
| Iterator<SymbolData> iter = innerClassesAndInterfacesIterator();
|
|
378 |
1270
| while (iter.hasNext()) {
|
|
379 |
11
| SymbolData sd = iter.next();
|
|
380 |
11
| String sdName = sd.getName();
|
|
381 |
| |
|
382 |
11
| sdName = LanguageLevelVisitor.getUnqualifiedClassName(sdName);
|
|
383 |
| |
|
384 |
| |
|
385 |
11
| if (firstIndexOfDot == -1) {
|
|
386 |
5
| if (sdName.equals(relName)) return sd;
|
|
387 |
| } |
|
388 |
| else { |
|
389 |
1
| if (sdName.equals(relName.substring(0, firstIndexOfDot))) {
|
|
390 |
0
| return sd.getInnerClassOrInterface(relName.substring(firstIndexOfDot + 1));
|
|
391 |
| } |
|
392 |
| } |
|
393 |
| } |
|
394 |
1265
| return null;
|
|
395 |
| } |
|
396 |
| |
|
397 |
1270
| public Iterator<SymbolData> innerClassesAndInterfacesIterator() { return _innerClasses.iterator(); }
|
|
398 |
| |
|
399 |
| |
|
400 |
150403
| public LinkedList<SymbolData> getInnerClasses() { return _innerClasses; }
|
|
401 |
| |
|
402 |
| |
|
403 |
0
| public void setInnerClasses(LinkedList<SymbolData> innerClasses) { _innerClasses = innerClasses; }
|
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
122
| public void addInnerClass(SymbolData innerClass) { _innerClasses.addLast(innerClass); }
|
|
409 |
| |
|
410 |
| |
|
411 |
2592
| public boolean hasModifier(String modifier) {
|
|
412 |
0
| if (getMav() == null) {return false;}
|
|
413 |
2592
| String[] mavStrings = _modifiersAndVisibility.getModifiers();
|
|
414 |
2592
| for (int i = 0; i < mavStrings.length; i++) {
|
|
415 |
2309
| if (mavStrings[i].equals(modifier)) {
|
|
416 |
289
| return true;
|
|
417 |
| } |
|
418 |
| } |
|
419 |
2303
| return false;
|
|
420 |
| } |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
176
| public void addModifier(String modifier) {
|
|
427 |
176
| if (! hasModifier(modifier)) {
|
|
428 |
0
| if (_modifiersAndVisibility == null) { setMav(new ModifiersAndVisibility(SourceInfo.NONE, new String[0])); }
|
|
429 |
156
| String[] modifiers = _modifiersAndVisibility.getModifiers();
|
|
430 |
156
| String[] newModifiers = new String[modifiers.length + 1];
|
|
431 |
156
| newModifiers[0] = modifier;
|
|
432 |
156
| for (int i = 1; i <= modifiers.length; i++) {
|
|
433 |
55
| newModifiers[i] = modifiers[i-1];
|
|
434 |
| } |
|
435 |
156
| _modifiersAndVisibility = new ModifiersAndVisibility(_modifiersAndVisibility.getSourceInfo(), newModifiers);
|
|
436 |
| } |
|
437 |
| } |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
15
| public String createUniqueName(String varName) {
|
|
445 |
15
| VariableData vd =
|
|
446 |
| TypeChecker.getFieldOrVariable(varName, this, getSymbolData(), new NullLiteral(SourceInfo.NONE), getVars(), |
|
447 |
| true, false); |
|
448 |
15
| String newName = varName;
|
|
449 |
15
| int counter = 0;
|
|
450 |
15
| while (vd != null && counter != -1) {
|
|
451 |
7
| newName = varName + counter; counter++;
|
|
452 |
7
| vd = TypeChecker.getFieldOrVariable(newName, this, getSymbolData(), new NullLiteral(SourceInfo.NONE),
|
|
453 |
| getVars(), true, false); |
|
454 |
| } |
|
455 |
0
| if (counter == -1) { throw new RuntimeException("Internal Program Error: Unable to rename variable " + varName
|
|
456 |
| + ". All possible names were taken. Please report this bug");} |
|
457 |
15
| return newName;
|
|
458 |
| } |
|
459 |
| |
|
460 |
| |
|
461 |
| |
|
462 |
| public static class DataTest extends TestCase { |
|
463 |
| |
|
464 |
| private Data _d; |
|
465 |
| |
|
466 |
| private ModifiersAndVisibility _publicMav = |
|
467 |
| new ModifiersAndVisibility(SourceInfo.NONE, new String[] { "public" }); |
|
468 |
| private ModifiersAndVisibility _staticMav = |
|
469 |
| new ModifiersAndVisibility(SourceInfo.NONE, new String[] { "static" }); |
|
470 |
| private ModifiersAndVisibility _lotsaMav = |
|
471 |
| new ModifiersAndVisibility(SourceInfo.NONE, new String[] { "public", "final", "abstract" }); |
|
472 |
| private ModifiersAndVisibility _protectedMav = |
|
473 |
| new ModifiersAndVisibility(SourceInfo.NONE, new String[] { "protected" }); |
|
474 |
| private ModifiersAndVisibility _privateMav = |
|
475 |
| new ModifiersAndVisibility(SourceInfo.NONE, new String[] { "private" }); |
|
476 |
| |
|
477 |
0
| public DataTest() { this("");}
|
|
478 |
9
| public DataTest(String name) { super(name); }
|
|
479 |
| |
|
480 |
1
| public void testRepeatedName() {
|
|
481 |
1
| _d = new SymbolData("myname");
|
|
482 |
| |
|
483 |
1
| VariableData vd = new VariableData("v1", _publicMav, SymbolData.INT_TYPE, false, _d);
|
|
484 |
| |
|
485 |
| |
|
486 |
1
| assertFalse("No variables to repeat name", _d._repeatedName(vd));
|
|
487 |
| |
|
488 |
| |
|
489 |
1
| _d.addVar(new VariableData("v2", _protectedMav, SymbolData.BOOLEAN_TYPE, true, _d));
|
|
490 |
1
| assertFalse("No repeated name", _d._repeatedName(vd));
|
|
491 |
| |
|
492 |
| |
|
493 |
1
| _d.addVar(vd);
|
|
494 |
1
| assertTrue("Should be repeated name", _d._repeatedName(vd));
|
|
495 |
| |
|
496 |
| } |
|
497 |
| |
|
498 |
1
| public void testIsAbstract() {
|
|
499 |
1
| _d = new SymbolData("myName");
|
|
500 |
1
| _d.setMav(_publicMav);
|
|
501 |
| |
|
502 |
| |
|
503 |
1
| assertFalse("Should not be abstract", _d.hasModifier("abstract"));
|
|
504 |
| |
|
505 |
1
| _d.setMav(_lotsaMav);
|
|
506 |
| |
|
507 |
| |
|
508 |
1
| assertTrue("Should be abstract", _d.hasModifier("abstract"));
|
|
509 |
| |
|
510 |
| |
|
511 |
| } |
|
512 |
| |
|
513 |
1
| public void testAddVar() {
|
|
514 |
1
| _d = new SymbolData("myName");
|
|
515 |
1
| VariableData vd = new VariableData("v1", _publicMav, SymbolData.INT_TYPE, true, _d);
|
|
516 |
1
| VariableData vd2 = new VariableData("v2", _publicMav, SymbolData.CHAR_TYPE, true, _d);
|
|
517 |
1
| LinkedList<VariableData> myVds = new LinkedList<VariableData>();
|
|
518 |
1
| myVds.addLast(vd);
|
|
519 |
| |
|
520 |
| |
|
521 |
1
| assertTrue("Should be able to add first variable", _d.addVar(vd));
|
|
522 |
1
| assertEquals("Variable list should have 1 variable, vd", myVds, _d.getVars());
|
|
523 |
| |
|
524 |
| |
|
525 |
1
| assertFalse("Should not be able to add same variable again", _d.addVar(vd));
|
|
526 |
1
| assertEquals("Variable list should not have changed", myVds, _d.getVars());
|
|
527 |
| |
|
528 |
| |
|
529 |
1
| myVds.addLast(vd2);
|
|
530 |
1
| assertTrue("Should be able to add a different variable", _d.addVar(vd2));
|
|
531 |
1
| assertEquals("Variable list should have 2 variables, vd, vd2", myVds, _d.getVars());
|
|
532 |
| |
|
533 |
| |
|
534 |
| |
|
535 |
| } |
|
536 |
| |
|
537 |
1
| public void testAddVars() {
|
|
538 |
1
| _d = new SymbolData("genius");
|
|
539 |
1
| VariableData vd = new VariableData("v1", _publicMav, SymbolData.INT_TYPE, true, _d);
|
|
540 |
1
| VariableData vd2 = new VariableData("v2", _publicMav, SymbolData.CHAR_TYPE, true, _d);
|
|
541 |
1
| VariableData[] toAdd = new VariableData[] {vd, vd2};
|
|
542 |
1
| LinkedList<VariableData> myVds = new LinkedList<VariableData>();
|
|
543 |
| |
|
544 |
| |
|
545 |
1
| myVds.addLast(vd);
|
|
546 |
1
| myVds.addLast(vd2);
|
|
547 |
1
| assertTrue("Should be able to add new vars array", _d.addVars(toAdd));
|
|
548 |
1
| assertEquals("Variable list should have 2 variables", myVds, _d.getVars());
|
|
549 |
| |
|
550 |
| |
|
551 |
1
| assertFalse("Should not be able to add same variables again", _d.addVars(toAdd));
|
|
552 |
1
| assertEquals("Variable list should not have changed", myVds, _d.getVars());
|
|
553 |
| |
|
554 |
| |
|
555 |
1
| VariableData vd3 = new VariableData("v3", _publicMav, SymbolData.INT_TYPE, true, _d);
|
|
556 |
1
| VariableData[] toAdd2 = new VariableData[] {vd3};
|
|
557 |
1
| myVds.addLast(vd3);
|
|
558 |
| |
|
559 |
1
| assertTrue("Should be able to add new variable array", _d.addVars(toAdd2));
|
|
560 |
1
| assertEquals("Variable list should now have 3 variables", myVds, _d.getVars());
|
|
561 |
| |
|
562 |
| |
|
563 |
1
| assertTrue("Should be able to add an empty array", _d.addVars(new VariableData[0]));
|
|
564 |
1
| assertEquals("Variable list should not have changed by adding empty array", myVds, _d.getVars());
|
|
565 |
| |
|
566 |
| |
|
567 |
| } |
|
568 |
| |
|
569 |
1
| public void testGetVar() {
|
|
570 |
1
| _d = new SymbolData("woah");
|
|
571 |
1
| VariableData vd = new VariableData("v1", _publicMav, SymbolData.INT_TYPE, false, _d);
|
|
572 |
1
| VariableData vd2 = new VariableData("v2", _publicMav, SymbolData.CHAR_TYPE, true, _d);
|
|
573 |
1
| VariableData[] toAdd = new VariableData[] {vd, vd2};
|
|
574 |
1
| _d.addVars(toAdd);
|
|
575 |
| |
|
576 |
| |
|
577 |
1
| assertEquals("Should return vd", vd, _d.getVar("v1"));
|
|
578 |
| |
|
579 |
| |
|
580 |
1
| assertEquals("Should return null--no variable with that name", null, _d.getVar("whatever"));
|
|
581 |
| |
|
582 |
| } |
|
583 |
| |
|
584 |
1
| public void testIsOuterData() {
|
|
585 |
1
| _d = new SymbolData("asdf");
|
|
586 |
1
| SymbolData d2 = new SymbolData("qwer");
|
|
587 |
1
| SymbolData d246 = new SymbolData("fdsa");
|
|
588 |
1
| d2.setOuterData(_d);
|
|
589 |
1
| _d.setOuterData(d246);
|
|
590 |
1
| assertTrue("d246 should be outer data of d2", d2.isOuterData(d246));
|
|
591 |
1
| assertTrue("d246 should be outer data of _d", _d.isOuterData(d246));
|
|
592 |
1
| assertFalse("d2 should not be outer data of d246", d246.isOuterData(d2));
|
|
593 |
| |
|
594 |
| } |
|
595 |
| |
|
596 |
1
| public void testGetInnerClassOrInterface() {
|
|
597 |
1
| SymbolData sd1 = new SymbolData("testing");
|
|
598 |
1
| SymbolData sd2 = new SymbolData("testing$test123");
|
|
599 |
1
| SymbolData sd3 = new SymbolData("testing$test123$test1234");
|
|
600 |
1
| sd1.addInnerClass(sd2);
|
|
601 |
1
| sd2.addInnerClass(sd3);
|
|
602 |
| |
|
603 |
| |
|
604 |
1
| SymbolData result = sd1.getInnerClassOrInterface("test123");
|
|
605 |
1
| assertEquals("The correct inner SymbolData should be returned", sd2, result);
|
|
606 |
| |
|
607 |
| |
|
608 |
1
| result = sd2.getInnerClassOrInterface("test1234");
|
|
609 |
1
| assertEquals("The correct nested inner SymbolData should be returned", sd3, result);
|
|
610 |
| |
|
611 |
| |
|
612 |
1
| result = sd1.getInnerClassOrInterface("test123.test1234");
|
|
613 |
1
| assertEquals("The correct nested inner SymbolData should be returned", sd3, result);
|
|
614 |
| |
|
615 |
| |
|
616 |
1
| result = sd1.getInnerClassOrInterface("test123$test1234");
|
|
617 |
1
| assertEquals("The correct nested inner SymbolData should be returned", sd3, result);
|
|
618 |
| |
|
619 |
| |
|
620 |
1
| result = sd1.getInnerClassOrInterface("testing.notYourInnerClass");
|
|
621 |
1
| assertEquals("null should be returned", null, result);
|
|
622 |
| |
|
623 |
1
| SymbolData sd4 = new SymbolData("testing");
|
|
624 |
1
| SymbolData sd5 = new SymbolData("testing$test123");
|
|
625 |
1
| sd5.setInterface(true);
|
|
626 |
1
| SymbolData sd6 = new SymbolData("testing$test123$2test1234");
|
|
627 |
1
| sd4.addInnerInterface(sd5);
|
|
628 |
1
| sd5.addInnerClass(sd6);
|
|
629 |
| |
|
630 |
| |
|
631 |
1
| result = sd4.getInnerClassOrInterface("test123");
|
|
632 |
1
| assertEquals("The correct inner SymbolData should be returned", sd5, result);
|
|
633 |
| |
|
634 |
| |
|
635 |
1
| result = sd5.getInnerClassOrInterface("test1234");
|
|
636 |
1
| assertEquals("The correct nested inner SymbolData should be returned", sd6, result);
|
|
637 |
| |
|
638 |
| |
|
639 |
1
| result = sd4.getInnerClassOrInterface("test123.test1234");
|
|
640 |
1
| assertEquals("The correct nested inner SymbolData should be returned", sd6, result);
|
|
641 |
| |
|
642 |
| |
|
643 |
1
| result = sd4.getInnerClassOrInterface("testing.notYourInnerClass");
|
|
644 |
1
| assertEquals("null should be returned", null, result);
|
|
645 |
| |
|
646 |
| |
|
647 |
1
| SymbolData sd7 = new SymbolData("test123.myMethod$bob");
|
|
648 |
1
| sd7.setIsContinuation(false);
|
|
649 |
1
| MethodData md = new MethodData("myMethod", _publicMav, new TypeParameter[0],
|
|
650 |
| SymbolData.INT_TYPE, new VariableData[0], new String[0], sd1, |
|
651 |
| new NullLiteral(SourceInfo.NONE)); |
|
652 |
1
| md.addInnerClass(sd7);
|
|
653 |
1
| assertEquals("Should return sd7", sd7, md.getInnerClassOrInterface("bob"));
|
|
654 |
| |
|
655 |
| |
|
656 |
1
| SymbolData interfaceInner = new SymbolData("MyInterface$MyInner");
|
|
657 |
1
| interfaceInner.setIsContinuation(false);
|
|
658 |
1
| interfaceInner.setInterface(true);
|
|
659 |
| |
|
660 |
1
| SymbolData superInner = new SymbolData("MySuper$MyInner");
|
|
661 |
1
| superInner.setIsContinuation(false);
|
|
662 |
| |
|
663 |
1
| SymbolData myInterface = new SymbolData("MyInterface");
|
|
664 |
1
| myInterface.setInterface(true);
|
|
665 |
1
| myInterface.setIsContinuation(false);
|
|
666 |
1
| myInterface.addInnerClass(interfaceInner);
|
|
667 |
1
| interfaceInner.setOuterData(myInterface);
|
|
668 |
| |
|
669 |
1
| SymbolData mySuper = new SymbolData("MySuper");
|
|
670 |
1
| mySuper.addInnerClass(superInner);
|
|
671 |
1
| superInner.setOuterData(mySuper);
|
|
672 |
| |
|
673 |
1
| SymbolData me = new SymbolData("Me");
|
|
674 |
1
| me.setSuperClass(mySuper);
|
|
675 |
1
| me.addInterface(myInterface);
|
|
676 |
| |
|
677 |
1
| assertEquals("Should return SymbolData.AMBIGUOUS_REFERENCE", SymbolData.AMBIGUOUS_REFERENCE,
|
|
678 |
| me.getInnerClassOrInterface("MyInner")); |
|
679 |
| |
|
680 |
| |
|
681 |
1
| superInner.setMav(_privateMav);
|
|
682 |
1
| assertEquals("Should return interfaceInner", interfaceInner, me.getInnerClassOrInterface("MyInner"));
|
|
683 |
| |
|
684 |
| |
|
685 |
1
| interfaceInner.setMav(_privateMav);
|
|
686 |
1
| assertEquals("Should return interfaceInner", interfaceInner, me.getInnerClassOrInterface("MyInner"));
|
|
687 |
| |
|
688 |
| |
|
689 |
1
| interfaceInner.setMav(_publicMav);
|
|
690 |
1
| SymbolData innerInterfaceInner = new SymbolData("MyInterface$MyInner$Inner");
|
|
691 |
1
| innerInterfaceInner.setMav(_privateMav);
|
|
692 |
1
| interfaceInner.addInnerClass(innerInterfaceInner);
|
|
693 |
1
| innerInterfaceInner.setOuterData(interfaceInner);
|
|
694 |
1
| assertEquals("Should return innerInterfaceInner", innerInterfaceInner,
|
|
695 |
| me.getInnerClassOrInterface("MyInner.Inner")); |
|
696 |
| |
|
697 |
| |
|
698 |
| } |
|
699 |
| |
|
700 |
1
| public void testCreateUniqueName() {
|
|
701 |
| |
|
702 |
1
| MethodData md = new MethodData("foozle", new VariableData[0]);
|
|
703 |
1
| md.addVars(md.getParams());
|
|
704 |
1
| md.setOuterData(new SymbolData("Fooz"));
|
|
705 |
1
| String result = md.createUniqueName("avar");
|
|
706 |
1
| assertEquals("the result is correct", "avar", result);
|
|
707 |
| |
|
708 |
| |
|
709 |
1
| VariableData vd = new VariableData("avar", _publicMav, SymbolData.INT_TYPE, true, null);
|
|
710 |
1
| md = new MethodData("foozleWithAvar", new VariableData[] {vd});
|
|
711 |
1
| vd.setEnclosingData(md);
|
|
712 |
1
| md.addVars(md.getParams());
|
|
713 |
1
| md.setOuterData(new SymbolData("Fooz"));
|
|
714 |
1
| result = md.createUniqueName("avar");
|
|
715 |
1
| assertEquals("the result is correct", "avar0", result);
|
|
716 |
| |
|
717 |
| |
|
718 |
1
| SymbolData sd = new SymbolData("RandomClass");
|
|
719 |
1
| VariableData vd0 = new VariableData("avar0", _publicMav, SymbolData.DOUBLE_TYPE, true, sd);
|
|
720 |
1
| vd.setEnclosingData(sd);
|
|
721 |
1
| sd.addVars(new VariableData[] {vd, vd0});
|
|
722 |
1
| result = sd.createUniqueName("avar");
|
|
723 |
1
| assertEquals("the result is correct", "avar1", result);
|
|
724 |
| |
|
725 |
| |
|
726 |
1
| sd = new SymbolData("RandomClass");
|
|
727 |
1
| sd.setMav(_staticMav);
|
|
728 |
1
| SymbolData sd2 = new SymbolData("IAteRandomClass");
|
|
729 |
1
| sd2.addInnerClass(sd);
|
|
730 |
1
| sd.setOuterData(sd2);
|
|
731 |
1
| sd.addVar(vd);
|
|
732 |
1
| result = sd.createUniqueName("avar");
|
|
733 |
1
| assertEquals("the result is correct", "avar0", result);
|
|
734 |
| |
|
735 |
| |
|
736 |
1
| SymbolData sd3 = new SymbolData("RandomsMama");
|
|
737 |
1
| sd.setSuperClass(sd3);
|
|
738 |
1
| sd3.addVar(vd0);
|
|
739 |
1
| result = sd.createUniqueName("avar");
|
|
740 |
1
| assertEquals("the result is correct", "avar1", result);
|
|
741 |
| |
|
742 |
| } |
|
743 |
| |
|
744 |
1
| public void testGetNextAnonymousInnerClass() {
|
|
745 |
1
| SymbolData sd1 = new SymbolData("silly");
|
|
746 |
1
| sd1.setIsContinuation(false);
|
|
747 |
| |
|
748 |
1
| _d = new BlockData(sd1);
|
|
749 |
| |
|
750 |
1
| SymbolData anon1 = new SymbolData("silly$1");
|
|
751 |
1
| anon1.setIsContinuation(false);
|
|
752 |
1
| SymbolData anon2 = new SymbolData("silly$2");
|
|
753 |
1
| anon2.setIsContinuation(false);
|
|
754 |
1
| sd1.addInnerClass(anon1);
|
|
755 |
1
| anon1.setOuterData(sd1);
|
|
756 |
1
| _d.addInnerClass(anon2);
|
|
757 |
1
| anon2.setOuterData(_d);
|
|
758 |
| |
|
759 |
1
| assertEquals("Should return anon1", anon1, sd1.getNextAnonymousInnerClass());
|
|
760 |
1
| assertEquals("Should return anon2", anon2, _d.getNextAnonymousInnerClass());
|
|
761 |
1
| assertEquals("Should return null", null, _d.getNextAnonymousInnerClass());
|
|
762 |
1
| assertEquals("Should return null", null, sd1.getNextAnonymousInnerClass());
|
|
763 |
| |
|
764 |
| |
|
765 |
| } |
|
766 |
| } |
|
767 |
| } |