mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8206955: MethodHandleProxies.asInterfaceInstance does not support default methods
Reviewed-by: mchung, plevart
This commit is contained in:
parent
cc248634a1
commit
70b706c224
2 changed files with 155 additions and 3 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -30,6 +30,8 @@ import java.security.AccessController;
|
|||
import java.security.PrivilegedAction;
|
||||
import sun.invoke.WrapperInstance;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import jdk.internal.reflect.CallerSensitive;
|
||||
import jdk.internal.reflect.Reflection;
|
||||
import sun.reflect.misc.ReflectUtil;
|
||||
|
@ -52,8 +54,10 @@ public class MethodHandleProxies {
|
|||
* A single-method interface is an interface which declares a uniquely named method.
|
||||
* When determining the uniquely named method of a single-method interface,
|
||||
* the public {@code Object} methods ({@code toString}, {@code equals}, {@code hashCode})
|
||||
* are disregarded. For example, {@link java.util.Comparator} is a single-method interface,
|
||||
* even though it re-declares the {@code Object.equals} method.
|
||||
* are disregarded as are any default (non-abstract) methods.
|
||||
* For example, {@link java.util.Comparator} is a single-method interface,
|
||||
* even though it re-declares the {@code Object.equals} method and also
|
||||
* declares default methods, such as {@code Comparator.reverse}.
|
||||
* <p>
|
||||
* The interface must be public. No additional access checks are performed.
|
||||
* <p>
|
||||
|
@ -177,6 +181,8 @@ public class MethodHandleProxies {
|
|||
checkTarget = checkTarget.asType(checkTarget.type().changeReturnType(Object.class));
|
||||
vaTargets[i] = checkTarget.asSpreader(Object[].class, smMT.parameterCount());
|
||||
}
|
||||
final ConcurrentHashMap<Method, MethodHandle> defaultMethodMap =
|
||||
hasDefaultMethods(intfc) ? new ConcurrentHashMap<>() : null;
|
||||
final InvocationHandler ih = new InvocationHandler() {
|
||||
private Object getArg(String name) {
|
||||
if ((Object)name == "getWrapperInstanceTarget") return target;
|
||||
|
@ -192,6 +198,9 @@ public class MethodHandleProxies {
|
|||
return getArg(method.getName());
|
||||
if (isObjectMethod(method))
|
||||
return callObjectMethod(proxy, method, args);
|
||||
if (isDefaultMethod(method)) {
|
||||
return callDefaultMethod(defaultMethodMap, proxy, intfc, method, args);
|
||||
}
|
||||
throw newInternalError("bad proxy method: "+method);
|
||||
}
|
||||
};
|
||||
|
@ -315,4 +324,45 @@ public class MethodHandleProxies {
|
|||
if (uniqueName == null) return null;
|
||||
return methods.toArray(new Method[methods.size()]);
|
||||
}
|
||||
|
||||
private static
|
||||
boolean isDefaultMethod(Method m) {
|
||||
return !Modifier.isAbstract(m.getModifiers());
|
||||
}
|
||||
|
||||
private static
|
||||
boolean hasDefaultMethods(Class<?> intfc) {
|
||||
for (Method m : intfc.getMethods()) {
|
||||
if (!isObjectMethod(m) &&
|
||||
!Modifier.isAbstract(m.getModifiers())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static
|
||||
Object callDefaultMethod(ConcurrentHashMap<Method, MethodHandle> defaultMethodMap,
|
||||
Object self, Class<?> intfc, Method m, Object[] args) throws Throwable {
|
||||
assert(isDefaultMethod(m) && !isObjectMethod(m)) : m;
|
||||
|
||||
// Lazily compute the associated method handle from the method
|
||||
MethodHandle dmh = defaultMethodMap.computeIfAbsent(m, mk -> {
|
||||
try {
|
||||
// Look up the default method for special invocation thereby
|
||||
// avoiding recursive invocation back to the proxy
|
||||
MethodHandle mh = MethodHandles.Lookup.IMPL_LOOKUP.findSpecial(
|
||||
intfc, mk.getName(),
|
||||
MethodType.methodType(mk.getReturnType(), mk.getParameterTypes()),
|
||||
self.getClass());
|
||||
return mh.asSpreader(Object[].class, mk.getParameterCount());
|
||||
} catch (NoSuchMethodException | IllegalAccessException e) {
|
||||
// The method is known to exist and should be accessible, this
|
||||
// method would not be called unless the invokeinterface to the
|
||||
// default (public) method passed access control checks
|
||||
throw new InternalError(e);
|
||||
}
|
||||
});
|
||||
return dmh.invoke(self, args);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue