Hidden classes could be coming to Java

Hidden classes could be coming to Java

A proposal before the OpenJDK development team would add hidden classes to Java, a capability intended to improve the efficiency of language implementations built on the JVM.

Hidden classes are classes that cannot be used directly by the bytecode of other classes, according to the JDK Enhancement Proposal. Rather, hidden classes are intended to be used by frameworks that generate classes at runtime and use them indirectly via reflection. A hidden class can be defined as a member of an access control nest and may be weakly referenced by its class loader. There is no timetable yet for when hidden classes might appear in Java.

In explaining the motivation behind the plan, the proposal states that many language implementations built on the JVM leverage dynamic class generation for efficiency and flexibility. Java’s javac compiler, for example, does not translate a lambda expression into a dedicated class file at compile time but emits bytecode to dynamically generate and instantiate a class. Similarly, non-Java JVM languages often implement higher-order features by using dynamic proxies to generate classes dynamically.

The implementers of these languages usually want a dynamically generated class to be part of an existing statically generated class and to have properties desirable of dynamically generated classes such as non-discoverability and access control. However, the standard APIs that define a class weren’t designed with these purposes in mind.