-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambdaMetafactoryTest.java
70 lines (56 loc) · 1.92 KB
/
LambdaMetafactoryTest.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package invoke;
import org.junit.jupiter.api.Test;
import java.lang.invoke.*;
/**
* Xxx1和Xxx2结果相同
*/
public class LambdaMetafactoryTest {
@Test
public void staticRun1() {
Runnable runnable = LambdaMetafactoryTest::staticRun;
// [static]hello world!
runnable.run();
}
@Test
public void staticRun2() throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
"run",
MethodType.methodType(Runnable.class),
MethodType.methodType(void.class),
lookup.findStatic(LambdaMetafactoryTest.class, "staticRun", MethodType.methodType(void.class)),
MethodType.methodType(void.class)
);
Runnable runnable = (Runnable) callSite.dynamicInvoker().invokeExact();
// [static]hello world!
runnable.run();
}
private static void staticRun() {
System.out.println("[static]hello world!");
}
@Test
public void run1() {
Runnable runnable = this::run;
// [instance]hello world
runnable.run();
}
@Test
public void run2() throws Throwable {
MethodHandles.Lookup lookup = MethodHandles.lookup();
CallSite callSite = LambdaMetafactory.metafactory(
lookup,
"run",
MethodType.methodType(Runnable.class, LambdaMetafactoryTest.class),
MethodType.methodType(void.class),
lookup.findVirtual(LambdaMetafactoryTest.class, "run", MethodType.methodType(void.class)),
MethodType.methodType(void.class)
);
Runnable runnable = (Runnable) callSite.dynamicInvoker().invokeExact(this);
// [instance]hello world
runnable.run();
}
private void run() {
System.out.println("[instance]hello world");
}
}