Class-File API

The Class-File API is defined in the package java.lang.classfile, which is used for parsing, generating, and transforming Java class files. The API processes the class files that tracks the class file format defined by the chapter "The class File Format" in The Java Virtual Machine Specification.. See Java Language and Virtual Machine Specifications.

Note:

This is a preview feature. A preview feature is a feature whose design, specification, and implementation are complete, but is not permanent. A preview feature may exist in a different form or not at all in future Java SE releases. To compile and run code that contains preview features, you must specify additional command-line options. See Preview Language and VM Features. For background information about the Class-File API, see JEP 457.
The Class-File API is defined by several key principles, such as:
  • It treats all class-file entities such as fields, methods, attributes, and bytecode instructions as immutable objects. This immutable representation ensures reliable sharing when a class file undergoes transformations.
  • It uses a tree structure to represent the hierarchical nature of class files.
  • It enables user-driven navigation for efficient parsing.
  • It emphasizes laziness in parsing, processing only the class files that are required by the user.
  • It transforms as an emergent property if the class-file parsing and generation APIs are sufficiently aligned. This does not require its own special mode or significant new API surface.

The Class-File API incorporates three main abstractions: elements, builders, and transforms. Elements are immutable descriptions of class file components. A builder facilities the construction of class files using specific building methods. There’s a build for each kind of compound element. Transforms represent functions that modify elements during the building process.

The API also introduces new methods for parsing class files using patterns. This enables more direct and concise expressions, leveraging Java's pattern-matching capabilities. The frameworks and tools that use this API automatically support the class files from the latest JDK.

See the package java.lang.classfile.