6 JVM APIs
Java Virtual Machine (JVM) APIs provide a set of classes and methods that enable you to interact with and control various aspects of the JVM. These APIs provide a way to interact with the JVM at runtime, allowing you to monitor and control the execution of Java applications.
Topics:
This topic introduces you to some of the new and important APIs that you need to be aware of.JVM Constants API
The JVM Constants API is defined in the package java.lang.constants
, which contains the nominal descriptors of various types of loadable constants. These nominal descriptors are useful for applications that manipulate class files and compile-time or link-time program analysis tools.
A nominal descriptor is not the value of a loadable constant but a description of its value, which can be reconstituted given a class loading context. A loadable constant is a constant pool entry that can be pushed onto the operand stack or can appear in the static argument list of a bootstrap method for the invokedynamic
instruction. The operand stack is where JVM instructions get their input and store their output. Every Java class file has a constant pool, which contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time.
The issue with working with non-nominal loadable constants, such as a Class
objects, whose references are resolved at run-time, is that these references depend on the correctness and consistency of the class loading context. Class loading may have side effects, such as running code that you don't want run and throwing access-related and out-of-memory exceptions, which you can avoid with nominal descriptions. In addition, class loading may not be possible at all.
See the package java.lang.constant
.
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.
For background information, see JEP 484: Class-File API.
- 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
.