|
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 |
| package edu.rice.cs.plt.reflect; |
|
36 |
| |
|
37 |
| import java.lang.reflect.Field; |
|
38 |
| import java.lang.reflect.Method; |
|
39 |
| import java.lang.reflect.Constructor; |
|
40 |
| import java.lang.reflect.InvocationTargetException; |
|
41 |
| import java.io.File; |
|
42 |
| import java.io.Serializable; |
|
43 |
| import edu.rice.cs.plt.lambda.*; |
|
44 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
45 |
| import edu.rice.cs.plt.io.IOUtil; |
|
46 |
| |
|
47 |
| import static edu.rice.cs.plt.reflect.ReflectException.*; |
|
48 |
| import static edu.rice.cs.plt.debug.DebugUtil.debug; |
|
49 |
| |
|
50 |
| public final class ReflectUtil { |
|
51 |
| |
|
52 |
| |
|
53 |
0
| private ReflectUtil() {}
|
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| public static final ClassLoader BOOT_CLASS_LOADER = new ClassLoader(null) {}; |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| public static final Iterable<File> SYSTEM_CLASS_PATH = IOUtil.parsePath(System.getProperty("java.class.path", "")); |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
55
| public static String simpleName(Class<?> c) {
|
|
82 |
7
| if (c.isArray()) { return simpleName(c.getComponentType()) + "[]"; }
|
|
83 |
48
| else if (isAnonymousClass(c)) {
|
|
84 |
1
| if (c.getInterfaces().length > 0) { return "anonymous " + simpleName(c.getInterfaces()[0]); }
|
|
85 |
1
| else { return "anonymous " + simpleName(c.getSuperclass()); }
|
|
86 |
| } |
|
87 |
| else { |
|
88 |
46
| String fullName = c.getName();
|
|
89 |
46
| int dot = fullName.lastIndexOf('.');
|
|
90 |
46
| int dollar = fullName.lastIndexOf('$');
|
|
91 |
46
| int nameStart = (dot > dollar) ? dot+1 : dollar+1;
|
|
92 |
46
| int length = fullName.length();
|
|
93 |
1
| while (nameStart < length && Character.isDigit(fullName.charAt(nameStart))) { nameStart++; }
|
|
94 |
46
| return fullName.substring(nameStart);
|
|
95 |
| } |
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
54
| public static boolean isAnonymousClass(Class<?> c) {
|
|
100 |
54
| String name = c.getName();
|
|
101 |
54
| String nameEnd = name.substring(name.lastIndexOf('$') + 1);
|
|
102 |
54
| for (int i = 0; i < nameEnd.length(); i++) {
|
|
103 |
50
| if (Character.isJavaIdentifierStart(nameEnd.charAt(i))) { return false; }
|
|
104 |
| } |
|
105 |
4
| return true;
|
|
106 |
| } |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
0
| public static Class<?> arrayBaseClass(Class<?> c) {
|
|
113 |
0
| Class<?> result = c;
|
|
114 |
0
| while (result.isArray()) { result = result.getComponentType(); }
|
|
115 |
0
| return result;
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
0
| public static int arrayDimensions(Class<?> c) {
|
|
123 |
0
| Class<?> rest = c;
|
|
124 |
0
| int result = 0;
|
|
125 |
0
| while (rest.isArray()) { rest = rest.getComponentType(); result++; }
|
|
126 |
0
| return result;
|
|
127 |
| } |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
43
| public static <T> T cast(Class<? extends T> c, Object o) throws ClassCastException {
|
|
147 |
43
| if (box(c).isInstance(o)) {
|
|
148 |
36
| @SuppressWarnings("unchecked") T result = (T) o;
|
|
149 |
36
| return result;
|
|
150 |
| } |
|
151 |
7
| else { throw new ClassCastException("Casting to " + c.getName() + " from " + o.getClass().getName()); }
|
|
152 |
| } |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
4
| @SuppressWarnings("unchecked") public static <T> Class<? extends T> getClass(T obj) {
|
|
159 |
4
| return (Class<? extends T>) obj.getClass();
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
54
| @SuppressWarnings("unchecked") public static <T> Class<T> box(Class<T> c) {
|
|
164 |
54
| if (c.isPrimitive()) {
|
|
165 |
1
| if (c == Boolean.TYPE) { return (Class<T>) Boolean.class; }
|
|
166 |
1
| if (c == Character.TYPE) { return (Class<T>) Character.class; }
|
|
167 |
1
| if (c == Byte.TYPE) { return (Class<T>) Byte.class; }
|
|
168 |
1
| if (c == Short.TYPE) { return (Class<T>) Short.class; }
|
|
169 |
4
| if (c == Integer.TYPE) { return (Class<T>) Integer.class; }
|
|
170 |
1
| if (c == Long.TYPE) { return (Class<T>) Long.class; }
|
|
171 |
1
| if (c == Float.TYPE) { return (Class<T>) Float.class; }
|
|
172 |
1
| if (c == Double.TYPE) { return (Class<T>) Double.class; }
|
|
173 |
1
| if (c == Void.TYPE) { return (Class<T>) Void.class; }
|
|
174 |
| } |
|
175 |
42
| return c;
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
| private static final ClassLoader CURRENT_LOADER = ReflectUtil.class.getClassLoader(); |
|
180 |
| |
|
181 |
| |
|
182 |
21
| private static Class<?>[] getClasses(Object[] args) {
|
|
183 |
21
| if (args.length > 0) {
|
|
184 |
6
| Class<?>[] sig = new Class<?>[args.length];
|
|
185 |
8
| for (int i = 0; i < args.length; i++) { sig[i] = args[i].getClass(); }
|
|
186 |
6
| return sig;
|
|
187 |
| } |
|
188 |
15
| else { return null; }
|
|
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 |
7
| public static Object loadObject(String className, Object... constructorArgs) throws ReflectException {
|
|
214 |
7
| return loadObject(CURRENT_LOADER, className, getClasses(constructorArgs), constructorArgs);
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
3
| public static Object loadObject(String className, Class<?>[] constructorSig, Object... constructorArgs)
|
|
233 |
| throws ReflectException { |
|
234 |
3
| return loadObject(CURRENT_LOADER, className, constructorSig, constructorArgs);
|
|
235 |
| } |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
1
| public static Object loadObject(ClassLoader loader, String className, Object... constructorArgs)
|
|
259 |
| throws ReflectException { |
|
260 |
1
| return loadObject(loader, className, getClasses(constructorArgs), constructorArgs);
|
|
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 |
13
| public static Object loadObject(ClassLoader loader, String className, Class<?>[] constructorSig,
|
|
299 |
| Object... constructorArgs) throws ReflectException { |
|
300 |
13
| try {
|
|
301 |
13
| Class<?> c = Class.forName(className, true, loader);
|
|
302 |
12
| Constructor<?> k = c.getConstructor(constructorSig);
|
|
303 |
8
| return k.newInstance(constructorArgs);
|
|
304 |
| } |
|
305 |
1
| catch (ClassNotFoundException e) { throw new ClassNotFoundReflectException(e); }
|
|
306 |
4
| catch (NoSuchMethodException e) { throw new NoSuchMethodReflectException(e); }
|
|
307 |
2
| catch (IllegalArgumentException e) { throw new IllegalArgumentReflectException(e); }
|
|
308 |
1
| catch (InvocationTargetException e) { throw new InvocationTargetReflectException(e); }
|
|
309 |
0
| catch (InstantiationException e) { throw new InstantiationReflectException(e); }
|
|
310 |
0
| catch (IllegalAccessException e) { throw new IllegalAccessReflectException(e); }
|
|
311 |
0
| catch (SecurityException e) { throw new SecurityReflectException(e); }
|
|
312 |
| } |
|
313 |
| |
|
314 |
5
| public static Object getStaticField(String className, String fieldName) throws ReflectException {
|
|
315 |
5
| return getStaticField(CURRENT_LOADER, className, fieldName);
|
|
316 |
| } |
|
317 |
| |
|
318 |
9
| public static Object getStaticField(ClassLoader loader, String className, String fieldName) throws ReflectException {
|
|
319 |
9
| try {
|
|
320 |
9
| Class<?> c = Class.forName(className, true, loader);
|
|
321 |
7
| Field f = c.getField(fieldName);
|
|
322 |
6
| try { return f.get(null); }
|
|
323 |
| |
|
324 |
1
| catch (NullPointerException e) { throw new NullPointerReflectException(e); }
|
|
325 |
| } |
|
326 |
2
| catch (ClassNotFoundException e) { throw new ClassNotFoundReflectException(e); }
|
|
327 |
1
| catch (NoSuchFieldException e) { throw new NoSuchFieldReflectException(e); }
|
|
328 |
0
| catch (IllegalArgumentException e) { throw new IllegalArgumentReflectException(e); }
|
|
329 |
0
| catch (IllegalAccessException e) { throw new IllegalAccessReflectException(e); }
|
|
330 |
0
| catch (SecurityException e) { throw new SecurityReflectException(e); }
|
|
331 |
| } |
|
332 |
| |
|
333 |
8
| public static Object invokeStaticMethod(String className, String methodName, Object... args)
|
|
334 |
| throws ReflectException { |
|
335 |
8
| return invokeStaticMethod(CURRENT_LOADER, className, methodName, getClasses(args), args);
|
|
336 |
| } |
|
337 |
| |
|
338 |
4
| public static Object invokeStaticMethod(ClassLoader loader, String className, String methodName, Object... args)
|
|
339 |
| throws ReflectException { |
|
340 |
4
| return invokeStaticMethod(loader, className, methodName, getClasses(args), args);
|
|
341 |
| } |
|
342 |
| |
|
343 |
3
| public static Object invokeStaticMethod(String className, String methodName, Class<?>[] signature, Object... args)
|
|
344 |
| throws ReflectException { |
|
345 |
3
| return invokeStaticMethod(CURRENT_LOADER, className, methodName, signature, args);
|
|
346 |
| } |
|
347 |
| |
|
348 |
15
| public static Object invokeStaticMethod(ClassLoader loader, String className, String methodName, Class<?>[] signature,
|
|
349 |
| Object... args) throws ReflectException { |
|
350 |
15
| try {
|
|
351 |
15
| Class<?> c = Class.forName(className, true, loader);
|
|
352 |
13
| Method m = c.getMethod(methodName, signature);
|
|
353 |
10
| try { return m.invoke(null, args); }
|
|
354 |
| |
|
355 |
1
| catch (NullPointerException e) { throw new NullPointerReflectException(e); }
|
|
356 |
| } |
|
357 |
2
| catch (ClassNotFoundException e) { throw new ClassNotFoundReflectException(e); }
|
|
358 |
3
| catch (NoSuchMethodException e) { throw new NoSuchMethodReflectException(e); }
|
|
359 |
1
| catch (IllegalArgumentException e) { throw new IllegalArgumentReflectException(e); }
|
|
360 |
0
| catch (InvocationTargetException e) { throw new InvocationTargetReflectException(e); }
|
|
361 |
0
| catch (IllegalAccessException e) { throw new IllegalAccessReflectException(e); }
|
|
362 |
0
| catch (SecurityException e) { throw new SecurityReflectException(e); }
|
|
363 |
| } |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
| |
|
384 |
| |
|
385 |
0
| public static Object loadLibraryAdapter(Iterable<? extends File> libraryPath, String adapterName,
|
|
386 |
| Object... constructorArgs) throws ReflectException { |
|
387 |
0
| return loadLibraryAdapter(CURRENT_LOADER, libraryPath, adapterName, getClasses(constructorArgs), constructorArgs);
|
|
388 |
| } |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
0
| public static Object loadLibraryAdapter(Iterable<? extends File> libraryPath, String adapterName,
|
|
405 |
| Class<?>[] constructorSig,Object... constructorArgs) throws ReflectException { |
|
406 |
0
| return loadLibraryAdapter(CURRENT_LOADER, libraryPath, adapterName, constructorSig, constructorArgs);
|
|
407 |
| } |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
1
| public static Object loadLibraryAdapter(ClassLoader baseLoader, Iterable<? extends File> libraryPath,
|
|
428 |
| String adapterName, Object... constructorArgs) throws ReflectException { |
|
429 |
1
| return loadLibraryAdapter(baseLoader, libraryPath, adapterName, getClasses(constructorArgs), constructorArgs);
|
|
430 |
| } |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
1
| public static Object loadLibraryAdapter(ClassLoader baseLoader, Iterable<? extends File> libraryPath,
|
|
448 |
| String adapterName, Class<?>[] constructorSig, Object... constructorArgs) |
|
449 |
| throws ReflectException { |
|
450 |
1
| ClassLoader libraryLoader = new PathClassLoader(baseLoader, libraryPath);
|
|
451 |
1
| ClassLoader adapterLoader = new PreemptingClassLoader(libraryLoader, adapterName);
|
|
452 |
1
| return loadObject(adapterLoader, adapterName, constructorSig, constructorArgs);
|
|
453 |
| } |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
0
| public static ComposedClassLoader mergeLoaders(ClassLoader first, ClassLoader second) {
|
|
459 |
0
| return new ComposedClassLoader(first, second);
|
|
460 |
| } |
|
461 |
| |
|
462 |
| |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
0
| public static ComposedClassLoader mergeLoaders(ClassLoader first, ClassLoader second, String... firstIncludes) {
|
|
467 |
0
| return mergeLoaders(first, second, false, firstIncludes);
|
|
468 |
| } |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
0
| public static ComposedClassLoader mergeLoaders(ClassLoader first, ClassLoader second, boolean blackList,
|
|
479 |
| String... firstPrefixes) { |
|
480 |
0
| ClassLoader filteredFirst = new ShadowingClassLoader(first, blackList, IterUtil.asIterable(firstPrefixes), false);
|
|
481 |
0
| return new ComposedClassLoader(filteredFirst, second);
|
|
482 |
| } |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
6
| public static <T> Box<T> staticFieldAsBox(Class<?> c, String fieldName, Class<T> fieldType) {
|
|
491 |
6
| return new FieldBox<T>(c, fieldName, fieldType, null);
|
|
492 |
| } |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
| |
|
498 |
| |
|
499 |
| |
|
500 |
4
| public static <T> Box<T> fieldAsBox(Object object, String fieldName, Class<T> fieldType) {
|
|
501 |
4
| return new FieldBox<T>(object.getClass(), fieldName, fieldType, object);
|
|
502 |
| } |
|
503 |
| |
|
504 |
| private static final class FieldBox<T> implements Box<T>, Serializable { |
|
505 |
| private final Class<?> _objClass; |
|
506 |
| private final String _name; |
|
507 |
| private final Class<T> _type; |
|
508 |
| private final Object _obj; |
|
509 |
| private transient Field _field; |
|
510 |
| |
|
511 |
10
| public FieldBox(Class<?> objClass, String name, Class<T> type, Object obj) {
|
|
512 |
10
| _objClass = objClass;
|
|
513 |
10
| _name = name;
|
|
514 |
10
| _type = type;
|
|
515 |
10
| _obj = obj;
|
|
516 |
10
| _field = null;
|
|
517 |
| } |
|
518 |
| |
|
519 |
12
| public T value() {
|
|
520 |
12
| try {
|
|
521 |
8
| if (_field == null) { _field = _objClass.getField(_name); }
|
|
522 |
10
| try { return cast(_type, _field.get(_obj)); }
|
|
523 |
| |
|
524 |
1
| catch (NullPointerException e) { throw new WrappedException(new NullPointerReflectException(e)); }
|
|
525 |
| } |
|
526 |
2
| catch (NoSuchFieldException e) { throw new WrappedException(new NoSuchFieldReflectException(e)); }
|
|
527 |
0
| catch (IllegalArgumentException e) { throw new WrappedException(new IllegalArgumentReflectException(e)); }
|
|
528 |
0
| catch (IllegalAccessException e) { throw new WrappedException(new IllegalAccessReflectException(e)); }
|
|
529 |
2
| catch (ClassCastException e) { throw new WrappedException(new ClassCastReflectException(e)); }
|
|
530 |
0
| catch (SecurityException e) { throw new WrappedException(new SecurityReflectException(e)); }
|
|
531 |
| } |
|
532 |
| |
|
533 |
6
| public void set(T value) {
|
|
534 |
6
| try {
|
|
535 |
2
| if (_field == null) { _field = _objClass.getField(_name); }
|
|
536 |
6
| _field.set(_obj, value);
|
|
537 |
| } |
|
538 |
0
| catch (NoSuchFieldException e) { throw new WrappedException(new NoSuchFieldReflectException(e)); }
|
|
539 |
0
| catch (IllegalArgumentException e) { throw new WrappedException(new IllegalArgumentReflectException(e)); }
|
|
540 |
2
| catch (IllegalAccessException e) { throw new WrappedException(new IllegalAccessReflectException(e)); }
|
|
541 |
0
| catch (SecurityException e) { throw new WrappedException(new SecurityReflectException(e)); }
|
|
542 |
| } |
|
543 |
| } |
|
544 |
| |
|
545 |
| |
|
546 |
| |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
2
| public static <O, R> Thunk<R> staticMethodAsThunk(Class<? super O> c, String methodName, Class<? extends R> retT) {
|
|
552 |
2
| return LambdaUtil.bindFirst(new MethodLambda<O, R>(c, methodName, retT), null);
|
|
553 |
| } |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
| |
|
561 |
1
| public static <R> Thunk<R> methodAsThunk(Object object, String methodName, Class<? extends R> retT) {
|
|
562 |
1
| return new ObjectMethodThunk<R>(object, methodName, retT);
|
|
563 |
| } |
|
564 |
| |
|
565 |
| |
|
566 |
| |
|
567 |
| |
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
2
| public static <O, R> Lambda<O, R> methodAsLambda(Class<? super O> c, String methodName, Class<? extends R> retT) {
|
|
572 |
2
| return new MethodLambda<O, R>(c, methodName, retT);
|
|
573 |
| } |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
| |
|
578 |
| |
|
579 |
| |
|
580 |
2
| public static <O, T, R> Lambda<T, R> staticMethodAsLambda(Class<? super O> c, String methodName,
|
|
581 |
| Class<? super T> argT, Class<? extends R> retT) { |
|
582 |
2
| return LambdaUtil.bindFirst(new MethodLambda2<O, T, R>(c, methodName, argT, retT), null);
|
|
583 |
| } |
|
584 |
| |
|
585 |
| |
|
586 |
| |
|
587 |
| |
|
588 |
| |
|
589 |
| |
|
590 |
| |
|
591 |
3
| public static <T, R> Lambda<T, R> methodAsLambda(Object object, String methodName, Class<? super T> argT,
|
|
592 |
| Class<? extends R> retT) { |
|
593 |
3
| return new ObjectMethodLambda<T, R>(object, methodName, argT, retT);
|
|
594 |
| } |
|
595 |
| |
|
596 |
| |
|
597 |
| |
|
598 |
| |
|
599 |
| |
|
600 |
| |
|
601 |
| |
|
602 |
1
| public static <O, T, R> Lambda2<O, T, R> methodAsLambda2(Class<? super O> c, String methodName,
|
|
603 |
| Class<? super T> argT, Class<? extends R> retT) { |
|
604 |
1
| return new MethodLambda2<O, T, R>(c, methodName, argT, retT);
|
|
605 |
| } |
|
606 |
| |
|
607 |
| |
|
608 |
| |
|
609 |
| |
|
610 |
| |
|
611 |
| |
|
612 |
1
| public static <O, T1, T2, R>
|
|
613 |
| Lambda2<T1, T2, R> staticMethodAsLambda2(Class<? super O> c, String methodName, Class<? super T1> arg1T, |
|
614 |
| Class<? super T2> arg2T, Class<? extends R> retT) { |
|
615 |
1
| return LambdaUtil.bindFirst(new MethodLambda3<O, T1, T2, R>(c, methodName, arg1T, arg2T, retT), null);
|
|
616 |
| } |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
| |
|
621 |
| |
|
622 |
| |
|
623 |
| |
|
624 |
1
| public static <T1, T2, R>
|
|
625 |
| Lambda2<T1, T2, R> methodAsLambda2(Object object, String methodName, Class<? super T1> arg1T, |
|
626 |
| Class<? super T2> arg2T, Class<? extends R> retT) { |
|
627 |
1
| return new ObjectMethodLambda2<T1, T2, R>(object, methodName, arg1T, arg2T, retT);
|
|
628 |
| } |
|
629 |
| |
|
630 |
| |
|
631 |
| |
|
632 |
| |
|
633 |
| |
|
634 |
| |
|
635 |
| |
|
636 |
1
| public static <O, T1, T2, R>
|
|
637 |
| Lambda3<O, T1, T2, R> methodAsLambda3(Class<? super O> c, String methodName, Class<? super T1> arg1T, |
|
638 |
| Class<? super T2> arg2T, Class<? extends R> retT) { |
|
639 |
1
| return new MethodLambda3<O, T1, T2, R>(c, methodName, arg1T, arg2T, retT);
|
|
640 |
| } |
|
641 |
| |
|
642 |
| |
|
643 |
| |
|
644 |
| |
|
645 |
| |
|
646 |
| |
|
647 |
1
| public static <O, T1, T2, T3, R>
|
|
648 |
| Lambda3<T1, T2, T3, R> staticMethodAsLambda3(Class<? super O> c, String methodName, Class<? super T1> arg1T, |
|
649 |
| Class<? super T2> arg2T, Class<? super T3> arg3T, |
|
650 |
| Class<? extends R> retT) { |
|
651 |
1
| return LambdaUtil.bindFirst(new MethodLambda4<O, T1, T2, T3, R>(c, methodName, arg1T, arg2T, arg3T, retT), null);
|
|
652 |
| } |
|
653 |
| |
|
654 |
| |
|
655 |
| |
|
656 |
| |
|
657 |
| |
|
658 |
| |
|
659 |
| |
|
660 |
1
| public static <T1, T2, T3, R>
|
|
661 |
| Lambda3<T1, T2, T3, R> methodAsLambda3(Object object, String methodName, Class<? super T1> arg1T, |
|
662 |
| Class<? super T2> arg2T, Class<? super T3> arg3T, Class<? extends R> retT) { |
|
663 |
1
| return new ObjectMethodLambda3<T1, T2, T3, R>(object, methodName, arg1T, arg2T, arg3T, retT);
|
|
664 |
| } |
|
665 |
| |
|
666 |
| |
|
667 |
| |
|
668 |
| |
|
669 |
| |
|
670 |
| |
|
671 |
| |
|
672 |
1
| public static <O, T1, T2, T3, R>
|
|
673 |
| Lambda4<O, T1, T2, T3, R> methodAsLambda4(Class<? super O> c, String methodName, Class<? super T1> arg1T, |
|
674 |
| Class<? super T2> arg2T, Class<? super T3> arg3T, |
|
675 |
| Class<? extends R> retT) { |
|
676 |
1
| return new MethodLambda4<O, T1, T2, T3, R>(c, methodName, arg1T, arg2T, arg3T, retT);
|
|
677 |
| } |
|
678 |
| |
|
679 |
| |
|
680 |
| |
|
681 |
| private static abstract class MethodWrapper<R> implements Serializable { |
|
682 |
| private final Class<?> _objClass; |
|
683 |
| private final String _name; |
|
684 |
| private final Class<? extends R> _returnType; |
|
685 |
| private final Class<?>[] _signature; |
|
686 |
| private transient Method _method; |
|
687 |
| |
|
688 |
17
| protected MethodWrapper(Class<?> objClass, String name, Class<? extends R> returnType, Class<?>... signature) {
|
|
689 |
17
| _objClass = objClass;
|
|
690 |
17
| _name = name;
|
|
691 |
17
| _returnType = returnType;
|
|
692 |
17
| _signature = signature;
|
|
693 |
17
| _method = null;
|
|
694 |
| } |
|
695 |
| |
|
696 |
| |
|
697 |
26
| protected R invoke(Object obj, Object... args) {
|
|
698 |
26
| try {
|
|
699 |
18
| if (_method == null) { _method = _objClass.getMethod(_name, _signature); }
|
|
700 |
23
| try { return cast(_returnType, _method.invoke(obj, args)); }
|
|
701 |
| |
|
702 |
1
| catch (NullPointerException e) { throw new WrappedException(new NullPointerReflectException(e)); }
|
|
703 |
| } |
|
704 |
3
| catch (NoSuchMethodException e) { throw new WrappedException(new NoSuchMethodReflectException(e)); }
|
|
705 |
0
| catch (IllegalArgumentException e) { throw new WrappedException(new IllegalArgumentReflectException(e)); }
|
|
706 |
0
| catch (InvocationTargetException e) { throw new WrappedException(new InvocationTargetReflectException(e)); }
|
|
707 |
0
| catch (IllegalAccessException e) { throw new WrappedException(new IllegalAccessReflectException(e)); }
|
|
708 |
2
| catch (ClassCastException e) { throw new WrappedException(new ClassCastReflectException(e)); }
|
|
709 |
0
| catch (SecurityException e) { throw new WrappedException(new SecurityReflectException(e)); }
|
|
710 |
| } |
|
711 |
| } |
|
712 |
| |
|
713 |
| private static final class ObjectMethodThunk<R> extends MethodWrapper<R> implements Thunk<R> { |
|
714 |
| private final Object _obj; |
|
715 |
1
| public ObjectMethodThunk(Object obj, String name, Class<? extends R> returnT) {
|
|
716 |
1
| super(obj.getClass(), name, returnT); _obj = obj;
|
|
717 |
| } |
|
718 |
1
| public R value() { return invoke(_obj); }
|
|
719 |
| } |
|
720 |
| |
|
721 |
| private static final class MethodLambda<O, R> extends MethodWrapper<R> implements Lambda<O, R> { |
|
722 |
4
| public MethodLambda(Class<? super O> objT, String name, Class<? extends R> returnT) {
|
|
723 |
4
| super(objT, name, returnT);
|
|
724 |
| } |
|
725 |
7
| public R value(O obj) { return invoke(obj); }
|
|
726 |
| } |
|
727 |
| |
|
728 |
| private static final class ObjectMethodLambda<T, R> extends MethodWrapper<R> implements Lambda<T, R> { |
|
729 |
| private final Object _obj; |
|
730 |
3
| public ObjectMethodLambda(Object obj, String name, Class<? super T> argT, Class<? extends R> returnT) {
|
|
731 |
3
| super(obj.getClass(), name, returnT, argT); _obj = obj;
|
|
732 |
| } |
|
733 |
3
| public R value(T arg) { return invoke(_obj, arg); }
|
|
734 |
| } |
|
735 |
| |
|
736 |
| private static final class MethodLambda2<O, T, R> extends MethodWrapper<R> implements Lambda2<O, T, R> { |
|
737 |
3
| public MethodLambda2(Class<? super O> objT, String name, Class<? super T> argT, Class<? extends R> returnT) {
|
|
738 |
3
| super(objT, name, returnT, argT);
|
|
739 |
| } |
|
740 |
4
| public R value(O obj, T arg) { return invoke(obj, arg); }
|
|
741 |
| } |
|
742 |
| |
|
743 |
| private static final class ObjectMethodLambda2<T1, T2, R> extends MethodWrapper<R> implements Lambda2<T1, T2, R> { |
|
744 |
| private final Object _obj; |
|
745 |
1
| public ObjectMethodLambda2(Object obj, String name, Class<? super T1> arg1T, Class<? super T2> arg2T,
|
|
746 |
| Class<? extends R> returnT) { |
|
747 |
1
| super(obj.getClass(), name, returnT, arg1T, arg2T); _obj = obj;
|
|
748 |
| } |
|
749 |
1
| public R value(T1 arg1, T2 arg2) { return invoke(_obj, arg1, arg2); }
|
|
750 |
| } |
|
751 |
| |
|
752 |
| private static final class MethodLambda3<O, T1, T2, R> extends MethodWrapper<R> implements Lambda3<O, T1, T2, R> { |
|
753 |
2
| public MethodLambda3(Class<? super O> objT, String name, Class<? super T1> arg1T, Class<? super T2> arg2T,
|
|
754 |
| Class<? extends R> returnT) { |
|
755 |
2
| super(objT, name, returnT, arg1T, arg2T);
|
|
756 |
| } |
|
757 |
5
| public R value(O obj, T1 arg1, T2 arg2) { return invoke(obj, arg1, arg2); }
|
|
758 |
| } |
|
759 |
| |
|
760 |
| private static final class ObjectMethodLambda3<T1, T2, T3, R> extends MethodWrapper<R> |
|
761 |
| implements Lambda3<T1, T2, T3, R> { |
|
762 |
| private final Object _obj; |
|
763 |
1
| public ObjectMethodLambda3(Object obj, String name, Class<? super T1> arg1T, Class<? super T2> arg2T,
|
|
764 |
| Class<? super T3> arg3T, Class<? extends R> returnT) { |
|
765 |
1
| super(obj.getClass(), name, returnT, arg1T, arg2T, arg3T); _obj = obj;
|
|
766 |
| } |
|
767 |
1
| public R value(T1 arg1, T2 arg2, T3 arg3) { return invoke(_obj, arg1, arg2, arg3); }
|
|
768 |
| } |
|
769 |
| |
|
770 |
| private static final class MethodLambda4<O, T1, T2, T3, R> extends MethodWrapper<R> |
|
771 |
| implements Lambda4<O, T1, T2, T3, R> { |
|
772 |
2
| public MethodLambda4(Class<? super O> objT, String name, Class<? super T1> arg1T, Class<? super T2> arg2T,
|
|
773 |
| Class<? super T3> arg3T, Class<? extends R> returnT) { |
|
774 |
2
| super(objT, name, returnT, arg1T, arg2T, arg3T);
|
|
775 |
| } |
|
776 |
4
| public R value(O obj, T1 arg1, T2 arg2, T3 arg3) { return invoke(obj, arg1, arg2, arg3); }
|
|
777 |
| } |
|
778 |
| |
|
779 |
| |
|
780 |
| |
|
781 |
| |
|
782 |
| |
|
783 |
| |
|
784 |
| |
|
785 |
| |
|
786 |
4
| public static <R> Thunk<R> constructorAsThunk(Class<? extends R> c) {
|
|
787 |
4
| return new ConstructorThunk<R>(c);
|
|
788 |
| } |
|
789 |
| |
|
790 |
| |
|
791 |
| |
|
792 |
| |
|
793 |
| |
|
794 |
| |
|
795 |
| |
|
796 |
3
| public static <T, R> Lambda<T, R> constructorAsLambda(Class<? extends R> c, Class<? super T> argT) {
|
|
797 |
3
| return new ConstructorLambda<T, R>(c, argT);
|
|
798 |
| } |
|
799 |
| |
|
800 |
| |
|
801 |
| |
|
802 |
| |
|
803 |
| |
|
804 |
| |
|
805 |
| |
|
806 |
0
| public static <T1, T2, R> Lambda2<T1, T2, R> constructorAsLambda2(Class<? extends R> c, Class<? super T1> arg1T,
|
|
807 |
| Class<? super T2> arg2T) { |
|
808 |
0
| return new ConstructorLambda2<T1, T2, R>(c, arg1T, arg2T);
|
|
809 |
| } |
|
810 |
| |
|
811 |
| |
|
812 |
| |
|
813 |
| |
|
814 |
| |
|
815 |
| |
|
816 |
| |
|
817 |
1
| public static <T1, T2, T3, R> Lambda3<T1, T2, T3, R>
|
|
818 |
| constructorAsLambda3(Class<? extends R> c, Class<? super T1> arg1T, Class<? super T2> arg2T, |
|
819 |
| Class<? super T3> arg3T) { |
|
820 |
1
| return new ConstructorLambda3<T1, T2, T3, R>(c, arg1T, arg2T, arg3T);
|
|
821 |
| } |
|
822 |
| |
|
823 |
| |
|
824 |
| |
|
825 |
| |
|
826 |
| |
|
827 |
| |
|
828 |
| |
|
829 |
0
| public static <T1, T2, T3, T4, R> Lambda4<T1, T2, T3, T4, R>
|
|
830 |
| constructorAsLambda4(Class<? extends R> c, Class<? super T1> arg1T, Class<? super T2> arg2T, |
|
831 |
| Class<? super T3> arg3T, Class<? super T4> arg4T) { |
|
832 |
0
| return new ConstructorLambda4<T1, T2, T3, T4, R>(c, arg1T, arg2T, arg3T, arg4T);
|
|
833 |
| } |
|
834 |
| |
|
835 |
| |
|
836 |
| |
|
837 |
| private static abstract class ConstructorWrapper<R> implements Serializable { |
|
838 |
| private final Class<? extends R> _c; |
|
839 |
| private final Class<?>[] _signature; |
|
840 |
| private transient Constructor<? extends R> _k; |
|
841 |
| |
|
842 |
8
| protected ConstructorWrapper(Class<? extends R> c, Class<?>... signature) {
|
|
843 |
8
| _c = c;
|
|
844 |
8
| _signature = signature;
|
|
845 |
8
| _k = null;
|
|
846 |
| } |
|
847 |
| |
|
848 |
| |
|
849 |
9
| protected R invoke(Object... args) {
|
|
850 |
9
| try {
|
|
851 |
8
| if (_k == null) { _k = _c.getConstructor(_signature); }
|
|
852 |
8
| return _k.newInstance(args);
|
|
853 |
| } |
|
854 |
1
| catch (NoSuchMethodException e) { throw new WrappedException(new NoSuchMethodReflectException(e)); }
|
|
855 |
0
| catch (IllegalArgumentException e) { throw new WrappedException(new IllegalArgumentReflectException(e)); }
|
|
856 |
1
| catch (InvocationTargetException e) { throw new WrappedException(new InvocationTargetReflectException(e)); }
|
|
857 |
0
| catch (IllegalAccessException e) { throw new WrappedException(new IllegalAccessReflectException(e)); }
|
|
858 |
1
| catch (InstantiationException e) { throw new WrappedException(new InstantiationReflectException(e)); }
|
|
859 |
0
| catch (SecurityException e) { throw new WrappedException(new SecurityReflectException(e)); }
|
|
860 |
| } |
|
861 |
| } |
|
862 |
| |
|
863 |
| private static final class ConstructorThunk<R> extends ConstructorWrapper<R> implements Thunk<R> { |
|
864 |
4
| public ConstructorThunk(Class<? extends R> c) { super(c); }
|
|
865 |
4
| public R value() { return invoke(); }
|
|
866 |
| } |
|
867 |
| |
|
868 |
| private static final class ConstructorLambda<T, R> extends ConstructorWrapper<R> implements Lambda<T, R> { |
|
869 |
3
| public ConstructorLambda(Class<? extends R> c, Class<? super T> argT) { super(c, argT); }
|
|
870 |
4
| public R value(T arg) { return invoke(arg); }
|
|
871 |
| } |
|
872 |
| |
|
873 |
| private static final class ConstructorLambda2<T1, T2, R> extends ConstructorWrapper<R> |
|
874 |
| implements Lambda2<T1, T2, R> { |
|
875 |
0
| public ConstructorLambda2(Class<? extends R> c, Class<? super T1> arg1T, Class<? super T2> arg2T) {
|
|
876 |
0
| super(c, arg1T, arg2T);
|
|
877 |
| } |
|
878 |
0
| public R value(T1 arg1, T2 arg2) { return invoke(arg1, arg2); }
|
|
879 |
| } |
|
880 |
| |
|
881 |
| private static final class ConstructorLambda3<T1, T2, T3, R> extends ConstructorWrapper<R> |
|
882 |
| implements Lambda3<T1, T2, T3, R> { |
|
883 |
1
| public ConstructorLambda3(Class<? extends R> c, Class<? super T1> arg1T, Class<? super T2> arg2T,
|
|
884 |
| Class<? super T3> arg3T) { |
|
885 |
1
| super(c, arg1T, arg2T, arg3T);
|
|
886 |
| } |
|
887 |
1
| public R value(T1 arg1, T2 arg2, T3 arg3) { return invoke(arg1, arg2, arg3); }
|
|
888 |
| } |
|
889 |
| |
|
890 |
| private static final class ConstructorLambda4<T1, T2, T3, T4, R> extends ConstructorWrapper<R> |
|
891 |
| implements Lambda4<T1, T2, T3, T4, R> { |
|
892 |
0
| public ConstructorLambda4(Class<? extends R> c, Class<? super T1> arg1T, Class<? super T2> arg2T,
|
|
893 |
| Class<? super T3> arg3T, Class<? super T4> arg4T) { |
|
894 |
0
| super(c, arg1T, arg2T, arg3T, arg4T);
|
|
895 |
| } |
|
896 |
0
| public R value(T1 arg1, T2 arg2, T3 arg3, T4 arg4) { return invoke(arg1, arg2, arg3, arg4); }
|
|
897 |
| } |
|
898 |
| } |