|
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.javalanglevels.util.*; |
|
42 |
| import java.util.*; |
|
43 |
| import junit.framework.TestCase; |
|
44 |
| |
|
45 |
| |
|
46 |
| public class VariableData { |
|
47 |
| |
|
48 |
| |
|
49 |
| private String _name; |
|
50 |
| |
|
51 |
| |
|
52 |
| private ModifiersAndVisibility _modifiersAndVisibility; |
|
53 |
| |
|
54 |
| |
|
55 |
| private SymbolData _type; |
|
56 |
| |
|
57 |
| |
|
58 |
| private boolean _hasBeenAssigned; |
|
59 |
| |
|
60 |
| |
|
61 |
| private boolean _hasInitializer; |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| private Data _enclosingData; |
|
66 |
| |
|
67 |
| |
|
68 |
| private boolean _generated; |
|
69 |
| |
|
70 |
| |
|
71 |
| private boolean _isLocalVariable; |
|
72 |
| |
|
73 |
| |
|
74 |
| private InstanceData _instance; |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
293865
| public VariableData(String name, ModifiersAndVisibility modifiersAndVisibility, SymbolData type,
|
|
84 |
| boolean hasBeenAssigned, Data enclosingData) { |
|
85 |
293865
| _name = name;
|
|
86 |
293865
| _modifiersAndVisibility = modifiersAndVisibility;
|
|
87 |
293865
| _type = type;
|
|
88 |
293865
| _hasBeenAssigned = hasBeenAssigned;
|
|
89 |
293865
| _enclosingData = enclosingData;
|
|
90 |
293865
| _hasInitializer = false;
|
|
91 |
293865
| _generated = false;
|
|
92 |
293865
| _isLocalVariable = false;
|
|
93 |
293865
| _instance = null;
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
791928
| public VariableData(SymbolData type) {
|
|
100 |
791928
| _name = "";
|
|
101 |
791928
| _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, new String[0]);
|
|
102 |
791928
| _type = type;
|
|
103 |
791928
| _hasBeenAssigned = false;
|
|
104 |
791928
| _isLocalVariable = true;
|
|
105 |
791928
| _instance = null;
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
74
| public VariableData copyWithoutVisibility() {
|
|
112 |
74
| String[] mavStrings = _modifiersAndVisibility.getModifiers();
|
|
113 |
74
| LinkedList<String> newMavList = new LinkedList<String>();
|
|
114 |
74
| int size = 0;
|
|
115 |
74
| for (int i = 0; i < mavStrings.length; i++) {
|
|
116 |
145
| String mod = mavStrings[i];
|
|
117 |
145
| if (! isVisibility(mod)) {
|
|
118 |
69
| newMavList.add(mod);
|
|
119 |
69
| size++;
|
|
120 |
| } |
|
121 |
| } |
|
122 |
74
| String[] newMavStrings = newMavList.toArray(new String[size]);
|
|
123 |
74
| ModifiersAndVisibility newMav = new ModifiersAndVisibility(SourceInfo.NONE, newMavStrings);
|
|
124 |
74
| return new VariableData(_name, newMav, _type, true, _enclosingData);
|
|
125 |
| } |
|
126 |
| |
|
127 |
| |
|
128 |
146
| public boolean equals(Object obj) {
|
|
129 |
1
| if (obj == null) return false;
|
|
130 |
145
| else if (obj.getClass() != this.getClass()) {
|
|
131 |
| |
|
132 |
0
| return false;
|
|
133 |
| } |
|
134 |
| |
|
135 |
145
| VariableData vd = (VariableData) obj;
|
|
136 |
| |
|
137 |
145
| if (! _name.equals(vd.getName())) {
|
|
138 |
| |
|
139 |
32
| return false;
|
|
140 |
| } |
|
141 |
113
| if (! _modifiersAndVisibility.equals(vd.getMav())) {
|
|
142 |
| |
|
143 |
1
| return false;
|
|
144 |
| } |
|
145 |
| |
|
146 |
112
| if (! _type.equals(vd.getType())) {
|
|
147 |
| |
|
148 |
2
| return false;
|
|
149 |
| } |
|
150 |
| |
|
151 |
110
| if (_hasBeenAssigned != vd.hasValue()) {
|
|
152 |
| |
|
153 |
0
| return false;
|
|
154 |
| } |
|
155 |
110
| if (_hasInitializer != vd._hasInitializer) {
|
|
156 |
| |
|
157 |
0
| return false;
|
|
158 |
| } |
|
159 |
110
| Data otherEnclosingData = vd.getEnclosingData();
|
|
160 |
| |
|
161 |
110
| if (_enclosingData == null) {
|
|
162 |
7
| if (otherEnclosingData == null) return true;
|
|
163 |
| else { |
|
164 |
| |
|
165 |
0
| return false;
|
|
166 |
| } |
|
167 |
| } |
|
168 |
103
| else if (_enclosingData != otherEnclosingData) {
|
|
169 |
| |
|
170 |
0
| return false;
|
|
171 |
| } |
|
172 |
| |
|
173 |
103
| return true;
|
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
0
| public int hashCode() { return getEnclosingData().hashCode() ^ getName().hashCode(); }
|
|
178 |
| |
|
179 |
| |
|
180 |
0
| public String toString() {
|
|
181 |
0
| return "VariableData(" + _name + ", " + Arrays.toString(_modifiersAndVisibility.getModifiers()) + ", " + _type +
|
|
182 |
| ", " + _hasBeenAssigned + ")"; |
|
183 |
| } |
|
184 |
| |
|
185 |
| |
|
186 |
8752727
| public String getName() { return _name; }
|
|
187 |
| |
|
188 |
| |
|
189 |
0
| public void setName(String s) { _name = s; }
|
|
190 |
| |
|
191 |
| |
|
192 |
597
| public ModifiersAndVisibility getMav() { return _modifiersAndVisibility; }
|
|
193 |
| |
|
194 |
| |
|
195 |
23
| public void setMav(ModifiersAndVisibility mav) { _modifiersAndVisibility = mav; }
|
|
196 |
| |
|
197 |
| |
|
198 |
1589
| public SymbolData getType() { return _type;
|
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
6
| public void setType(SymbolData type) { _type = type; }
|
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
1
| public InstanceData getInstanceData() {
|
|
210 |
1
| assert _type != null;
|
|
211 |
1
| if (_instance == null) _instance = _type.getInstanceData();
|
|
212 |
1
| return _instance;
|
|
213 |
| } |
|
214 |
| |
|
215 |
| |
|
216 |
1134
| public Data getEnclosingData() { return _enclosingData; }
|
|
217 |
| |
|
218 |
| |
|
219 |
698035
| public void setEnclosingData(Data d) { _enclosingData = d; }
|
|
220 |
| |
|
221 |
| |
|
222 |
2
| public boolean isLocalVariable() { return _isLocalVariable; }
|
|
223 |
| |
|
224 |
| |
|
225 |
118
| public void setIsLocalVariable(boolean b) { _isLocalVariable = b; }
|
|
226 |
| |
|
227 |
| |
|
228 |
121
| public void setFinal() {
|
|
229 |
121
| String[] newModifiers = Utilities.catenate(_modifiersAndVisibility.getModifiers(), new String[]{"final"});
|
|
230 |
| |
|
231 |
121
| _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, newModifiers);
|
|
232 |
| } |
|
233 |
| |
|
234 |
| |
|
235 |
81
| public void setPrivate() {
|
|
236 |
81
| if (! hasModifier("private")) {
|
|
237 |
80
| String[] modifiers = _modifiersAndVisibility.getModifiers();
|
|
238 |
80
| String[] newModifiers = new String[modifiers.length + 1];
|
|
239 |
80
| newModifiers[0] = "private";
|
|
240 |
80
| for (int i = 1; i <= modifiers.length; i++) {
|
|
241 |
4
| newModifiers[i] = modifiers[i-1];
|
|
242 |
| } |
|
243 |
80
| _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, newModifiers);
|
|
244 |
| } |
|
245 |
| } |
|
246 |
| |
|
247 |
| |
|
248 |
0
| public void addModifier(String s) {
|
|
249 |
0
| if (! hasModifier(s)) {
|
|
250 |
0
| String[] modifiers = _modifiersAndVisibility.getModifiers();
|
|
251 |
0
| String[] newModifiers = new String[modifiers.length + 1];
|
|
252 |
0
| newModifiers[0] = s;
|
|
253 |
0
| for (int i = 1; i <= modifiers.length; i++) {
|
|
254 |
0
| newModifiers[i] = modifiers[i-1];
|
|
255 |
| } |
|
256 |
0
| _modifiersAndVisibility = new ModifiersAndVisibility(SourceInfo.NONE, newModifiers);
|
|
257 |
| } |
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
0
| public void setPrivateAndFinal() {
|
|
262 |
0
| setPrivate();
|
|
263 |
0
| setFinal();
|
|
264 |
| } |
|
265 |
| |
|
266 |
| |
|
267 |
0
| public void setFinalAndStatic() {
|
|
268 |
0
| setFinal();
|
|
269 |
0
| addModifier("static");
|
|
270 |
| } |
|
271 |
| |
|
272 |
| |
|
273 |
413
| public boolean isFinal() { return hasModifier("final"); }
|
|
274 |
| |
|
275 |
| |
|
276 |
95
| public boolean isPrivate() { return hasModifier("private"); }
|
|
277 |
| |
|
278 |
| |
|
279 |
93
| public boolean isStatic() { return hasModifier("static"); }
|
|
280 |
| |
|
281 |
145
| public static boolean isVisibility(String s) {
|
|
282 |
145
| return s.equals("private") || s.equals("protected") || s.equals("public");
|
|
283 |
| } |
|
284 |
| |
|
285 |
| |
|
286 |
941
| public boolean hasModifier(String modifier) {
|
|
287 |
941
| String[] mavStrings = _modifiersAndVisibility.getModifiers();
|
|
288 |
941
| for (int i = 0; i < mavStrings.length; i++) {
|
|
289 |
901
| if (mavStrings[i].equals(modifier)) {
|
|
290 |
172
| return true;
|
|
291 |
| } |
|
292 |
| } |
|
293 |
769
| return false;
|
|
294 |
| } |
|
295 |
| |
|
296 |
| |
|
297 |
15
| public void setGenerated(boolean value) { _generated = value; }
|
|
298 |
| |
|
299 |
| |
|
300 |
0
| public boolean isGenerated() { return _generated; }
|
|
301 |
| |
|
302 |
| |
|
303 |
87
| public boolean hasInitializer() { return _hasInitializer; }
|
|
304 |
| |
|
305 |
| |
|
306 |
195
| public void setHasInitializer(boolean value) { _hasInitializer = value; }
|
|
307 |
| |
|
308 |
| |
|
309 |
977
| public boolean hasValue() { return _hasBeenAssigned; }
|
|
310 |
| |
|
311 |
| |
|
312 |
0
| public void setHasValue() { _hasBeenAssigned = true; }
|
|
313 |
| |
|
314 |
| |
|
315 |
| |
|
316 |
371
| public boolean gotValue() {
|
|
317 |
245
| if (hasValue()) { return false; }
|
|
318 |
126
| _hasBeenAssigned = true;
|
|
319 |
126
| return true;
|
|
320 |
| } |
|
321 |
| |
|
322 |
| |
|
323 |
45
| public boolean lostValue() {
|
|
324 |
45
| if (hasValue()) {
|
|
325 |
43
| _hasBeenAssigned = false;
|
|
326 |
43
| return true;
|
|
327 |
| } |
|
328 |
2
| return false;
|
|
329 |
| } |
|
330 |
| |
|
331 |
| |
|
332 |
| public static class VariableDataTest extends TestCase { |
|
333 |
| |
|
334 |
| private VariableData _vd; |
|
335 |
| private VariableData _vd2; |
|
336 |
| |
|
337 |
| private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"}); |
|
338 |
| private ModifiersAndVisibility |
|
339 |
| _publicMav2 = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"}); |
|
340 |
| private ModifiersAndVisibility |
|
341 |
| _protectedMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"}); |
|
342 |
0
| public VariableDataTest() { this(""); }
|
|
343 |
1
| public VariableDataTest(String name) { super(name); }
|
|
344 |
| |
|
345 |
1
| public void testEquals() {
|
|
346 |
1
| _vd = new VariableData("v", _publicMav, SymbolData.INT_TYPE, true, null);
|
|
347 |
| |
|
348 |
| |
|
349 |
1
| _vd2 = new VariableData("v", _publicMav2, SymbolData.INT_TYPE, true, null);
|
|
350 |
1
| assertTrue("Equals should return true if two VariableDatas are equal", _vd.equals(_vd2));
|
|
351 |
1
| assertTrue("Equals should return true in opposite direction as well", _vd2.equals(_vd));
|
|
352 |
| |
|
353 |
| |
|
354 |
1
| _vd2 = null;
|
|
355 |
1
| assertFalse("Equals should return false if VariableData is compared to null",_vd.equals(null));
|
|
356 |
| |
|
357 |
| |
|
358 |
1
| _vd2 = new VariableData("q", _publicMav, SymbolData.INT_TYPE, true, null);
|
|
359 |
1
| assertFalse("Equals should return false if variable names are different", _vd.equals(_vd2));
|
|
360 |
| |
|
361 |
| |
|
362 |
1
| _vd2 = new VariableData("v", _protectedMav, SymbolData.INT_TYPE, true, null);
|
|
363 |
1
| assertFalse("Equals should return false if variable modifiers are different", _vd.equals(_vd2));
|
|
364 |
| |
|
365 |
| |
|
366 |
1
| _vd2 = new VariableData("v", _publicMav, SymbolData.BOOLEAN_TYPE, true, null);
|
|
367 |
1
| assertFalse("Equals should return false if variable types are different", _vd.equals(_vd2));
|
|
368 |
| } |
|
369 |
| } |
|
370 |
| } |