The JVM constants API

This JEP introduces an API to standardize the description and loading of class constants.

Every Java class has a constant pool. It either stores simple values such as strings and integers or values to represent classes or methods. Class constant pool values are used as operand values for the ldc (load constant) bytecode instruction. These constants can also be used by the invokedynamic bytecode instructionin the static argument list of a bootstrap method.

When either an ldc or invokedynamic instruction executes, it represents the constant value as a Java data type value, a class, an integer, or a string. Until now, the responsibility of modeling bytecode instructions and loading constants was on the class that wanted to manipulate class files. This usually takes the focus of these classes off their business logic, moving it to the specifics of how to model bytecode instructions and load class constants. This is clearly a good candidate to separate the concerns and define the API to work with the how part.

Also, it isn't easy for classes to implement this functionality by themselves, because loading class constants isn't a simple process for non-string and non-integer values. Class loading is a complex process and has multiple points of failure. Class loading is dependent on the host environment, including the existence of classes, the ability to gain access to them, and their relevant permissions. Class loading could also fail during the linking process.

An absence of a standardized library to handle these functions of loading constants also results in mismatched expectations between programs.

JDK 12 defines a new package, java.lang.invoke.constant, which defines a family of value-based symbolic reference types. It can be used to describe all types of loadable constants. A symbolic constant uses a nominal form and essentially excludes a constant from its loading or accessibility context. The package includes types such as ClassDesc, MethodTypeDesc, MethodHandleDesc, and DynamicConstantDes to describe various constants. Data types such as String, Integer, Long, Float, and Double are also used to represent simple class constants.

There are varied use cases of this package. Libraries that parse or generate bytecodes need to describe classes and method handles in a symbolic manner. Bootstraps for invokedynamic will get simpler because they will be able to work with symbolic representation, rather than working with live classes and method handles. It will be simpler for the compiler and offline transformers to describe classes and their members, which can't be loaded into the running Virtual Machine (VM). Compiler plugins, such as annotation processors, also need to describe uses classes and their members in symbolic terms.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.18