Skip to content

Commit 0021d5f

Browse files
bithazardjknack
authored andcommitted
Java 11 upgrade: Merge MemberValueResolver
1 parent 865f2d7 commit 0021d5f

4 files changed

Lines changed: 21 additions & 121 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/BaseTemplate.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.io.IOException;
2626
import java.io.Writer;
2727
import java.lang.invoke.MethodHandles;
28-
import java.lang.reflect.Constructor;
28+
import java.lang.invoke.MethodType;
2929
import java.lang.reflect.InvocationHandler;
3030
import java.lang.reflect.Method;
3131
import java.lang.reflect.Modifier;
@@ -263,19 +263,13 @@ private boolean isDefault(final Method method) {
263263
}
264264

265265
private Object invokeDefaultMethod(final Method method, final Class<?> lookupClass,
266-
final Object proxy, final Object... args)
267-
throws Throwable {
268-
// Jumping through these hoops is needed because calling unreflectSpecial requires that
269-
// the lookup instance have private access to the special caller. None of the static
270-
// factory methods for Lookup will give us an instance with the access modes we need,
271-
// so we work around it by calling the private constructor via reflection.
272-
Constructor<MethodHandles.Lookup> constructor = MethodHandles.Lookup.class
273-
.getDeclaredConstructor(Class.class, int.class);
274-
constructor.setAccessible(true);
275-
return constructor.newInstance(lookupClass, -1 /* trusted */)
276-
.unreflectSpecial(method, lookupClass)
277-
.bindTo(proxy)
278-
.invokeWithArguments(args);
266+
final Object proxy, final Object... args) throws Throwable {
267+
MethodType methodType = MethodType.methodType(method.getReturnType(),
268+
method.getParameterTypes());
269+
return MethodHandles.lookup()
270+
.findSpecial(lookupClass, method.getName(), methodType, lookupClass)
271+
.bindTo(proxy)
272+
.invokeWithArguments(args);
279273
}
280274

281275
@Override

handlebars/src/test/java/com/github/jknack/handlebars/CollectionsLengthTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class CollectionsLengthTest extends AbstractTest {
1010

11-
static class SizeAndLength {
11+
public static class SizeAndLength {
1212
int size;
1313
int length;
1414

handlebars/src/test/java/com/github/jknack/handlebars/TypeSafeTemplateDefaultMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public String getName() {
1919
}
2020
}
2121

22-
interface UserTemplate extends TypeSafeTemplate<User> {
22+
public interface UserTemplate extends TypeSafeTemplate<User> {
2323
default String renderUpperCase(User user) throws IOException {
2424
return apply(user).toUpperCase();
2525
}

handlebars/src/test/java/mustache/specs/SpecRunner.java

Lines changed: 11 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@
1717
*/
1818
package mustache.specs;
1919

20-
import java.lang.reflect.Method;
2120
import java.util.ArrayList;
2221
import java.util.Collection;
2322
import java.util.List;
2423

2524
import org.junit.runner.Description;
2625
import org.junit.runners.Parameterized;
26+
import org.junit.runners.model.FrameworkMethod;
27+
import org.junit.runners.model.TestClass;
2728

2829
public class SpecRunner extends Parameterized {
29-
3030
private List<String> labels;
31-
3231
private Description labelledDescription;
3332

3433
public SpecRunner(final Class<?> cl) throws Throwable {
@@ -37,120 +36,28 @@ public SpecRunner(final Class<?> cl) throws Throwable {
3736
generateLabelledDescription();
3837
}
3938

40-
private void initialiseLabels() throws Exception {
41-
Collection<Object[]> parameterArrays = getParameterArrays();
39+
private void initialiseLabels() throws Throwable {
40+
Collection<Object[]> parameterArrays = getParameterArrays4_4();
4241
labels = new ArrayList<String>();
4342
for (Object[] parameterArray : parameterArrays) {
4443
Spec spec = (Spec) parameterArray[0];
4544
labels.add(spec.name());
4645
}
47-
48-
}
49-
50-
private Collection<Object[]> getParameterArrays() throws Exception {
51-
Method testClassMethod = getDeclaredMethod(this.getClass(),
52-
"getTestClass");
53-
Class<?> returnType = testClassMethod.getReturnType();
54-
if (returnType == Class.class) {
55-
return getParameterArrays4_3();
56-
} else {
57-
return getParameterArrays4_4();
58-
}
59-
}
60-
61-
private Collection<Object[]> getParameterArrays4_3() throws Exception {
62-
Object[][] methodCalls = new Object[][] {new Object[] {"getTestClass" } };
63-
Class<?> cl = invokeMethodChain(this, methodCalls);
64-
Method[] methods = cl.getMethods();
65-
66-
Method parametersMethod = null;
67-
for (Method method : methods) {
68-
boolean providesParameters = method
69-
.isAnnotationPresent(Parameters.class);
70-
if (!providesParameters) {
71-
continue;
72-
}
73-
74-
if (parametersMethod != null) {
75-
throw new Exception(
76-
"Only one method should be annotated with @Labels");
77-
}
78-
79-
parametersMethod = method;
80-
}
81-
82-
if (parametersMethod == null) {
83-
throw new Exception("No @Parameters method found");
84-
}
85-
86-
@SuppressWarnings("unchecked")
87-
Collection<Object[]> parameterArrays =
88-
(Collection<Object[]>) parametersMethod
89-
.invoke(null);
90-
return parameterArrays;
91-
92-
}
93-
94-
private Collection<Object[]> getParameterArrays4_4() throws Exception {
95-
Object[][] methodCalls = new Object[][] {
96-
new Object[] {"getTestClass" },
97-
new Object[] {"getAnnotatedMethods", Class.class,
98-
Parameters.class },
99-
new Object[] {"get", int.class, 0 },
100-
// use array type for varargs (equivalent (almost))
101-
new Object[] {"invokeExplosively", Object.class, null,
102-
Object[].class, new Object[] {} } };
103-
Collection<Object[]> parameterArrays = invokeMethodChain(this,
104-
methodCalls);
105-
return parameterArrays;
10646
}
10747

10848
@SuppressWarnings("unchecked")
109-
private <T> T invokeMethodChain(Object object, final Object[][] methodCalls)
110-
throws Exception {
111-
for (Object[] methodCall : methodCalls) {
112-
String methodName = (String) methodCall[0];
113-
int parameterCount = (methodCall.length - 1) / 2;
114-
Class<?>[] classes = new Class<?>[parameterCount];
115-
Object[] arguments = new Object[parameterCount];
116-
for (int i = 1; i < methodCall.length; i += 2) {
117-
Class<?> cl = (Class<?>) methodCall[i];
118-
Object argument = methodCall[i + 1];
119-
int index = (i - 1) / 2; // messy!
120-
classes[index] = cl;
121-
arguments[index] = argument;
122-
}
123-
Method method = getDeclaredMethod(object.getClass(), methodName,
124-
classes);
125-
method.setAccessible(true);
126-
object = method.invoke(object, arguments);
127-
}
128-
return (T) object;
129-
}
130-
131-
// iterates through super-classes until found. Throws NoSuchMethodException
132-
// if not
133-
private Method getDeclaredMethod(Class<?> cl, final String methodName,
134-
final Class<?>... parameterTypes) throws NoSuchMethodException {
135-
do {
136-
try {
137-
Method method = cl
138-
.getDeclaredMethod(methodName, parameterTypes);
139-
return method;
140-
} catch (NoSuchMethodException e) {
141-
// do nothing - just fall through to the below
142-
}
143-
cl = cl.getSuperclass();
144-
} while (cl != null);
145-
throw new NoSuchMethodException("Method " + methodName
146-
+ "() not found in hierarchy");
49+
private Collection<Object[]> getParameterArrays4_4() throws Throwable {
50+
TestClass testClass = this.getTestClass();
51+
List<FrameworkMethod> annotatedMethods = testClass.getAnnotatedMethods(Parameters.class);
52+
FrameworkMethod frameworkMethod = annotatedMethods.get(0);
53+
return (Collection<Object[]>)frameworkMethod.invokeExplosively(testClass);
14754
}
14855

14956
private void generateLabelledDescription() throws Exception {
15057
Description originalDescription = super.getDescription();
15158
labelledDescription = Description
15259
.createSuiteDescription(originalDescription.getDisplayName());
153-
ArrayList<Description> childDescriptions = originalDescription
60+
List<Description> childDescriptions = originalDescription
15461
.getChildren();
15562
int childCount = childDescriptions.size();
15663
if (childCount != labels.size()) {
@@ -163,7 +70,7 @@ private void generateLabelledDescription() throws Exception {
16370
String label = labels.get(i);
16471
Description newDescription = Description
16572
.createSuiteDescription(label);
166-
ArrayList<Description> grandChildren = childDescription
73+
List<Description> grandChildren = childDescription
16774
.getChildren();
16875
for (Description grandChild : grandChildren) {
16976
newDescription.addChild(grandChild);
@@ -176,5 +83,4 @@ private void generateLabelledDescription() throws Exception {
17683
public Description getDescription() {
17784
return labelledDescription;
17885
}
179-
18086
}

0 commit comments

Comments
 (0)