1

What exactly is the Hidden Classes feature in Java 15 ?

As i read in Java 15 documentation , Spring & Hibernate frameworks used Hidden Classes ? I need real example to Spring and/or Hibernate used Hidden Classes .

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Abd Abughazaleh
  • 4,615
  • 3
  • 44
  • 53
  • I've downloaded Spring framework source code from GitHub, searched for "defineHiddenClass" and got no hits. – Vladimir.V.Bvn Feb 28 '21 at 13:37
  • Can you write example for me, explain exactly what is it ? – Abd Abughazaleh Feb 28 '21 at 13:47
  • 1
    The purpose of this site is not giving tutorials, especially not for solving non-existing problems. Besides that, when you use lambda expressions or method references, you are already using hidden classes. – Holger Mar 01 '21 at 11:08
  • 5
    Hidden classes are not a Java _language_ feature; you don't write one in Java code. They are an implementation technique for low-level libraries that dynamically generate classes, such as `LambdaMetafactory`, which is used in the translation of lambda expressions by the Java compiler. – Brian Goetz Mar 01 '21 at 15:47
  • @BrianGoetz do you have a full example plz ? – Abd Abughazaleh Mar 02 '21 at 21:41
  • 3
    See, for example: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java#L409 – Brian Goetz Mar 02 '21 at 21:49
  • 2
    @BrianGoetz there’s one thing I don’t get. What is the purpose of the `STRONG` option? Why would you ever want to prevent the garbage collection of an entirely unreachable class that is impossible to ever get re-used? As far as I know, the hidden classes created via `Unsafe` did not have that option and lambda expressions worked well without it. – Holger Mar 18 '21 at 13:21

1 Answers1

2

To answer this question, it's important to first distinguish the Java language from the Java runtime. The former is the "Java code" a programmer writes, while the latter is the "Java program" people use to run code written in that language (among other languages, like Kotlin and Clojure).

In the Java language, there are many ways to define a class. Most classes are like ArrayList, which are top-level and have a programmer-defined name. But other ways of defining a class may not have a simple name. An anonymous inner class, for example, does not provide a way for a programmer to give it a name. The same goes for lambda expressions introduced in Java 8.

In the past, the Java runtime has had the limitation that all classes must have a name and must be publicly addressable by the runtime. This has meant that the Java compiler gives an anonymous inner class a name unlikely to conflict with any other class's name, usually with dollar signs, which are legal class names to the Java runtime, but illegal class names in the Java language. This is an implementation detail. But because classes all have names and are all addressable through the class loader, abstraction is "leaky"; there are ways these classes which may be meant to be hidden can be addressed through access to the class loader.

"Hidden classes" are a new (to Java 15) feature of the Java runtime, a way for programs to define classes that cannot be addressed by other classes running on the class loader. They still have names, but access is scoped in a way that their existence cannot be "leaked" to other parts of the program. It is not a new language feature, but a tool that a compiler (or runtime framework) may use to implement certain pre-existing language features.

To the typical Java programmer, this process is transparent. Unless your program compiles code or manipulates bytecode, you do not need to worry about this implementation detail. But for those who do, this is a valuable tool in their software toolbox.

Matthew Leidholm
  • 4,199
  • 3
  • 24
  • 26
  • 2
    Actually, that feature exists since Java 7. But now, it has become a standard, to be usable without creating dependencies to a particular runtime implementation. – Holger Mar 03 '21 at 14:43