-
Notifications
You must be signed in to change notification settings - Fork 2
Hidden classes
A class can be defined as hidden, which means that:
- It doesn't appear in stack traces.
- It can be unloaded when no longer referenced, even when its
ClassLoader
is still referenced. - Other classes cannot reference the hidden class by name.
The most useful aspect of hidden classes is the unloading behavior, but the name referencing restriction is unfortunate. This means that hidden classes cannot be extended, and they cannot appear in the field, method, or constructor signatures of another class. Direct access to hidden class members isn't possible, because doing so requires that a class name be provided, but hidden classes don't really have names.
Because of the naming restriction, the members of a hidden class can only be accessed indirectly. Members that are declared in a super class or interface can be accessed using the super class or interface, and members that are declared only in the hidden class can be accessed using a MethodHandle
or a VarHandle
. With the Cojen/Maker library, the necessary transformations are applied automatically. In particular:
- Accessing a method defined in a hidden class whose declaration is inherited will be referenced by the super class or interface method.
- Accessing a method which is declared in a hidden class is performed using a
MethodHandle
. - Accessing a hidden class constructor is performed using a
MethodHandle
. - Accessing a field which is declared in a hidden class is performed using a
VarHandle
. - Calling
instanceOf
against a hidden class calls theisInstance
method. - A
cast
to a hidden class is performed using aMethodHandle
, similar to theexplicitCast
method.
For the most part, this makes working with hidden classes almost as easy as working with normal classes.