Skip to content

Commit

Permalink
8315810: Reimplement sun.reflect.ReflectionFactory::newConstructorFor…
Browse files Browse the repository at this point in the history
…Serialization with method handles

Co-authored-by: Chen Liang <liach@openjdk.org>
Reviewed-by: rriggs
  • Loading branch information
Mandy Chung and liach committed Sep 14, 2023
1 parent eb1f67b commit 5cea53d
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 40 deletions.
18 changes: 12 additions & 6 deletions src/java.base/share/classes/java/io/ObjectStreamClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -1034,19 +1034,25 @@ Object newInstance()
new AccessControlContext(domains));
} catch (UndeclaredThrowableException x) {
Throwable cause = x.getCause();
if (cause instanceof InstantiationException)
throw (InstantiationException) cause;
if (cause instanceof InvocationTargetException)
throw (InvocationTargetException) cause;
if (cause instanceof IllegalAccessException)
throw (IllegalAccessException) cause;
if (cause instanceof InstantiationException ie)
throw ie;
if (cause instanceof InvocationTargetException ite)
throw ite;
if (cause instanceof IllegalAccessException iae)
throw iae;
// not supposed to happen
throw x;
}
}
} catch (IllegalAccessException ex) {
// should not occur, as access checks have been suppressed
throw new InternalError(ex);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof Error err)
throw err;
else
throw ex;
} catch (InstantiationError err) {
var ex = new InstantiationException();
ex.initCause(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ static DirectMethodHandle make(Class<?> refc, MemberName member) {
}
static DirectMethodHandle make(MemberName member) {
if (member.isConstructor())
return makeAllocator(member);
return makeAllocator(member.getDeclaringClass(), member);
return make(member.getDeclaringClass(), member);
}
private static DirectMethodHandle makeAllocator(MemberName ctor) {
static DirectMethodHandle makeAllocator(Class<?> instanceClass, MemberName ctor) {
assert(ctor.isConstructor() && ctor.getName().equals("<init>"));
Class<?> instanceClass = ctor.getDeclaringClass();
ctor = ctor.asConstructor();
assert(ctor.isConstructor() && ctor.getReferenceKind() == REF_newInvokeSpecial) : ctor;
MethodType mtype = ctor.getMethodType().changeReturnType(instanceClass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,12 @@ public MethodHandle reflectiveInvoker(Class<?> caller) {
public Class<?>[] exceptionTypes(MethodHandle handle) {
return VarHandles.exceptionTypes(handle);
}

@Override
public MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException {
return IMPL_LOOKUP.serializableConstructor(decl, ctorToCall);
}

});
}

Expand Down
27 changes: 27 additions & 0 deletions src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Original file line number Diff line number Diff line change
Expand Up @@ -3525,6 +3525,33 @@ public MethodHandle unreflectConstructor(Constructor<?> c) throws IllegalAccessE
return lookup.getDirectConstructorNoSecurityManager(ctor.getDeclaringClass(), ctor);
}

/*
* Produces a method handle that is capable of creating instances of the given class
* and instantiated by the given constructor. No security manager check.
*
* This method should only be used by ReflectionFactory::newConstructorForSerialization.
*/
/* package-private */ MethodHandle serializableConstructor(Class<?> decl, Constructor<?> c) throws IllegalAccessException {
MemberName ctor = new MemberName(c);
assert(ctor.isConstructor() && constructorInSuperclass(decl, c));
checkAccess(REF_newInvokeSpecial, decl, ctor);
assert(!MethodHandleNatives.isCallerSensitive(ctor)); // maybeBindCaller not relevant here
return DirectMethodHandle.makeAllocator(decl, ctor).setVarargs(ctor);
}

private static boolean constructorInSuperclass(Class<?> decl, Constructor<?> ctor) {
if (decl == ctor.getDeclaringClass())
return true;

Class<?> cl = decl;
while ((cl = cl.getSuperclass()) != null) {
if (cl == ctor.getDeclaringClass()) {
return true;
}
}
return false;
}

/**
* Produces a method handle giving read access to a reflected field.
* The type of the method handle will have a return type of the field's
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,12 @@ public interface JavaLangInvokeAccess {
* @return an array of exceptions, or {@code null}.
*/
Class<?>[] exceptionTypes(MethodHandle handle);

/**
* Returns a method handle that allocates an instance of the given class
* and then invoke the given constructor of one of its superclasses.
*
* This method should only be used by ReflectionFactory::newConstructorForSerialization.
*/
MethodHandle serializableConstructor(Class<?> decl, Constructor<?> ctorToCall) throws IllegalAccessException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ private boolean isIllegalArgument(RuntimeException ex) {
private void checkReceiver(Object o) {
// NOTE: will throw NullPointerException, as specified, if o is null
if (!declaringClass.isAssignableFrom(o.getClass())) {
throw new IllegalArgumentException("object is not an instance of declaring class");
throw new IllegalArgumentException("object of type " + o.getClass().getName()
+ " is not an instance of " + declaringClass.getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,63 @@ static ConstructorAccessorImpl newConstructorAccessor(Constructor<?> ctor) {
// Ensure class initialized outside the invocation of method handle
// so that EIIE is propagated (not wrapped with ITE)
ensureClassInitialized(ctor.getDeclaringClass());
try {
MethodHandle target = makeConstructorHandle(JLIA.unreflectConstructor(ctor));
return DirectConstructorHandleAccessor.constructorAccessor(ctor, target);
} catch (IllegalAccessException e) {
throw new InternalError(e);
}
}

/**
* Creates a ConstructorAccessor that is capable of creating instances
* of the given class and instantiated by the given constructor.
*
* @param decl the class to instantiate
* @param ctor the constructor to call
* @return an accessible constructor
*/
static ConstructorAccessorImpl newSerializableConstructorAccessor(Class<?> decl, Constructor<?> ctor) {
if (!constructorInSuperclass(decl, ctor)) {
throw new UnsupportedOperationException(ctor + " not a superclass of " + decl.getName());
}

// ExceptionInInitializerError may be thrown during class initialization
// Ensure class initialized outside the invocation of method handle
// so that EIIE is propagated (not wrapped with ITE)
ensureClassInitialized(decl);
try {
MethodHandle mh = JLIA.unreflectConstructor(ctor);
int paramCount = mh.type().parameterCount();
MethodHandle target = mh.asFixedArity();
MethodType mtype = specializedMethodTypeForConstructor(paramCount);
if (paramCount > SPECIALIZED_PARAM_COUNT) {
// spread the parameters only for the non-specialized case
target = target.asSpreader(Object[].class, paramCount);
}
target = target.asType(mtype);
MethodHandle target = makeConstructorHandle(JLIA.serializableConstructor(decl, ctor));
return DirectConstructorHandleAccessor.constructorAccessor(ctor, target);
} catch (IllegalAccessException e) {
throw new InternalError(e);
}
}

private static boolean constructorInSuperclass(Class<?> decl, Constructor<?> ctor) {
if (decl == ctor.getDeclaringClass())
return true;

Class<?> cl = decl;
while ((cl = cl.getSuperclass()) != null) {
if (cl == ctor.getDeclaringClass()) {
return true;
}
}
return false;
}

private static MethodHandle makeConstructorHandle(MethodHandle ctor) {
int paramCount = ctor.type().parameterCount();
MethodHandle target = ctor.asFixedArity();
MethodType mtype = specializedMethodTypeForConstructor(paramCount);
if (paramCount > SPECIALIZED_PARAM_COUNT) {
// spread the parameters only for the non-specialized case
target = target.asSpreader(Object[].class, paramCount);
}
return target.asType(mtype);
}

/**
* Creates a FieldAccessor for the given reflected field.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,27 +370,27 @@ public final Constructor<?> newConstructorForSerialization(Class<?> cl) {
private final Constructor<?> generateConstructor(Class<?> cl,
Constructor<?> constructorToCall) {


ConstructorAccessor acc = new SerializationConstructorAccessorGenerator().
generateSerializationConstructor(cl,
Constructor<?> ctor = newConstructor(constructorToCall.getDeclaringClass(),
constructorToCall.getParameterTypes(),
constructorToCall.getExceptionTypes(),
constructorToCall.getModifiers(),
constructorToCall.getDeclaringClass());
Constructor<?> c = newConstructor(constructorToCall.getDeclaringClass(),
constructorToCall.getParameterTypes(),
constructorToCall.getExceptionTypes(),
constructorToCall.getModifiers(),
langReflectAccess.
getConstructorSlot(constructorToCall),
langReflectAccess.
getConstructorSignature(constructorToCall),
langReflectAccess.
getConstructorAnnotations(constructorToCall),
langReflectAccess.
getConstructorParameterAnnotations(constructorToCall));
setConstructorAccessor(c, acc);
c.setAccessible(true);
return c;
langReflectAccess.getConstructorSlot(constructorToCall),
langReflectAccess.getConstructorSignature(constructorToCall),
langReflectAccess.getConstructorAnnotations(constructorToCall),
langReflectAccess.getConstructorParameterAnnotations(constructorToCall));
ConstructorAccessor acc;
if (useOldSerializableConstructor()) {
acc = new SerializationConstructorAccessorGenerator().
generateSerializationConstructor(cl,
constructorToCall.getParameterTypes(),
constructorToCall.getModifiers(),
constructorToCall.getDeclaringClass());
} else {
acc = MethodHandleAccessorFactory.newSerializableConstructorAccessor(cl, ctor);
}
setConstructorAccessor(ctor, acc);
ctor.setAccessible(true);
return ctor;
}

public final MethodHandle readObjectForSerialization(Class<?> cl) {
Expand Down Expand Up @@ -548,6 +548,10 @@ static boolean useNativeAccessorOnly() {
return config().useNativeAccessorOnly;
}

static boolean useOldSerializableConstructor() {
return config().useOldSerializableConstructor;
}

private static boolean disableSerialConstructorChecks() {
return config().disableSerialConstructorChecks;
}
Expand All @@ -564,6 +568,7 @@ private static boolean disableSerialConstructorChecks() {
private static @Stable Config config;

private static final Config DEFAULT_CONFIG = new Config(false, // useNativeAccessorOnly
false, // useOldSerializeableConstructor
false); // disableSerialConstructorChecks

/**
Expand All @@ -578,6 +583,7 @@ private static boolean disableSerialConstructorChecks() {
* is to override them.
*/
private record Config(boolean useNativeAccessorOnly,
boolean useOldSerializableConstructor,
boolean disableSerialConstructorChecks) {
}

Expand All @@ -601,10 +607,12 @@ private static Config loadConfig() {
Properties props = GetPropertyAction.privilegedGetProperties();
boolean useNativeAccessorOnly =
"true".equals(props.getProperty("jdk.reflect.useNativeAccessorOnly"));
boolean useOldSerializableConstructor =
"true".equals(props.getProperty("jdk.reflect.useOldSerializableConstructor"));
boolean disableSerialConstructorChecks =
"true".equals(props.getProperty("jdk.disableSerialConstructorChecks"));

return new Config(useNativeAccessorOnly, disableSerialConstructorChecks);
return new Config(useNativeAccessorOnly, useOldSerializableConstructor, disableSerialConstructorChecks);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/jdk/java/lang/reflect/MethodHandleAccessorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ static void doTest(Field f, Object target, Object oldValue, Object newValue, Thr
};
private static final Throwable[] mismatched_target_type = new Throwable[] {
new IllegalArgumentException("argument type mismatch"),
new IllegalArgumentException("object is not an instance of declaring class"),
new IllegalArgumentException("object of type java.lang.Object is not an instance of MethodHandleAccessorsTest"),
};
private static final Throwable[] cannot_get_field = new Throwable[] {
new IllegalArgumentException("Can not get")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ static void testNonSerializableConstructor(Class<?> cl,
}
}

@Test(expectedExceptions = UnsupportedOperationException.class)
static void testConstructorNotSuperClass() throws ReflectiveOperationException {
factory.newConstructorForSerialization(Bar.class, Baz.class.getDeclaredConstructor());
}

static class Foo {
private int foo;
public Foo() {
Expand Down

1 comment on commit 5cea53d

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.