This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPublicStaticMethodInvocationTest.java
117 lines (96 loc) · 4.46 KB
/
PublicStaticMethodInvocationTest.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package none.cvg.methods;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import none.cvg.DemoClass;
import none.cvg.HandlesKataDisplayNames;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static none.cvg.ErrorMessages.REFLECTION_FAILURE;
import static none.cvg.ErrorMessages.TEST_FAILURE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
/*
* TODO:
* This test aims at using MethodHandles to invoke a public static method on a class.
* Each solved test shows how this can be achieved with the traditional reflection calls.
* Each unsolved test provides a few hints that will allow the kata-taker to manually solve
* the exercise to achieve the same goal with MethodHandles.
*/
@DisplayNameGeneration(HandlesKataDisplayNames.class)
@DisplayName("Invoke DemoClass.publicStaticMethod(String)")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class PublicStaticMethodInvocationTest {
@Test
@Tag("PASSING")
@Order(1)
public void reflectionPublicStaticMethod() {
String expectedOutput = "DemoClass.class - Public static method via reflection";
try {
// Find the method on the class via a getMethod.
Method publicStaticMethod =
DemoClass.class.getMethod("publicStaticMethod",
String.class);
assertEquals(expectedOutput,
publicStaticMethod.invoke(DemoClass.class, "via reflection"),
"Reflection invocation failed");
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
fail(REFLECTION_FAILURE.getValue() + e.getMessage());
}
}
@Test
@Tag("TODO")
@Order(2)
public void methodHandlePublicStaticMethod() {
String expectedOutput = "DemoClass.class - Public static method via Method Handles";
/*
* TODO:
* Get a public lookup from java.lang.invoke.MethodHandles
* Public parts of a class are looked up via "public lookups"
* Check API: java.lang.invoke.MethodHandles.publicLookup()
*/
MethodHandles.Lookup publicStaticMethodHandlesLookup = null;
/*
* TODO:
* Replace the "null"s with valid values to get a signature for a publicStaticMethod.
* Create a methodType instance that matches the signature of what we wish to invoke
* The publicMethod() returns a String and accepts a String parameter
* Check API: java.lang.invoke.MethodType.methodType(?, ?)
*/
MethodType methodType = null; // HINT: MethodType.methodType(null, null);
try {
/*
* TODO:
* Replace the "null"s with valid values to get a handle to publicStaticMethod.
* "Find" a method of the class via the Lookup instance,
* based on the methodType described above and the method name.
* Public static methods can be searched via the findStatic() method.
* Check API: java.lang.invoke.MethodHandles.Lookup.findStatic(?, ?, ?)
*/
MethodHandle publicStaticMethodHandle =
publicStaticMethodHandlesLookup.findStatic(null,
null, null); // Class, Method name, Method type
/*
* ATTENTION: The invoke() method does not take an instance of demoClass as its
* first parameter for static method invocation.
*/
assertEquals(expectedOutput,
publicStaticMethodHandle.invoke(
"via Method Handles"),
"Method handles invocation failed");
} catch (NoSuchMethodException | IllegalAccessException e) {
fail("Failed to execute a public static method invocation via Method Handles: "
+ e.getMessage());
} catch (Throwable t) {
// invoke throws a Throwable (hence catching Throwable separately).
fail(TEST_FAILURE.getValue() + t.getMessage());
}
}
}