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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package edu.caltech.cs2.helpers;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class Reflection {
public static <T> T getFieldValue(Class clazz, String name, Object o) {
T result = null;
try {
Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
return (T) field.get(o);
} catch (NoSuchFieldException | IllegalAccessException e) {
fail("Could not find field " + name + " in class " + clazz.getName());
return null;
}
}
public static Method getMethod(Class clazz, String name, Class<?>... params) {
Method method = null;
try {
method = clazz.getDeclaredMethod(name, params);
method.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
fail("Could not find method " + name + " in class " + clazz.getName());
return null;
}
return method;
}
public static Constructor getConstructor(Class clazz, Class<?>... params) {
Constructor c = null;
try {
c = clazz.getDeclaredConstructor(params);
c.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
fail("Could not find constructor " + clazz.getName() + "("
+ String.join(", ", (String[]) Stream.of(params).map(x -> x.getName()).collect(Collectors.toList()).toArray())
+ ")" + " in class " + clazz.getName());
return null;
}
return c;
}
public static <T> T invoke(Method m, Object... args) {
T result = null;
try {
result = (T) m.invoke(args[0], Arrays.copyOfRange(args, 1, args.length));
} catch (IllegalAccessException | InvocationTargetException e) {
fail(e.getCause());
}
return result;
}
public static <T> T invokeStatic(Method m, Object... args) {
T result = null;
try {
result = (T) m.invoke(null, args);
} catch (IllegalAccessException | InvocationTargetException e) {
fail(e.getCause());
}
return result;
}
public static <T> T newInstance(Constructor c, Object... args) {
T result = null;
try {
result = (T) c.newInstance(args);
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
fail(e.getCause());
}
return result;
}
public static Stream<Field> getFields(Class clazz) {
return Stream.of(clazz.getDeclaredFields());
}
private static int stringToIntModifier(String modifier) {
switch (modifier.toLowerCase()) {
case "private":
return Modifier.PRIVATE;
case "public":
return Modifier.PUBLIC;
case "protected":
return Modifier.PROTECTED;
case "static":
return Modifier.STATIC;
case "final":
return Modifier.FINAL;
default:
fail("Unknown modifier test.");
}
/* Should never reach here... */
return -1;
}
public static Predicate<Member> hasModifier(String modifier) {
return (Member f) -> (f.getModifiers() & stringToIntModifier(modifier)) != 0;
}
public static Predicate<Member> doesNotHaveModifier(String modifier) {
return (Member f) -> (f.getModifiers() & stringToIntModifier(modifier)) == 0;
}
public static Predicate<Field> hasType(Class clazz) {
return (Field f) -> f.getType().equals(clazz);
}
public static Predicate<Field> doesNotHaveType(Class clazz) {
return (Field f) -> !f.getType().equals(clazz);
}
public static void assertFieldsLessThan(Class clazz, Class FieldType, int x) {
assertFieldsLessThan(clazz, null, FieldType, x);
}
public static void assertFieldsLessThan(Class clazz, String modifier, int x) {
assertFieldsLessThan(clazz, modifier, null, x);
}
public static void assertFieldsLessThan(Class clazz, Stream<Field> fields, int x) {
assertFieldsLessThan(clazz, fields, null, null, x);
}
public static void assertFieldsLessThan(Class clazz, String modifier, Class FieldType, int x) {
assertFieldsLessThan(clazz, getFields(clazz), modifier, FieldType, x);
}
public static void assertFieldsLessThan(Class clazz, Stream<Field> fields, String modifier, Class FieldType, int x) {
if (modifier != null) {
fields = fields.filter(hasModifier(modifier)).filter(doesNotHaveModifier("static"));
}
if (FieldType != null) {
fields = fields.filter(hasType(FieldType));
}
if (fields.count() >= x) {
fail(clazz.getName() + " has too many fields" + (modifier != null ? " with modifier " + modifier : "") + ""
+ (FieldType != null ? " of type " + FieldType.getName() : ""));
}
}
public static void assertFieldsGreaterThan(Class clazz, Class FieldType, int x) {
assertFieldsGreaterThan(clazz, null, FieldType, x);
}
public static void assertFieldsGreaterThan(Class clazz, String modifier, int x) {
assertFieldsGreaterThan(clazz, modifier, null, x);
}
public static void assertFieldsGreaterThan(Class clazz, Stream<Field> fields, int x) {
assertFieldsGreaterThan(clazz, fields, null, null, x);
}
public static void assertFieldsGreaterThan(Class clazz, String modifier, Class FieldType, int x) {
assertFieldsGreaterThan(clazz, getFields(clazz), modifier, FieldType, x);
}
public static void assertFieldsGreaterThan(Class clazz, Stream<Field> fields, String modifier, Class FieldType,
int x) {
if (modifier != null) {
fields = fields.filter(hasModifier(modifier));
}
if (FieldType != null) {
fields = fields.filter(hasType(FieldType));
}
if (fields.count() <= x) {
fail(clazz.getName() + " has too few fields" + (modifier != null ? " with modifier " + modifier : "") + " "
+ (FieldType != null ? " of type " + FieldType.getName() : ""));
}
}
public static void assertFieldsEqualTo(Class clazz, Class FieldType, int x) {
assertFieldsEqualTo(clazz, null, FieldType, x);
}
public static void assertFieldsEqualTo(Class clazz, String modifier, int x) {
assertFieldsEqualTo(clazz, modifier, null, x);
}
public static void assertFieldsEqualTo(Class clazz, Stream<Field> fields, int x) {
assertFieldsEqualTo(clazz, fields, null, null, x);
}
public static void assertFieldsEqualTo(Class clazz, String modifier, Class FieldType, int x) {
assertFieldsEqualTo(clazz, getFields(clazz), modifier, FieldType, x);
}
public static void assertFieldsEqualTo(Class clazz, Stream<Field> fields, String modifier, Class FieldType, int x) {
if (modifier != null) {
fields = fields.filter(hasModifier(modifier));
}
if (FieldType != null) {
fields = fields.filter(hasType(FieldType));
}
if (fields.count() != x) {
fail(clazz.getName() + " has the wrong number of fields" + (modifier != null ? " with modifier " + modifier : "")
+ " " + (FieldType != null ? " of type " + FieldType.getName() : ""));
}
}
public static void assertNoPublicFields(Class clazz) {
assertFieldsEqualTo(clazz, getFields(clazz).filter(doesNotHaveModifier("static")), "public", null, 0);
}
public static void assertNoProtectedFields(Class clazz) {
assertFieldsEqualTo(clazz, getFields(clazz).filter(doesNotHaveModifier("static")), "protected", null, 0);
}
public static void assertFieldModifiers(Class clazz) {
List<Field> fields = getFields(clazz).collect(Collectors.toList());
for (Field f : fields) {
int m = f.getModifiers();
assertTrue(Modifier.isPrivate(m) || Modifier.isProtected(m) || Modifier.isPublic(m),
"Field \"" + f.getName() + "\" does not have one of {public, private, protected}");
}
}
public static Field getFieldByName(Class clazz, String name) {
try {
return clazz.getDeclaredField(name);
} catch (NoSuchFieldException e) {
fail(clazz.getName() + " should have a field named '" + name + "', but does not.");
// Should not reach here!
return null;
}
}
public static Field getNonStaticFieldByType(Class clazz, Class FieldType) {
Stream<Field> fields = getFields(clazz).filter(hasType(FieldType)).filter(doesNotHaveModifier("static"));
List<Field> fieldsList = fields.collect(Collectors.toList());
if (fieldsList.isEmpty()) {
fail(clazz.getName() + " should have a field with the type '" + FieldType.getName() + "', but does not.");
// Should not reach here!
return null;
}
if (fieldsList.size() > 1) {
fail(clazz.getName() + " should only have one field with the type '" + FieldType.getName() + "', but has more.");
// Should not reach here
return null;
}
return fieldsList.get(0);
}
public static Field getFieldByType(Class clazz, Class FieldType) {
Stream<Field> fields = getFields(clazz).filter(hasType(FieldType));
List<Field> fieldsList = fields.collect(Collectors.toList());
if (fieldsList.isEmpty()) {
fail(clazz.getName() + " should have a field with the type '" + FieldType.getName() + "', but does not.");
// Should not reach here!
return null;
}
if (fieldsList.size() > 1) {
fail(clazz.getName() + " should only have one field with the type '" + FieldType.getName() + "', but has more.");
// Should not reach here
return null;
}
return fieldsList.get(0);
}
public static Field getFieldByModifiers(Class clazz, String modifier) {
return getFieldByModifiers(clazz, List.of(modifier));
}
public static Field getFieldByModifiers(Class clazz, List<String> modifiers) {
Stream<Field> fields = getFields(clazz);
for (String m : modifiers) {
fields = fields.filter(hasModifier(m));
}
List<Field> fieldsList = fields.collect(Collectors.toList());
if (fieldsList.isEmpty()) {
fail(clazz.getName() + " should have a field with the modifiers '" + String.join(", ", modifiers)
+ "', but does not.");
// Should not reach here!
return null;
}
if (fieldsList.size() > 1) {
fail(clazz.getName() + " should only have one field with the modifiers '" + String.join(", ", modifiers)
+ "', but has more.");
// Should not reach here
return null;
}
return fieldsList.get(0);
}
public static void checkFieldModifiers(Field f, String modifier) {
checkFieldModifiers(f, List.of(modifier));
}
public static void checkFieldModifiers(Field f, List<String> modifiers) {
if (!modifiers.stream().allMatch(m -> hasModifier(m).test(f))) {
fail(f.getName() + " is missing at least one of the modifiers: " + String.join(", ", modifiers));
}
}
public static void assertPublicInterface(Class clazz, List<String> methods) {
SortedSet<String> expected = new TreeSet<>(methods);
SortedSet<String> actual = new TreeSet<>(Stream.of(clazz.getDeclaredMethods()).filter(hasModifier("public"))
.map(x -> x.getName()).collect(Collectors.toList()));
if (!expected.equals(actual)) {
String diff = "expected: " + expected + "\nactual: " + actual;
fail("The public interface of " + clazz.getName() + " has incorrect functionality.\n" + diff);
}
}
public static void assertMethodCorrectlyOverridden(Class clazz, String method, Class<?>... params) {
Method studentc = getMethod(clazz, method, params);
Method superc = getMethod(clazz.getSuperclass(), method, params);
if (!studentc.getReturnType().equals(superc.getReturnType())) {
fail("You should be overriding the " + method + "method, but your signature wasn't correct.");
}
}
}