-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathTestSolutionPrivateMethodInvocation.java
141 lines (116 loc) · 5.5 KB
/
TestSolutionPrivateMethodInvocation.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package none.cvg.methods;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import none.cvg.handles.DemoClass;
import none.cvg.handles.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.handles.ErrorMessages.REFLECTION_FAILURE;
import static none.cvg.handles.ErrorMessages.TEST_FAILURE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
/*
* DONE:
* This test aims at using MethodHandles to invoke a private 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.privateMethod(String)")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestSolutionPrivateMethodInvocation {
@Test
@Tag("PASSING")
@Order(1)
public void reflectionPrivateMethod() {
String expectedOutput = "[DemoClass] - Private method via reflection";
try {
// Cannot call getMethod(), use getDeclaredMethod() to get private and protected methods
Method privateMethod =
DemoClass.class.getDeclaredMethod("privateMethod",
String.class);
/*
* Method has to be made accessible.
* Not setting this will cause IllegalAccessException
* Setting accessible to true causes the JVM to skip access control checks
*
* This is needed to access non-public content
*/
privateMethod.setAccessible(true);
DemoClass demoClass = new DemoClass();
assertEquals(expectedOutput,
privateMethod.invoke(demoClass, "via reflection"),
"Reflection invocation failed");
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
fail(REFLECTION_FAILURE.getValue() + e.getMessage());
}
}
@Test
@Tag("PASSING")
@Order(2)
public void methodHandlePrivateMethod() {
String expectedOutput = "[DemoClass] - Private method via Method Handles";
/*
* DONE:
* Get a non-public lookup from java.lang.invoke.MethodHandles
* Non-public methods are looked up via "lookup"
* Find the differences between lookup() and publicLookup() on MethodHandles.
* Check API: java.lang.invoke.MethodHandles.lookup()
*/
MethodHandles.Lookup privateMethodHandlesLookup = MethodHandles.lookup();
try {
/*
* DONE:
* Replace the "null"s with valid values to produce a privateMethod instance.
* Create a method instance that matches the name "privateMethod()"
* See java.lang.class for a getDeclaredMethod()
* Look for the declared method on the DemoClass.class
* The privateMethod() needs a String input parameter.
* Same call as is used in vanilla reflection.
* Check API: java.lang.Class.getDeclaredMethod(?, ?)
*/
Method privateMethod =
DemoClass.class.getDeclaredMethod("privateMethod",
String.class); // Method name, Class ... = Parameter types
/*
* Method has to be made accessible.
* Not setting this will cause IllegalAccessException
* Setting accessible to true causes the JVM to skip access control checks
*
* This is needed to access non-public content
*/
privateMethod.setAccessible(true);
/*
* DONE:
* Replace the "null" with a valid value to get an unreflected private method handle.
* Unreflect to create a method handle from a method instance
* Generate a MethodHandle from the method instance created earlier.
* See unreflect method in the innerclass Lookup of MethodHandles.
* java.lang.invoke.MethodHandles.Lookup.unreflect()
* We can unreflect the method since we bypassed the access control checks above.
* Check API: java.lang.invoke.MethodHandles.Lookup.unreflect(?)
*/
MethodHandle privateMethodHandle =
privateMethodHandlesLookup.unreflect(privateMethod);
DemoClass demoClass = new DemoClass();
assertEquals(expectedOutput,
privateMethodHandle.invoke(demoClass,
"via Method Handles"),
"Method handles invocation failed");
} catch (NoSuchMethodException | IllegalAccessException e) {
fail("Failed to execute a private method invocation via Method Handles: "
+ e.getMessage());
} catch (Throwable t) {
// invoke throws a Throwable (hence catching Throwable separately).
fail(TEST_FAILURE.getValue() + t.getMessage());
}
}
}