mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8280377: MethodHandleProxies does not correctly invoke default methods with varags
Reviewed-by: alanb
This commit is contained in:
parent
2eab86b513
commit
a183bfb436
12 changed files with 295 additions and 66 deletions
|
@ -28,9 +28,11 @@ package java.lang.invoke;
|
|||
import java.lang.reflect.*;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
|
||||
import jdk.internal.access.JavaLangReflectAccess;
|
||||
import jdk.internal.access.SharedSecrets;
|
||||
import sun.invoke.WrapperInstance;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import jdk.internal.reflect.CallerSensitive;
|
||||
import jdk.internal.reflect.Reflection;
|
||||
|
@ -184,8 +186,6 @@ 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;
|
||||
|
@ -202,7 +202,8 @@ public class MethodHandleProxies {
|
|||
if (isObjectMethod(method))
|
||||
return callObjectMethod(proxy, method, args);
|
||||
if (isDefaultMethod(method)) {
|
||||
return callDefaultMethod(defaultMethodMap, proxy, intfc, method, args);
|
||||
// no additional access check is performed
|
||||
return JLRA.invokeDefault(proxy, method, args, null);
|
||||
}
|
||||
throw newInternalError("bad proxy method: "+method);
|
||||
}
|
||||
|
@ -320,37 +321,5 @@ public class MethodHandleProxies {
|
|||
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);
|
||||
}
|
||||
private static final JavaLangReflectAccess JLRA = SharedSecrets.getJavaLangReflectAccess();
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ package java.lang.reflect;
|
|||
import jdk.internal.reflect.CallerSensitive;
|
||||
import jdk.internal.reflect.Reflection;
|
||||
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
|
@ -262,33 +261,6 @@ public interface InvocationHandler {
|
|||
throws Throwable {
|
||||
Objects.requireNonNull(proxy);
|
||||
Objects.requireNonNull(method);
|
||||
|
||||
// verify that the object is actually a proxy instance
|
||||
if (!Proxy.isProxyClass(proxy.getClass())) {
|
||||
throw new IllegalArgumentException("'proxy' is not a proxy instance");
|
||||
}
|
||||
if (!method.isDefault()) {
|
||||
throw new IllegalArgumentException("\"" + method + "\" is not a default method");
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends Proxy> proxyClass = (Class<? extends Proxy>)proxy.getClass();
|
||||
|
||||
Class<?> intf = method.getDeclaringClass();
|
||||
// access check on the default method
|
||||
method.checkAccess(Reflection.getCallerClass(), intf, proxyClass, method.getModifiers());
|
||||
|
||||
MethodHandle mh = Proxy.defaultMethodHandle(proxyClass, method);
|
||||
// invoke the super method
|
||||
try {
|
||||
// the args array can be null if the number of formal parameters required by
|
||||
// the method is zero (consistent with Method::invoke)
|
||||
Object[] params = args != null ? args : Proxy.EMPTY_ARGS;
|
||||
return mh.invokeExact(proxy, params);
|
||||
} catch (ClassCastException | NullPointerException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
} catch (Proxy.InvocationException e) {
|
||||
// unwrap and throw the exception thrown by the default method
|
||||
throw e.getCause();
|
||||
}
|
||||
return Proxy.invokeDefault(proxy, method, args, Reflection.getCallerClass());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1314,6 +1314,46 @@ public class Proxy implements java.io.Serializable {
|
|||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* Invoke the default method of the given proxy with an explicit caller class.
|
||||
*
|
||||
* @throws IllegalAccessException if the proxy interface is inaccessible to the caller
|
||||
* if caller is non-null
|
||||
*/
|
||||
static Object invokeDefault(Object proxy, Method method, Object[] args, Class<?> caller)
|
||||
throws Throwable {
|
||||
// verify that the object is actually a proxy instance
|
||||
if (!Proxy.isProxyClass(proxy.getClass())) {
|
||||
throw new IllegalArgumentException("'proxy' is not a proxy instance");
|
||||
}
|
||||
if (!method.isDefault()) {
|
||||
throw new IllegalArgumentException("\"" + method + "\" is not a default method");
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends Proxy> proxyClass = (Class<? extends Proxy>)proxy.getClass();
|
||||
|
||||
// skip access check if caller is null
|
||||
if (caller != null) {
|
||||
Class<?> intf = method.getDeclaringClass();
|
||||
// access check on the default method
|
||||
method.checkAccess(caller, intf, proxyClass, method.getModifiers());
|
||||
}
|
||||
|
||||
MethodHandle mh = Proxy.defaultMethodHandle(proxyClass, method);
|
||||
// invoke the super method
|
||||
try {
|
||||
// the args array can be null if the number of formal parameters required by
|
||||
// the method is zero (consistent with Method::invoke)
|
||||
Object[] params = args != null ? args : Proxy.EMPTY_ARGS;
|
||||
return mh.invokeExact(proxy, params);
|
||||
} catch (ClassCastException | NullPointerException e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e);
|
||||
} catch (Proxy.InvocationException e) {
|
||||
// unwrap and throw the exception thrown by the default method
|
||||
throw e.getCause();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal exception type to wrap the exception thrown by the default method
|
||||
* so that it can distinguish CCE and NPE thrown due to the arguments
|
||||
|
|
|
@ -127,4 +127,9 @@ class ReflectAccess implements jdk.internal.access.JavaLangReflectAccess {
|
|||
{
|
||||
return ctor.newInstanceWithCaller(args, true, caller);
|
||||
}
|
||||
|
||||
public Object invokeDefault(Object proxy, Method method, Object[] args, Class<?> caller)
|
||||
throws Throwable {
|
||||
return Proxy.invokeDefault(proxy, method, args, caller);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -102,4 +102,11 @@ public interface JavaLangReflectAccess {
|
|||
/** Returns a new instance created by the given constructor with access check */
|
||||
public <T> T newInstance(Constructor<T> ctor, Object[] args, Class<?> caller)
|
||||
throws IllegalAccessException, InstantiationException, InvocationTargetException;
|
||||
|
||||
/** Invokes the given default method if the method's declaring interface is
|
||||
* accessible to the given caller. Otherwise, IllegalAccessException will
|
||||
* be thrown. If the caller is null, no access check is performed.
|
||||
*/
|
||||
public Object invokeDefault(Object proxy, Method method, Object[] args, Class<?> caller)
|
||||
throws Throwable;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue