8333796: Add missing serialization functionality to sun.reflect.ReflectionFactory

Reviewed-by: liach, rriggs
This commit is contained in:
David M. Lloyd 2024-11-20 14:17:28 +00:00 committed by Roger Riggs
parent 21f0ed50a2
commit e11d126a8d
6 changed files with 912 additions and 2 deletions

View File

@ -0,0 +1,177 @@
/*
* Copyright (c) 2024, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.io;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import jdk.internal.access.JavaObjectStreamReflectionAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ByteArray;
/**
* Utilities relating to serialization and deserialization of objects.
*/
final class ObjectStreamReflection {
// todo: these could be constants
private static final MethodHandle DRO_HANDLE;
private static final MethodHandle DWO_HANDLE;
static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType droType = MethodType.methodType(void.class, ObjectStreamClass.class, Object.class, ObjectInputStream.class);
DRO_HANDLE = lookup.findStatic(ObjectStreamReflection.class, "defaultReadObject", droType);
MethodType dwoType = MethodType.methodType(void.class, ObjectStreamClass.class, Object.class, ObjectOutputStream.class);
DWO_HANDLE = lookup.findStatic(ObjectStreamReflection.class, "defaultWriteObject", dwoType);
} catch (NoSuchMethodException | IllegalAccessException e) {
throw new InternalError(e);
}
}
/**
* Populate a serializable object from data acquired from the stream's
* {@link java.io.ObjectInputStream.GetField} object independently of
* the actual {@link ObjectInputStream} implementation which may
* arbitrarily override the {@link ObjectInputStream#readFields()} method
* in order to deserialize using a custom object format.
* <p>
* The fields are populated using the mechanism defined in {@link ObjectStreamClass},
* which requires objects and primitives to each be packed into a separate array
* whose relative field offsets are defined in the {@link ObjectStreamField}
* corresponding to each field.
* Utility methods on the {@code ObjectStreamClass} instance are then used
* to validate and perform the actual field accesses.
*
* @param streamClass the object stream class of the object (must not be {@code null})
* @param obj the object to deserialize (must not be {@code null})
* @param ois the object stream (must not be {@code null})
* @throws IOException if the call to {@link ObjectInputStream#readFields}
* or one of its field accessors throws this exception type
* @throws ClassNotFoundException if the call to {@link ObjectInputStream#readFields}
* or one of its field accessors throws this exception type
*/
private static void defaultReadObject(ObjectStreamClass streamClass, Object obj, ObjectInputStream ois)
throws IOException, ClassNotFoundException {
ObjectInputStream.GetField getField = ois.readFields();
byte[] bytes = new byte[streamClass.getPrimDataSize()];
Object[] objs = new Object[streamClass.getNumObjFields()];
for (ObjectStreamField field : streamClass.getFields(false)) {
int offset = field.getOffset();
String fieldName = field.getName();
switch (field.getTypeCode()) {
case 'B' -> bytes[offset] = getField.get(fieldName, (byte) 0);
case 'C' -> ByteArray.setChar(bytes, offset, getField.get(fieldName, (char) 0));
case 'D' -> ByteArray.setDoubleRaw(bytes, offset, getField.get(fieldName, 0.0));
case 'F' -> ByteArray.setFloatRaw(bytes, offset, getField.get(fieldName, 0.0f));
case 'I' -> ByteArray.setInt(bytes, offset, getField.get(fieldName, 0));
case 'J' -> ByteArray.setLong(bytes, offset, getField.get(fieldName, 0L));
case 'S' -> ByteArray.setShort(bytes, offset, getField.get(fieldName, (short) 0));
case 'Z' -> ByteArray.setBoolean(bytes, offset, getField.get(fieldName, false));
case '[', 'L' -> objs[offset] = getField.get(fieldName, null);
default -> throw new IllegalStateException();
}
}
streamClass.checkObjFieldValueTypes(obj, objs);
streamClass.setPrimFieldValues(obj, bytes);
streamClass.setObjFieldValues(obj, objs);
}
/**
* Populate and write a stream's {@link java.io.ObjectOutputStream.PutField} object
* from field data acquired from a serializable object independently of
* the actual {@link ObjectOutputStream} implementation which may
* arbitrarily override the {@link ObjectOutputStream#putFields()}
* and {@link ObjectOutputStream#writeFields()} methods
* in order to deserialize using a custom object format.
* <p>
* The fields are accessed using the mechanism defined in {@link ObjectStreamClass},
* which causes objects and primitives to each be packed into a separate array
* whose relative field offsets are defined in the {@link ObjectStreamField}
* corresponding to each field.
*
* @param streamClass the object stream class of the object (must not be {@code null})
* @param obj the object to serialize (must not be {@code null})
* @param oos the object stream (must not be {@code null})
* @throws IOException if the call to {@link ObjectInputStream#readFields}
* or one of its field accessors throws this exception type
*/
private static void defaultWriteObject(ObjectStreamClass streamClass, Object obj, ObjectOutputStream oos)
throws IOException {
ObjectOutputStream.PutField putField = oos.putFields();
byte[] bytes = new byte[streamClass.getPrimDataSize()];
Object[] objs = new Object[streamClass.getNumObjFields()];
streamClass.getPrimFieldValues(obj, bytes);
streamClass.getObjFieldValues(obj, objs);
for (ObjectStreamField field : streamClass.getFields(false)) {
int offset = field.getOffset();
String fieldName = field.getName();
switch (field.getTypeCode()) {
case 'B' -> putField.put(fieldName, bytes[offset]);
case 'C' -> putField.put(fieldName, ByteArray.getChar(bytes, offset));
case 'D' -> putField.put(fieldName, ByteArray.getDouble(bytes, offset));
case 'F' -> putField.put(fieldName, ByteArray.getFloat(bytes, offset));
case 'I' -> putField.put(fieldName, ByteArray.getInt(bytes, offset));
case 'J' -> putField.put(fieldName, ByteArray.getLong(bytes, offset));
case 'S' -> putField.put(fieldName, ByteArray.getShort(bytes, offset));
case 'Z' -> putField.put(fieldName, ByteArray.getBoolean(bytes, offset));
case '[', 'L' -> putField.put(fieldName, objs[offset]);
default -> throw new IllegalStateException();
}
}
oos.writeFields();
}
static final class Access implements JavaObjectStreamReflectionAccess {
static {
SharedSecrets.setJavaObjectStreamReflectionAccess(new Access());
}
public MethodHandle defaultReadObject(Class<?> clazz) {
return handleForClass(DRO_HANDLE, clazz, ObjectInputStream.class);
}
public MethodHandle defaultWriteObject(Class<?> clazz) {
return handleForClass(DWO_HANDLE, clazz, ObjectOutputStream.class);
}
private static MethodHandle handleForClass(final MethodHandle handle, final Class<?> clazz, final Class<?> ioClass) {
ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz);
if (streamClass != null) {
try {
streamClass.checkDefaultSerialize();
return handle.bindTo(streamClass)
.asType(MethodType.methodType(void.class, clazz, ioClass));
} catch (InvalidClassException e) {
// ignore and return null
}
}
return null;
}
}
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2024, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.internal.access;
import java.lang.invoke.MethodHandle;
public interface JavaObjectStreamReflectionAccess {
MethodHandle defaultReadObject(Class<?> clazz);
MethodHandle defaultWriteObject(Class<?> clazz);
}

View File

@ -77,6 +77,7 @@ public class SharedSecrets {
private static JavaObjectInputStreamReadString javaObjectInputStreamReadString;
private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
private static JavaObjectInputFilterAccess javaObjectInputFilterAccess;
private static JavaObjectStreamReflectionAccess javaObjectStreamReflectionAccess;
private static JavaNetInetAddressAccess javaNetInetAddressAccess;
private static JavaNetHttpCookieAccess javaNetHttpCookieAccess;
private static JavaNetUriAccess javaNetUriAccess;
@ -431,6 +432,21 @@ public class SharedSecrets {
javaObjectInputFilterAccess = access;
}
public static JavaObjectStreamReflectionAccess getJavaObjectStreamReflectionAccess() {
var access = javaObjectStreamReflectionAccess;
if (access == null) {
try {
Class.forName("java.io.ObjectStreamReflection$Access", true, null);
access = javaObjectStreamReflectionAccess;
} catch (ClassNotFoundException e) {}
}
return access;
}
public static void setJavaObjectStreamReflectionAccess(JavaObjectStreamReflectionAccess access) {
javaObjectStreamReflectionAccess = access;
}
public static void setJavaIORandomAccessFileAccess(JavaIORandomAccessFileAccess jirafa) {
javaIORandomAccessFileAccess = jirafa;
}

View File

@ -29,6 +29,7 @@ import java.io.Externalizable;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamField;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.lang.invoke.MethodHandle;
@ -39,7 +40,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.security.PrivilegedAction;
import java.util.Set;
import jdk.internal.access.JavaLangReflectAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.VM;
@ -66,6 +70,7 @@ public class ReflectionFactory {
private static volatile Method hasStaticInitializerMethod;
private final JavaLangReflectAccess langReflectAccess;
private ReflectionFactory() {
this.langReflectAccess = SharedSecrets.getJavaLangReflectAccess();
}
@ -363,6 +368,46 @@ public class ReflectionFactory {
}
}
public final MethodHandle defaultReadObjectForSerialization(Class<?> cl) {
if (hasDefaultOrNoSerialization(cl)) {
return null;
}
return SharedSecrets.getJavaObjectStreamReflectionAccess().defaultReadObject(cl);
}
public final MethodHandle defaultWriteObjectForSerialization(Class<?> cl) {
if (hasDefaultOrNoSerialization(cl)) {
return null;
}
return SharedSecrets.getJavaObjectStreamReflectionAccess().defaultWriteObject(cl);
}
/**
* These are specific leaf classes which appear to be Serializable, but which
* have special semantics according to the serialization specification. We
* could theoretically include array classes here, but it is easier and clearer
* to just use `Class#isArray` instead.
*/
private static final Set<Class<?>> nonSerializableLeafClasses = Set.of(
Class.class,
String.class,
ObjectStreamClass.class
);
private static boolean hasDefaultOrNoSerialization(Class<?> cl) {
return ! Serializable.class.isAssignableFrom(cl)
|| cl.isInterface()
|| cl.isArray()
|| Proxy.isProxyClass(cl)
|| Externalizable.class.isAssignableFrom(cl)
|| cl.isEnum()
|| cl.isRecord()
|| cl.isHidden()
|| nonSerializableLeafClasses.contains(cl);
}
/**
* Returns a MethodHandle for {@code writeReplace} on the serializable class
* or null if no match found.
@ -468,6 +513,28 @@ public class ReflectionFactory {
}
}
public final ObjectStreamField[] serialPersistentFields(Class<?> cl) {
if (! Serializable.class.isAssignableFrom(cl) || cl.isInterface() || cl.isEnum()) {
return null;
}
try {
Field field = cl.getDeclaredField("serialPersistentFields");
int mods = field.getModifiers();
if (! (Modifier.isStatic(mods) && Modifier.isPrivate(mods) && Modifier.isFinal(mods))) {
return null;
}
if (field.getType() != ObjectStreamField[].class) {
return null;
}
field.setAccessible(true);
ObjectStreamField[] array = (ObjectStreamField[]) field.get(null);
return array != null && array.length > 0 ? array.clone() : array;
} catch (ReflectiveOperationException e) {
return null;
}
}
//--------------------------------------------------------------------------
//
// Internals only below this point
@ -556,5 +623,4 @@ public class ReflectionFactory {
return cl1.getClassLoader() == cl2.getClassLoader() &&
cl1.getPackageName() == cl2.getPackageName();
}
}

View File

@ -25,6 +25,9 @@
package sun.reflect;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.OptionalDataException;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
@ -129,6 +132,50 @@ public class ReflectionFactory {
return delegate.readObjectNoDataForSerialization(cl);
}
/**
* Generate and return a direct MethodHandle which implements
* the general default behavior for serializable class's {@code readObject}.
* The generated method behaves in accordance with the
* Java Serialization specification's rules for that method.
* <p>
* The generated method will invoke {@link ObjectInputStream#readFields()}
* to acquire the stream field values. The serialization fields of the class will
* then be populated from the stream values.
* <p>
* Only fields which are eligible for default serialization will be populated.
* This includes only fields which are not {@code transient} and not {@code static}
* (even if the field is {@code final} or {@code private}).
* <p>
* Requesting a default serialization method for a class in a disallowed
* category is not supported; {@code null} will be returned for such classes.
* The disallowed categories include (but are not limited to):
* <ul>
* <li>Classes which do not implement {@code Serializable}</li>
* <li>Classes which implement {@code Externalizable}</li>
* <li>Classes which are specially handled by the Java Serialization specification,
* including record classes, enum constant classes, {@code Class}, {@code String},
* array classes, reflection proxy classes, and hidden classes</li>
* <li>Classes which declare a valid {@code serialPersistentFields} value</li>
* <li>Any special types which may possibly be added to the JDK or the Java language
* in the future which in turn might require special handling by the
* provisions of the corresponding future version of the Java Serialization
* specification</li>
* </ul>
* <p>
* The generated method will accept the instance as its first argument
* and the {@code ObjectInputStream} as its second argument.
* The return type of the method is {@code void}.
*
* @param cl a Serializable class
* @return a direct MethodHandle for the synthetic {@code readObject} method
* or {@code null} if the class falls in a disallowed category
*
* @since 24
*/
public final MethodHandle defaultReadObjectForSerialization(Class<?> cl) {
return delegate.defaultReadObjectForSerialization(cl);
}
/**
* Returns a direct MethodHandle for the {@code writeObject} method on
* a Serializable class.
@ -144,6 +191,53 @@ public class ReflectionFactory {
return delegate.writeObjectForSerialization(cl);
}
/**
* Generate and return a direct MethodHandle which implements
* the general default behavior for serializable class's {@code writeObject}.
* The generated method behaves in accordance with the
* Java Serialization specification's rules for that method.
* <p>
* The generated method will invoke {@link ObjectOutputStream#putFields}
* to acquire the buffer for the stream field values. The buffer will
* be populated from the serialization fields of the class. The buffer
* will then be flushed to the stream using the
* {@link ObjectOutputStream#writeFields()} method.
* <p>
* Only fields which are eligible for default serialization will be written
* to the buffer.
* This includes only fields which are not {@code transient} and not {@code static}
* (even if the field is {@code final} or {@code private}).
* <p>
* Requesting a default serialization method for a class in a disallowed
* category is not supported; {@code null} will be returned for such classes.
* The disallowed categories include (but are not limited to):
* <ul>
* <li>Classes which do not implement {@code Serializable}</li>
* <li>Classes which implement {@code Externalizable}</li>
* <li>Classes which are specially handled by the Java Serialization specification,
* including record classes, enum constant classes, {@code Class}, {@code String},
* array classes, reflection proxy classes, and hidden classes</li>
* <li>Classes which declare a valid {@code serialPersistentFields} value</li>
* <li>Any special types which may possibly be added to the JDK or the Java language
* in the future which in turn might require special handling by the
* provisions of the corresponding future version of the Java Serialization
* specification</li>
* </ul>
* <p>
* The generated method will accept the instance as its first argument
* and the {@code ObjectOutputStream} as its second argument.
* The return type of the method is {@code void}.
*
* @param cl a Serializable class
* @return a direct MethodHandle for the synthetic {@code writeObject} method
* or {@code null} if the class falls in a disallowed category
*
* @since 24
*/
public final MethodHandle defaultWriteObjectForSerialization(Class<?> cl) {
return delegate.defaultWriteObjectForSerialization(cl);
}
/**
* Returns a direct MethodHandle for the {@code readResolve} method on
* a serializable class.
@ -197,4 +291,21 @@ public class ReflectionFactory {
throw new InternalError("unable to create OptionalDataException", ex);
}
}
/**
* {@return the declared {@code serialPersistentFields} from a
* serializable class, or {@code null} if none is declared, the field
* is declared but not valid, or the class is not a valid serializable class}
* A class is a valid serializable class if it implements {@code Serializable}
* but not {@code Externalizable}. The {@code serialPersistentFields} field
* is valid if it meets the type and accessibility restrictions defined
* by the Java Serialization specification.
*
* @param cl a Serializable class
*
* @since 24
*/
public final ObjectStreamField[] serialPersistentFields(Class<?> cl) {
return delegate.serialPersistentFields(cl);
}
}

View File

@ -29,12 +29,16 @@ import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.io.ObjectStreamException;
import java.io.ObjectStreamField;
import java.io.OptionalDataException;
import java.io.Serial;
import java.io.Serializable;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import sun.reflect.ReflectionFactory;
@ -46,7 +50,7 @@ import org.testng.TestNG;
/*
* @test
* @bug 8137058 8164908 8168980 8275137
* @bug 8137058 8164908 8168980 8275137 8333796
* @summary Basic test for the unsupported ReflectionFactory
* @modules jdk.unsupported
* @run testng ReflectionFactoryTest
@ -327,6 +331,509 @@ public class ReflectionFactoryTest {
}
private static final String[] names = {
"boolean_",
"final_boolean",
"byte_",
"final_byte",
"char_",
"final_char",
"short_",
"final_short",
"int_",
"final_int",
"long_",
"final_long",
"float_",
"final_float",
"double_",
"final_double",
"str",
"final_str",
"writeFields",
};
// test that the generated read/write objects are working properly
@Test
static void testDefaultReadWriteObject() throws Throwable {
Ser2 ser = new Ser2((byte) 0x33, (short) 0x2244, (char) 0x5342, 0x05382716, 0xf035a73b09113bacL, 1234f, 3456.0, true, new Ser3(0x004917aa));
ser.byte_ = (byte) 0x44;
ser.short_ = (short) 0x3355;
ser.char_ = (char) 0x6593;
ser.int_ = 0x4928a299;
ser.long_ = 0x24aa19883f4b9138L;
ser.float_ = 4321f;
ser.double_ = 6543.0;
ser.boolean_ = false;
ser.ser = new Ser3(0x70b030a0);
// first, ensure that each field gets written
MethodHandle writeObject = factory.defaultWriteObjectForSerialization(Ser2.class);
Assert.assertNotNull(writeObject, "writeObject not created");
boolean[] called = new boolean[19];
@SuppressWarnings("removal")
ObjectOutputStream oos = new ObjectOutputStream() {
protected void writeObjectOverride(final Object obj) throws IOException {
throw new IOException("Wrong method called");
}
public void defaultWriteObject() throws IOException {
throw new IOException("Wrong method called");
}
public void writeFields() {
called[18] = true;
}
public PutField putFields() {
return new PutField() {
public void put(final String name, final boolean val) {
switch (name) {
case "boolean_" -> {
Assert.assertFalse(val);
called[0] = true;
}
case "final_boolean" -> {
Assert.assertTrue(val);
called[1] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final byte val) {
switch (name) {
case "byte_" -> {
Assert.assertEquals(val, (byte) 0x44);
called[2] = true;
}
case "final_byte" -> {
Assert.assertEquals(val, (byte) 0x33);
called[3] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final char val) {
switch (name) {
case "char_" -> {
Assert.assertEquals(val, (char) 0x6593);
called[4] = true;
}
case "final_char" -> {
Assert.assertEquals(val, (char) 0x5342);
called[5] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final short val) {
switch (name) {
case "short_" -> {
Assert.assertEquals(val, (short) 0x3355);
called[6] = true;
}
case "final_short" -> {
Assert.assertEquals(val, (short) 0x2244);
called[7] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final int val) {
switch (name) {
case "int_" -> {
Assert.assertEquals(val, 0x4928a299);
called[8] = true;
}
case "final_int" -> {
Assert.assertEquals(val, 0x05382716);
called[9] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final long val) {
switch (name) {
case "long_" -> {
Assert.assertEquals(val, 0x24aa19883f4b9138L);
called[10] = true;
}
case "final_long" -> {
Assert.assertEquals(val, 0xf035a73b09113bacL);
called[11] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final float val) {
switch (name) {
case "float_" -> {
Assert.assertEquals(val, 4321f);
called[12] = true;
}
case "final_float" -> {
Assert.assertEquals(val, 1234f);
called[13] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final double val) {
switch (name) {
case "double_" -> {
Assert.assertEquals(val, 6543.0);
called[14] = true;
}
case "final_double" -> {
Assert.assertEquals(val, 3456.0);
called[15] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
public void put(final String name, final Object val) {
switch (name) {
case "ser" -> {
Assert.assertEquals(val, new Ser3(0x70b030a0));
called[16] = true;
}
case "final_ser" -> {
Assert.assertEquals(val, new Ser3(0x004917aa));
called[17] = true;
}
default -> throw new Error("Unexpected field " + name);
}
}
@SuppressWarnings("removal")
public void write(final ObjectOutput out) throws IOException {
throw new IOException("Wrong method called");
}
};
}
};
writeObject.invokeExact(ser, oos);
for (int i = 0; i < 19; i ++) {
Assert.assertTrue(called[i], names[i]);
}
// now, test the read side
MethodHandle readObject = factory.defaultReadObjectForSerialization(Ser2.class);
Assert.assertNotNull(readObject, "readObject not created");
@SuppressWarnings("removal")
ObjectInputStream ois = new ObjectInputStream() {
protected Object readObjectOverride() throws IOException {
throw new IOException("Wrong method called");
}
public GetField readFields() {
return new GetField() {
public ObjectStreamClass getObjectStreamClass() {
throw new Error("Wrong method called");
}
public boolean defaulted(final String name) throws IOException {
throw new IOException("Wrong method called");
}
public boolean get(final String name, final boolean val) {
return switch (name) {
case "boolean_" -> {
called[0] = true;
yield true;
}
case "final_boolean" -> {
called[1] = true;
yield true;
}
default -> throw new Error("Unexpected field " + name);
};
}
public byte get(final String name, final byte val) {
return switch (name) {
case "byte_" -> {
called[2] = true;
yield (byte) 0x11;
}
case "final_byte" -> {
called[3] = true;
yield (byte) 0x9f;
}
default -> throw new Error("Unexpected field " + name);
};
}
public char get(final String name, final char val) {
return switch (name) {
case "char_" -> {
called[4] = true;
yield (char) 0x59a2;
}
case "final_char" -> {
called[5] = true;
yield (char) 0xe0d0;
}
default -> throw new Error("Unexpected field " + name);
};
}
public short get(final String name, final short val) {
return switch (name) {
case "short_" -> {
called[6] = true;
yield (short) 0x0917;
}
case "final_short" -> {
called[7] = true;
yield (short) 0x110e;
}
default -> throw new Error("Unexpected field " + name);
};
}
public int get(final String name, final int val) {
return switch (name) {
case "int_" -> {
called[8] = true;
yield 0xd0244e19;
}
case "final_int" -> {
called[9] = true;
yield 0x011004da;
}
default -> throw new Error("Unexpected field " + name);
};
}
public long get(final String name, final long val) {
return switch (name) {
case "long_" -> {
called[10] = true;
yield 0x0b8101d84aa31711L;
}
case "final_long" -> {
called[11] = true;
yield 0x30558aa7189ed821L;
}
default -> throw new Error("Unexpected field " + name);
};
}
public float get(final String name, final float val) {
return switch (name) {
case "float_" -> {
called[12] = true;
yield 0x5.01923ap18f;
}
case "final_float" -> {
called[13] = true;
yield 0x0.882afap1f;
}
default -> throw new Error("Unexpected field " + name);
};
}
public double get(final String name, final double val) {
return switch (name) {
case "double_" -> {
called[14] = true;
yield 0x9.4a8fp6;
}
case "final_double" -> {
called[15] = true;
yield 0xf.881a8p4;
}
default -> throw new Error("Unexpected field " + name);
};
}
public Object get(final String name, final Object val) {
return switch (name) {
case "ser" -> {
called[16] = true;
yield new Ser3(0x44cc55dd);
}
case "final_ser" -> {
called[17] = true;
yield new Ser3(0x9a8b7c6d);
}
default -> throw new Error("Unexpected field " + name);
};
}
};
}
};
// all the same methods, except for `writeFields`
Arrays.fill(called, false);
Constructor<?> ctor = factory.newConstructorForSerialization(Ser2.class, Object.class.getDeclaredConstructor());
ser = (Ser2) ctor.newInstance();
readObject.invokeExact(ser, ois);
// excluding "writeFields", so 18 instead of 19
for (int i = 0; i < 18; i ++) {
Assert.assertTrue(called[i], names[i]);
}
Assert.assertEquals(ser.byte_, (byte)0x11);
Assert.assertEquals(ser.final_byte, (byte)0x9f);
Assert.assertEquals(ser.char_, (char)0x59a2);
Assert.assertEquals(ser.final_char, (char)0xe0d0);
Assert.assertEquals(ser.short_, (short)0x0917);
Assert.assertEquals(ser.final_short, (short)0x110e);
Assert.assertEquals(ser.int_, 0xd0244e19);
Assert.assertEquals(ser.final_int, 0x011004da);
Assert.assertEquals(ser.long_, 0x0b8101d84aa31711L);
Assert.assertEquals(ser.final_long, 0x30558aa7189ed821L);
Assert.assertEquals(ser.float_, 0x5.01923ap18f);
Assert.assertEquals(ser.final_float, 0x0.882afap1f);
Assert.assertEquals(ser.double_, 0x9.4a8fp6);
Assert.assertEquals(ser.final_double, 0xf.881a8p4);
Assert.assertEquals(ser.ser, new Ser3(0x44cc55dd));
Assert.assertEquals(ser.final_ser, new Ser3(0x9a8b7c6d));
}
static class Ser2 implements Serializable {
@Serial
private static final long serialVersionUID = -2852896623833548574L;
byte byte_;
short short_;
char char_;
int int_;
long long_;
float float_;
double double_;
boolean boolean_;
Ser3 ser;
final byte final_byte;
final short final_short;
final char final_char;
final int final_int;
final long final_long;
final float final_float;
final double final_double;
final boolean final_boolean;
final Ser3 final_ser;
Ser2(final byte final_byte, final short final_short, final char final_char, final int final_int,
final long final_long, final float final_float, final double final_double,
final boolean final_boolean, final Ser3 final_ser) {
this.final_byte = final_byte;
this.final_short = final_short;
this.final_char = final_char;
this.final_int = final_int;
this.final_long = final_long;
this.final_float = final_float;
this.final_double = final_double;
this.final_boolean = final_boolean;
this.final_ser = final_ser;
}
}
static class Ser3 implements Serializable {
@Serial
private static final long serialVersionUID = -1234752876749422678L;
@Serial
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("value", int.class)
};
final int value;
Ser3(final int value) {
this.value = value;
}
public boolean equals(final Object obj) {
return obj instanceof Ser3 s && value == s.value;
}
public int hashCode() {
return value;
}
}
static class SerInvalidFields implements Serializable {
// this is deliberately wrong
@SuppressWarnings({"unused", "serial"})
@Serial
private static final String serialPersistentFields = "Oops!";
@Serial
private static final long serialVersionUID = -8090960816811629489L;
}
static class Ext1 implements Externalizable {
@Serial
private static final long serialVersionUID = 7109990719266285013L;
public void writeExternal(final ObjectOutput objectOutput) {
}
public void readExternal(final ObjectInput objectInput) {
}
}
static class Ext2 implements Externalizable {
public void writeExternal(final ObjectOutput objectOutput) {
}
public void readExternal(final ObjectInput objectInput) {
}
}
record Rec1(int hello, boolean world) implements Serializable {
@Serial
private static final long serialVersionUID = 12349876L;
}
enum Enum1 {
hello,
world,
;
private static final long serialVersionUID = 1020304050L;
}
interface Proxy1 {
void hello();
}
static class SerialPersistentFields implements Serializable {
@Serial
private static final long serialVersionUID = -4947917866973382882L;
@Serial
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("array1", Object[].class),
new ObjectStreamField("nonExistent", String.class)
};
private int int1;
private Object[] array1;
}
// Check our simple accessors
@Test
static void testAccessors() {
Assert.assertEquals(factory.serialPersistentFields(Ser3.class), Ser3.serialPersistentFields);
Assert.assertNotSame(factory.serialPersistentFields(Ser3.class), Ser3.serialPersistentFields);
Assert.assertNull(factory.serialPersistentFields(SerInvalidFields.class));
}
// Ensure that classes with serialPersistentFields do not allow default setting/getting
@Test
static void testDisallowed() {
Assert.assertNull(factory.defaultWriteObjectForSerialization(SerialPersistentFields.class));
Assert.assertNull(factory.defaultReadObjectForSerialization(SerialPersistentFields.class));
}
// Main can be used to run the tests from the command line with only testng.jar.
@SuppressWarnings("raw_types")
@Test(enabled = false)