JEP 309: Dynamic Class-File Constants
Summary
Extend the Java class-file format to support a new constant-pool form, CONSTANT_Dynamic
. Loading a CONSTANT_Dynamic
will delegate creation to a bootstrap method, just as linking an invokedynamic
call site delegates linkage to a bootstrap method.
Goals
We seek to reduce the cost and disruption of creating new forms of materializable class-file constants, which in turn offers language designers and compiler implementors broader options for expressivity and performance. We do so by creating a single new constant-pool form that can be parameterized with user-provided behavior, in the form of a bootstrap method with static arguments.
We will also adjust the link-time handshake between the JVM and bootstrap methods, so as to adapt the bootstrap API used by invokedynamic
to apply also to dynamic constants.
Based on experience with invokedynamic
we will make adjustments to the bootstrapping handshake of both invokedynamic
and dynamic constants, loosening certain restrictions on the processing of argument lists to bootstrap methods.
This work requires some prototyping of JDK library support for a representative sample of a few kinds of constant types, notably variable handles (JEP 193). In support of such prototyping, this work will coordinate with other work on basic language support for constant expressions (JEP 303).
Non-Goals
This JEP aims to support arbitrary constants in the constant pool. Although there are proposals for still other uses of bootstrap methods, such as method recipes, this JEP concentrates on one use.
The success of this JEP does not depend on support from the Java language or the Java compiler backend, although it is more likely to succeed if it is used by the compiler backend.
Although large aggregate constants are a weak point in Java's translation strategy, this JEP cannot solve for aggregates until there are better ways to encapsulate them in constant forms, such as frozen arrays or primitive-specialized lists.
Success Metrics
As a minimal requirement, it should be practical to expose constant-pool forms to describe primitive class mirrors (int.class
), null
, enum
constants, and most forms of VarHandle
in terms of CONSTANT_Dynamic
.
Dynamic constants must be usable in any context which currently allows general constant pool constants, such as CONSTANT_String
and CONSTANT_MethodType
. Thus, they must be valid operands to the ldc
instruction and must be allowed as static parameters to bootstrap methods.
The bootstrap-method handshake should support complex constants which contain thousands of component arguments, lifting the current limit of 251 constant arguments. As a stretch goal, there should also be a way for the bootstrap method to more accurately control linkage errors produced by resolving bootstrap method arguments.
At the end of the work we should also have cause to believe that this mechanism can be made to work for a wide variety of library types, such as derived method handles, small immutable collections (lists, maps, sets), numerics, regular expressions, string formatters, or simple data classes.
Followup work should be identified and documented. See "Possible extensions" below.