RFR: 8220607 Draft JEP: Hidden Classes

Mandy Chung mandy.chung at oracle.com
Fri Dec 6 22:21:49 UTC 2019

On 12/6/19 12:09 PM, Kasper Nielsen wrote:
> On Thu, 5 Dec 2019 at 01:13, <(unknown sender)> wrote:
>> Draft JEP:
>>   ? https://bugs.openjdk.java.net/browse/JDK-8220607
> Looks good, one quick question though. Is there any reason for the asymmetry
> with Lookup.defineClass()? defineHiddenClass() takes a boolean initialize
> parameter while defineClass() does not.

Another asymmetry between Lookup::defineClass and defineHiddenClass is 
the return value, Class vs Lookup.

It's trivial to get the class from a Lookup object but we can't go the 
other way. If a framework wants to get Lookup from a hidden class it 
just defines (e.g. get a MethodHandle and invoke), if dHC returned a 
Class, the hidden class had to provide an entry point to return its 
Lookup object.  Such entry point has to be accessible to the framework 
but it has to be cautious in leaking its full-power Lookup.

> Also, as I see it there is no way to defer initialization of a hidden class,
> without instantiating an instance of it? Normally you can get around the fact
> that Unsafe.ensureClassInitialized is not public via Class.forName.

Exactly since `Class::forName` with initialize=true to invoke `<clinit>` 
does not work for a hidden class.

FWIW.  A class may also be initialized as a result of execution of other 
bytecodes such as getstatic, putstatic, invokestatic etc as specified in 
JVMS 5.5 besides new.

> Making Unsafe.ensureClassInitialized public in some way would also help when
> trying to implement the SharedSecrets "pattern" for modular access outside of
> the JDK.
> try {
>      Class.forName(initalizeClass.getName(), true,
> initalizeClass.getClassLoader());
> } catch (ClassNotFoundException e) {
>      throw new ExceptionInInitializerError(e); // Should never happen
> }
> Is a bit of an eyesore.

Yes it is.

I have considered adding Lookup::defineClassAsLookup(byte[] bytes, 
boolean initialize).   Lookup::defineClass was added in JDK 9.   I 
haven't seen any request to initialize the class.   So I think it can be 
done as a separate enhancement.  I will file a JBS issue.


More information about the valhalla-dev mailing list