Skip to content

Commit

Permalink
Fix encapsulated default method lookup on interfaces #614
Browse files Browse the repository at this point in the history
Use MethodHandles.privateLookupIn() instead of plain MethodHandles.lookup() to enable private lookups on Java 9.
  • Loading branch information
mp911de committed Sep 28, 2017
1 parent d336f73 commit f49c8c0
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/main/java/com/lambdaworks/redis/internal/DefaultMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,39 @@ boolean isAvailable() {
*/
ENCAPSULATED {

Method privateLookupIn = findBridgeMethod();

@Override
MethodHandle lookup(Method method) throws ReflectiveOperationException {

MethodType methodType = MethodType.methodType(method.getReturnType(), method.getParameterTypes());

return MethodHandles.lookup().findSpecial(method.getDeclaringClass(), method.getName(), methodType,
method.getDeclaringClass());
return getLookup(method.getDeclaringClass()).findSpecial(method.getDeclaringClass(), method.getName(),
methodType, method.getDeclaringClass());
}

private Method findBridgeMethod() {

try {
return MethodHandles.class.getDeclaredMethod("privateLookupIn", Class.class, Lookup.class);
} catch (ReflectiveOperationException e) {
return null;
}
}

private Lookup getLookup(Class<?> declaringClass) {

Lookup lookup = MethodHandles.lookup();

if (privateLookupIn != null) {
try {
return (Lookup) privateLookupIn.invoke(null, declaringClass, lookup);
} catch (ReflectiveOperationException e) {
return lookup;
}
}

return lookup;
}

@Override
Expand Down

0 comments on commit f49c8c0

Please sign in to comment.