|
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.util.Log; |
|
41 |
| import java.util.*; |
|
42 |
| import junit.framework.TestCase; |
|
43 |
| import edu.rice.cs.javalanglevels.parser.JExprParser; |
|
44 |
| |
|
45 |
| |
|
46 |
| public class MethodData extends BodyData { |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| private TypeParameter[] _typeParameters; |
|
52 |
| |
|
53 |
| |
|
54 |
| private SymbolData _returnType; |
|
55 |
| |
|
56 |
| |
|
57 |
| private VariableData[] _params; |
|
58 |
| |
|
59 |
| |
|
60 |
| private String[] _thrown; |
|
61 |
| |
|
62 |
| |
|
63 |
| private JExpression _jexpr; |
|
64 |
| |
|
65 |
| |
|
66 |
| private boolean _generated; |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
803848
| public MethodData(String name, ModifiersAndVisibility modifiersAndVisibility, TypeParameter[] typeParameters,
|
|
79 |
| SymbolData returnType, VariableData[] params, String[] thrown, SymbolData enclosingClass, |
|
80 |
| JExpression jexpr) { |
|
81 |
803848
| super(enclosingClass);
|
|
82 |
803848
| _name = name;
|
|
83 |
803848
| _modifiersAndVisibility = modifiersAndVisibility;
|
|
84 |
803848
| _typeParameters = typeParameters;
|
|
85 |
803848
| _returnType = returnType;
|
|
86 |
803848
| _params = params;
|
|
87 |
803848
| _thrown = thrown;
|
|
88 |
803848
| _jexpr = jexpr;
|
|
89 |
803848
| _generated = false;
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
14
| public MethodData(String name, VariableData[] params) {
|
|
94 |
14
| this(name, new ModifiersAndVisibility(SourceInfo.NONE, new String[0]), new TypeParameter[0], null,
|
|
95 |
| params, new String[0], null, new NullLiteral(SourceInfo.NONE)); |
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
802958
| public static MethodData make(String name, ModifiersAndVisibility modifiersAndVisibility, TypeParameter[] typeParameters,
|
|
100 |
| SymbolData returnType, VariableData[] params, String[] thrown, SymbolData enclosingClass, |
|
101 |
| JExpression jexpr) { |
|
102 |
| |
|
103 |
802958
| MethodData md =
|
|
104 |
| new MethodData(name, modifiersAndVisibility, typeParameters, returnType, params, thrown, enclosingClass, jexpr); |
|
105 |
| |
|
106 |
802958
| return md;
|
|
107 |
| } |
|
108 |
| |
|
109 |
0
| public static MethodData make(String name, VariableData[] params) { return new MethodData(name, params); }
|
|
110 |
| |
|
111 |
| |
|
112 |
398
| public boolean isGenerated() { return _generated; }
|
|
113 |
| |
|
114 |
| |
|
115 |
0
| public boolean isStatic() { return hasModifier("static"); }
|
|
116 |
| |
|
117 |
| |
|
118 |
591
| public void setGenerated(boolean generated) { _generated = generated; }
|
|
119 |
| |
|
120 |
| |
|
121 |
5359
| public boolean equals(Object obj) {
|
|
122 |
8
| if (obj == this) return true;
|
|
123 |
1
| if (obj == null) return false;
|
|
124 |
10
| if ((obj.getClass() != this.getClass())) return false;
|
|
125 |
5340
| MethodData md = (MethodData) obj;
|
|
126 |
| |
|
127 |
5340
| return _name.equals(md.getName()) &&
|
|
128 |
| _modifiersAndVisibility.equals(md.getMav()) && |
|
129 |
| |
|
130 |
| |
|
131 |
| LanguageLevelVisitor.arrayEquals(_params, md.getParams()) && |
|
132 |
| LanguageLevelVisitor.arrayEquals(_thrown, md.getThrown()) && |
|
133 |
| _enclosingData.get(0) == md.getEnclosingData().get(0) && |
|
134 |
| _vars.equals(md.getVars()); |
|
135 |
| } |
|
136 |
| |
|
137 |
| |
|
138 |
0
| public int hashCode() { return getName().hashCode() ^ getEnclosingData().hashCode(); }
|
|
139 |
| |
|
140 |
0
| public boolean isMethodData() { return true; }
|
|
141 |
| |
|
142 |
201
| public MethodData getMethodData() { return this; }
|
|
143 |
| |
|
144 |
| |
|
145 |
2
| public TypeParameter[] getTypeParameters() { return _typeParameters; }
|
|
146 |
| |
|
147 |
| |
|
148 |
918
| public SymbolData getReturnType() { return _returnType; }
|
|
149 |
| |
|
150 |
| |
|
151 |
14
| public void setReturnType(SymbolData rt) { _returnType = rt; }
|
|
152 |
| |
|
153 |
| |
|
154 |
3823
| public VariableData[] getParams() { return _params; }
|
|
155 |
| |
|
156 |
| |
|
157 |
324
| public void setParams(VariableData[] p) { _params = p; }
|
|
158 |
| |
|
159 |
| |
|
160 |
494
| public String[] getThrown() { return _thrown; }
|
|
161 |
| |
|
162 |
| |
|
163 |
22
| public void setThrown(String[] thrown) { _thrown = thrown; }
|
|
164 |
| |
|
165 |
| |
|
166 |
2107
| public ModifiersAndVisibility getMav() { return _modifiersAndVisibility; }
|
|
167 |
| |
|
168 |
| |
|
169 |
0
| public void addPublicMav() {
|
|
170 |
0
| String[] oldMav = _modifiersAndVisibility.getModifiers();
|
|
171 |
0
| String[] modifiers = new String[oldMav.length + 1];
|
|
172 |
0
| modifiers[0] = "public";
|
|
173 |
0
| for (int i = 0; i < oldMav.length; i++) { modifiers[i+1] = oldMav[i];
|
|
174 |
| } |
|
175 |
0
| _modifiersAndVisibility = new ModifiersAndVisibility(_modifiersAndVisibility.getSourceInfo(), modifiers);
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
9
| public JExpression getJExpression() { return _jexpr; }
|
|
181 |
| |
|
182 |
0
| public String toString() { return "method: " + _name; }
|
|
183 |
| |
|
184 |
0
| public String toBigString() {
|
|
185 |
0
| return "method " + _name + "<" + _modifiersAndVisibility + ", " +
|
|
186 |
| Arrays.toString(_typeParameters) + ", " + _returnType + ", " + Arrays.toString(_params) + ", " + |
|
187 |
| Arrays.toString(_thrown) + ", " + _jexpr + ", " + _generated + ">" ; } |
|
188 |
| |
|
189 |
| |
|
190 |
| public static class MethodDataTest extends TestCase { |
|
191 |
| |
|
192 |
| private MethodData _md; |
|
193 |
| private MethodData _md2; |
|
194 |
| |
|
195 |
| private ModifiersAndVisibility _publicMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"}); |
|
196 |
| private ModifiersAndVisibility _publicMav2 = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"public"}); |
|
197 |
| private ModifiersAndVisibility _protectedMav = |
|
198 |
| new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"protected"}); |
|
199 |
| private ModifiersAndVisibility _finalMav = new ModifiersAndVisibility(SourceInfo.NONE, new String[] {"final"}); |
|
200 |
| |
|
201 |
0
| public MethodDataTest() { this(""); }
|
|
202 |
| |
|
203 |
1
| public MethodDataTest(String name) { super(name); }
|
|
204 |
| |
|
205 |
1
| public void testEquals() {
|
|
206 |
1
| VariableData vd = new VariableData("v", _publicMav, SymbolData.INT_TYPE, true, _md);
|
|
207 |
1
| VariableData vd2 = new VariableData("v2", _protectedMav, SymbolData.BOOLEAN_TYPE, true, _md);
|
|
208 |
1
| TypeParameter[] tp = new TypeParameter[0];
|
|
209 |
| |
|
210 |
1
| Type t = new PrimitiveType(SourceInfo.NONE, "int");
|
|
211 |
1
| Word name = new Word(SourceInfo.NONE, "m");
|
|
212 |
1
| Word paramName = new Word(SourceInfo.NONE, "i");
|
|
213 |
1
| FormalParameter fp =
|
|
214 |
| new FormalParameter(SourceInfo.NONE, new UninitializedVariableDeclarator(SourceInfo.NONE, t, paramName), |
|
215 |
| false); |
|
216 |
1
| MethodDef mdef =
|
|
217 |
| new AbstractMethodDef(SourceInfo.NONE, _publicMav, tp, t, name, new FormalParameter[] {fp}, |
|
218 |
| new ReferenceType[0]); |
|
219 |
1
| _md = new MethodData("m", _publicMav, tp, SymbolData.INT_TYPE, new VariableData[] {vd},
|
|
220 |
| new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef); |
|
221 |
| |
|
222 |
| |
|
223 |
1
| _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
|
|
224 |
| new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef); |
|
225 |
1
| assertTrue("Two MethodDatas with same fields should be equal", _md.equals(_md2));
|
|
226 |
| |
|
227 |
| |
|
228 |
1
| _md2 = new MethodData("m", _publicMav2, tp, SymbolData.DOUBLE_TYPE, new VariableData[]{vd},
|
|
229 |
| new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef); |
|
230 |
1
| assertTrue("Two MethodDatas with same fields but different return types should be equal", _md.equals(_md2));
|
|
231 |
| |
|
232 |
| |
|
233 |
1
| _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
|
|
234 |
| new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, null); |
|
235 |
1
| assertTrue("Two MethodDatas with same fields but different method defs should be equal", _md.equals(_md2));
|
|
236 |
| |
|
237 |
| |
|
238 |
1
| _md2 = null;
|
|
239 |
1
| assertFalse("A MethodData is never equal to null", _md.equals(_md2));
|
|
240 |
| |
|
241 |
| |
|
242 |
1
| assertFalse("A MethodData is never equal to another class", _md.equals(new Integer(5)));
|
|
243 |
| |
|
244 |
| |
|
245 |
1
| _md2 = new MethodData("q", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
|
|
246 |
| new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef); |
|
247 |
1
| assertFalse("Two MethodDatas with different names are not equal", _md.equals(_md2));
|
|
248 |
| |
|
249 |
| |
|
250 |
1
| _md2 = new MethodData("m", _finalMav, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
|
|
251 |
| new String[] {"I throw this"}, SymbolData.BOOLEAN_TYPE, mdef); |
|
252 |
1
| assertFalse("Two MethodDatas with different MAVs are not equal", _md.equals(_md2));
|
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
| |
|
265 |
1
| _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
|
|
266 |
| new String[] {"I throw this", "maybe this too"}, SymbolData.BOOLEAN_TYPE, mdef); |
|
267 |
1
| assertFalse("Two MethodDatas with different thrown arrays are not equal", _md.equals(_md2));
|
|
268 |
| |
|
269 |
| |
|
270 |
1
| _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd},
|
|
271 |
| new String[] {"I throw this"}, SymbolData.NULL_TYPE, mdef); |
|
272 |
1
| assertFalse("Two MethodDatas with different enclosing datas are not equal", _md.equals(_md2));
|
|
273 |
| |
|
274 |
| |
|
275 |
1
| _md2 = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd, vd2},
|
|
276 |
| new String[] {"I throw this"}, SymbolData.NULL_TYPE, mdef); |
|
277 |
1
| assertFalse("Two MethodDatas with different variables are not equal", _md.equals(_md2));
|
|
278 |
| |
|
279 |
| |
|
280 |
1
| _md = new MethodData("m", _publicMav2, tp, SymbolData.INT_TYPE, new VariableData[]{vd2, vd},
|
|
281 |
| new String[] {"I throw this"}, SymbolData.NULL_TYPE, mdef); |
|
282 |
1
| assertFalse("Two MethodDatas with same parameters in different order are not equal", _md.equals(_md2));
|
|
283 |
| |
|
284 |
| } |
|
285 |
| } |
|
286 |
| } |