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 pathProtectedMethodInvocationTest.java
141 lines (116 loc) · 5.45 KB
/
ProtectedMethodInvocationTest.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.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 protected 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.protectedMethod(String)")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ProtectedMethodInvocationTest {
@Test
@Tag("PASSING")
@Order(1)
public void reflectionProtectedMethod() {
String expectedOutput = "[DemoClass] - Protected method via reflection";
try {
// Cannot call getMethod(), use getDeclaredMethod() to get private and protected methods
Method protectedMethod =
DemoClass.class.getDeclaredMethod("protectedMethod",
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
*/
protectedMethod.setAccessible(true);
DemoClass demoClass = new DemoClass();
assertEquals(expectedOutput,
protectedMethod.invoke(demoClass, "via reflection"),
"Reflection invocation failed");
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
fail(REFLECTION_FAILURE.getValue() + e.getMessage());
}
}
@Test
@Order(2)
@Tag("TODO")
public void methodHandleProtectedMethod() {
String expectedOutput = "[DemoClass] - Protected method via Method Handles";
/*
* TODO:
* 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 protectedMethodHandlesLookup = null;
try {
/*
* TODO:
* Replace the "null"s valid values to create a protectedMethod instance.
* Create a method instance that matches the name "protectedMethod()"
* See java.lang.class for a getDeclaredMethod()
* Look for the declared method on the DemoClass.class
* The protectedMethod() accepts a String input parameter.
* Same call as is used in vanilla reflection.
* Check API: java.lang.Class.getDeclaredMethod(?, ?)
*/
Method protectedMethod =
DemoClass.class.getDeclaredMethod(null,
null); // 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
*/
protectedMethod.setAccessible(true);
/*
* TODO:
* Replace the null with the method instance to produce a protectedMethod 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 protectedMethodHandle =
protectedMethodHandlesLookup.unreflect(null);
DemoClass demoClass = new DemoClass();
assertEquals(expectedOutput,
protectedMethodHandle.invoke(demoClass,
"via Method Handles"),
"Method handles invocation failed");
} catch (NoSuchMethodException | IllegalAccessException e) {
fail("Failed to execute a protected method invocation via Method Handles: "
+ e.getMessage());
} catch (Throwable t) {
// invoke throws a Throwable (hence catching Throwable separately).
fail(TEST_FAILURE.getValue() + t.getMessage());
}
}
}