|
25 | 25 | import java.lang.invoke.MethodType; |
26 | 26 | import java.lang.reflect.Method; |
27 | 27 | import java.lang.reflect.Modifier; |
| 28 | +import java.util.Arrays; |
28 | 29 |
|
29 | 30 | /** |
30 | 31 | * @author Ampflower |
@@ -85,20 +86,42 @@ private static boolean signature(Method method, MethodType signature) { |
85 | 86 | return true; |
86 | 87 | } |
87 | 88 |
|
| 89 | + private static MethodHandle virtual(MethodType signature, Class<?> clazz, String reference) throws IllegalAccessException { |
| 90 | + for (final var method : clazz.getMethods()) { |
| 91 | + if (Modifier.isStatic(method.getModifiers())) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + if (!virtual(method, reference, signature)) { |
| 95 | + continue; |
| 96 | + } |
| 97 | + return lookup.unreflect(method); |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
88 | 102 | public static MethodHandle virtual(Class<?> clazz, String reference, MethodType signature) { |
89 | 103 | try { |
90 | | - for (final var method : clazz.getMethods()) { |
91 | | - if (Modifier.isStatic(method.getModifiers())) { |
92 | | - continue; |
93 | | - } |
94 | | - if (!virtual(method, reference, signature)) { |
95 | | - continue; |
96 | | - } |
97 | | - return lookup.unreflect(method); |
| 104 | + final var method = virtual(signature, clazz, reference); |
| 105 | + if (method != null) { |
| 106 | + return method; |
98 | 107 | } |
99 | 108 | } catch (IllegalAccessException e) { |
100 | 109 | throw new AssertionError(e); |
101 | 110 | } |
102 | 111 | throw new AssertionError(clazz + " has no such method: " + reference + signature); |
103 | 112 | } |
| 113 | + |
| 114 | + public static MethodHandle virtual(Class<?> clazz, MethodType signature, String... reference) { |
| 115 | + try { |
| 116 | + for (final var i : reference) { |
| 117 | + final var method = virtual(signature, clazz, i); |
| 118 | + if (method != null) { |
| 119 | + return method; |
| 120 | + } |
| 121 | + } |
| 122 | + } catch (IllegalAccessException e) { |
| 123 | + throw new AssertionError(e); |
| 124 | + } |
| 125 | + throw new AssertionError(clazz + " has no such method: " + Arrays.toString(reference) + signature); |
| 126 | + } |
104 | 127 | } |
0 commit comments