Download Jar. Libraryless. Click here for Pure Java version (6067L/43K).
1 | !7 |
2 | |
3 | sinterface Bla { |
4 | default S bla() { ret "ok"; } |
5 | } |
6 | |
7 | sclass Blubb implements Bla { |
8 | } |
9 | |
10 | sclass Overridden implements Bla { |
11 | public S bla() { ret "different!"; } |
12 | } |
13 | |
14 | sclass OverriddenSubclass extends Overridden {} |
15 | |
16 | p-exp { |
17 | assertEqualsVerbose("ok", call(new Blubb, "bla")); |
18 | assertEqualsVerbose("different!", call(new Overridden, "bla")); |
19 | print("==="); |
20 | assertEqualsVerbose("different!", call(new OverriddenSubclass, "bla")); |
21 | } |
22 | |
23 | final sclass _MethodCache { |
24 | final Class c; |
25 | final HashMap<S, L<Method>> cache = new HashMap; |
26 | |
27 | *(Class *c) { _init(); } |
28 | |
29 | void _init() { |
30 | Class _c = c; |
31 | while (_c != null) { |
32 | for (Method m : _c.getDeclaredMethods()) { |
33 | m.setAccessible(true); |
34 | if (m.getName().equals("bla")) print("Found: " + m); |
35 | multiMapPut(cache, m.getName(), m); |
36 | } |
37 | |
38 | _c = _c.getSuperclass(); |
39 | } |
40 | |
41 | // add default methods |
42 | for (Class intf : allInterfacesImplementedBy(c)) |
43 | for (Method m : intf.getDeclaredMethods()) |
44 | if (m.isDefault()) { |
45 | m.setAccessible(true); |
46 | if (m.getName().equals("bla")) print("Found: " + m); |
47 | multiMapPut(cache, m.getName(), m); |
48 | } |
49 | |
50 | ifdef _MethodCache_debug |
51 | print("MethodCache " + this + ": " + className(c) + " => " + keys(cache)); |
52 | endifdef |
53 | } |
54 | |
55 | // Returns only matching methods |
56 | Method findMethod(S method, O[] args) ctex { |
57 | L<Method> m = cache.get(method); |
58 | ifdef _MethodCache_debug |
59 | print("findMethod " + this + ": " + className(c) + "/" + method + " => " + m); |
60 | endifdef |
61 | if (m == null) null; |
62 | int n = m.size(); |
63 | for i to n: { |
64 | Method me = m.get(i); |
65 | if (call_checkArgs(me, args, false)) |
66 | ret me; |
67 | } |
68 | null; |
69 | } |
70 | } |
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1022197 |
Snippet name: | Call default method by reflection Spike [OK] |
Eternal ID of this version: | #1022197/12 |
Text MD5: | 4893e26e8793e21cf5ce691bc92fc619 |
Transpilation MD5: | d172293d8747b7496e55063d4d434767 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-03-11 10:22:32 |
Source code size: | 1803 bytes / 70 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 392 / 1047 |
Version history: | 11 change(s) |
Referenced in: | [show references] |