-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodHandleTest.java
More file actions
32 lines (24 loc) · 864 Bytes
/
Copy pathMethodHandleTest.java
File metadata and controls
32 lines (24 loc) · 864 Bytes
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
package jvm;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import static java.lang.invoke.MethodHandles.lookup;
public class MethodHandleTest {
static class ClassA {
public void println(String s) {
System.out.println(s);
}
}
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, Throwable {
// TODO Auto-generated method stub
// Object obj = System.currentTimeMillis() % 2 == 0 ? System.out
// : new ClassA();
Object obj = new ClassA();
getPrintlnMH(obj).invokeExact("methodHandleTest");
}
private static MethodHandle getPrintlnMH(Object receiver)
throws NoSuchMethodException, IllegalAccessException {
MethodType mt = MethodType.methodType(void.class, String.class);
return lookup().findVirtual(receiver.getClass(), "println", mt).bindTo(
receiver);
}
}