RFR: 8333796: Add missing serialization functionality to sun.reflect.ReflectionFactory [v2]

ExE Boss duke at openjdk.org
Wed Aug 28 00:13:23 UTC 2024


On Tue, 27 Aug 2024 20:45:50 GMT, David M. Lloyd <duke at openjdk.org> wrote:

>> Issue [JDK-8164908](https://bugs.openjdk.org/browse/JDK-8164908) added support for functionality required to continue to support IIOP and custom serializers in light of additional module-based restrictions on reflection. It was expected that these libraries would use `sun.misc.Unsafe` in order to access fields of serializable classes. However, with JEP 471, the methods necessary to do this are being removed.
>> 
>> To allow these libraries to continue to function, it is proposed to add two methods to `sun.reflect.ReflectionFactory` which will allow serialization libraries to acquire a method handle to generated `readObject`/`writeObject` methods which set or get the fields of the serializable class using the serialization `GetField`/`PutField` mechanism. These generated methods should be used by serialization libraries to serialize and deserialize classes which do not have a `readObject`/`writeObject` method or which use `ObjectInputStream.defaultReadObject`/`ObjectOutputStream.defaultWriteObject` to supplement default serialization.
>> 
>> It is also proposed to add methods which allow for the reading of serialization-specific private static final fields from classes which have them.
>> 
>> With the addition of these methods, serialization libraries no longer need to rely on `Unsafe` for serialization/deserialization activities.
>> cc: @AlanBateman
>
> David M. Lloyd has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Eliminate cache
>  - Rework using facilities found in ObjectStreamClass
>    
>    This variation has the disadvantage of requiring a couple temporary arrays to be allocated each time the method handles are used.

No need to create multiple `MethodHandles.Lookup` objects in the static initialiser:

src/java.base/share/classes/java/io/ObjectStreamDefaultSupport.java line 50:

> 48:             DRO_HANDLE = MethodHandles.lookup().findStatic(ObjectStreamDefaultSupport.class, "defaultReadObject", droType);
> 49:             MethodType dwoType = MethodType.methodType(void.class, ObjectStreamClass.class, Object.class, ObjectOutputStream.class);
> 50:             DWO_HANDLE = MethodHandles.lookup().findStatic(ObjectStreamDefaultSupport.class, "defaultWriteObject", dwoType);

Suggestion:

            var lookup = MethodHandles.lookup();
            MethodType droType = MethodType.methodType(void.class, ObjectStreamClass.class, Object.class, ObjectInputStream.class);
            DRO_HANDLE = lookup.findStatic(ObjectStreamDefaultSupport.class, "defaultReadObject", droType);
            MethodType dwoType = MethodType.methodType(void.class, ObjectStreamClass.class, Object.class, ObjectOutputStream.class);
            DWO_HANDLE = lookup.findStatic(ObjectStreamDefaultSupport.class, "defaultWriteObject", dwoType);

-------------

PR Review: https://git.openjdk.org/jdk/pull/19702#pullrequestreview-2264666938
PR Review Comment: https://git.openjdk.org/jdk/pull/19702#discussion_r1733657524


More information about the core-libs-dev mailing list