1 | static Object call(Class c, String method, Object... args) ctex {
|
2 | Method m = call_findStaticMethod(c, method, args); |
3 | m.setAccessible(true); |
4 | return m.invoke(null, args); |
5 | } |
6 | |
7 | static Object call(Object o, String method, Object... args) ctex {
|
8 | Method m = call_findMethod(o, method, args); |
9 | m.setAccessible(true); |
10 | return m.invoke(o, args); |
11 | } |
12 | |
13 | static Method call_findMethod(Object o, String method, Object[] args) {
|
14 | // TODO: go to superclasses too |
15 | for (Method m : o.getClass().getDeclaredMethods()) |
16 | if (m.getName().equals(method) && (m.getModifiers() & Modifier.STATIC) == 0 && m.getParameterTypes().length == args.length) // TODO: multicast |
17 | return m; |
18 | throw new RuntimeException("Method '" + method + "' with " + args.length + " parameter(s) not found in " + o.getClass().getName());
|
19 | } |
20 | |
21 | static Method call_findStaticMethod(Class<?> c, String method, Object[] args) {
|
22 | for (Method m : c.getDeclaredMethods()) |
23 | if (m.getName().equals(method) && (m.getModifiers() & Modifier.STATIC) != 0 && m.getParameterTypes().length == args.length) // TODO: multicast |
24 | return m; |
25 | throw new RuntimeException("Static method '" + method + "' with " + args.length + " parameter(s) not found in " + c.getName());
|
26 | } |
Snippet is not live.
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #2000492 |
| Snippet name: | call function(s) for reflective calls (e.g. to hotwired objects) |
| Eternal ID of this version: | #2000492/1 |
| Text MD5: | 54565f37057521dd8e889d1c4b88eecf |
| Author: | stefan |
| Category: | |
| Type: | New Tinybrain snippet |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-08-03 01:10:00 |
| Source code size: | 1300 bytes / 26 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 858 / 235 |
| Referenced in: | [show references] |