On Tue, 17 Mar 2026 14:27:08 GMT, Chen Liang <[email protected]> wrote:

> Hi burningtnt, your comment refers to the ReflectionFactory in 
> jdk.unsupported. This API is intended only to work with serializable classes, 
> so it rejects classes like yours, that are not serializable.
> 
> There's no plan to support use cases like yours. In fact, there are proposed 
> JIT optimizations that would be broken if we try to dynamically add 
> constructors. Your best alternative is to transform the class files of your 
> desired classes to add new constructors that fulfill your needs.

Actually, I cannot find methods to obtain MethodHandle in 
`sun.reflect.ReflectionFactory`, to special code that initializes an object by 
assigning to its instance fields directly, including final fields. As 
documented in JEP, for Serilizable classes, what I'm expecting is a 
MethodHandle that accepts multiple arguments and returns an object with all 
final fields set (to values in the arguments passed to that MethodHandle).

It will be disappointed to see JVM cannot create objects while set its final 
fields. In the past, once you get a MethodHandle.Lookup with proper permission, 
you are able to perform any operation including accessing fields or invokeing 
methods. However, we are now unable to create objects. :(

Though the following comment is far beyond JEP 500, but it might be time to 
carefully propose standard replacement (or internal ones) for 
`sun.misc.Unsafe#allocateInstance` for serialization non-Serializable classes 
(As documented in JEP 471, Future Work).

A possible solution might be providing new magic MethodHandle, which clones a 
previous available object, turns it into larval state, update one of its final 
fields, publish the value and return it. With this approach, developers of 
serilization libraries can still construct object while setting it's final 
fields.

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

PR Comment: https://git.openjdk.org/jdk/pull/25115#issuecomment-4075505821

Reply via email to