Interface ConstantPoolBuilder
- All Superinterfaces:
ConstantPool, Iterable<PoolEntry>
public sealed interface ConstantPoolBuilder extends ConstantPool
class file. Provides read and write access to the constant pool that is being built. Writing is append-only (the index of new entries monotonically increase) and idempotent (entry-bearing methods will return an existing entry if there is a suitable one). For class file building, an overload of ClassFile::build takes a ConstantPoolBuilder. For class file transformations via ClassFile::transformClass, the ClassFile.ConstantPoolSharingOption controls how the constant pool builder of the resulting class is created.
Alien Constant Pool Entries
Inclass file building and constant pool building, some constant pool entries supplied may be alien to this constant pool builder of the active class file builder. For example, classEntry(Utf8Entry) may be called with an alien UTF8 entry. Alien entries will be converted to a pool entry in this constant pool builder, reusing equivalent entries or adding new entries if there is none. As a result, all pool entries returned by entry-bearing methods in this constant pool builder belong to this constant pool. Some ClassFileBuilder methods may have their outputs adjusted if they receive pool entries alien to their constant pools. For example, if an ldc_w instruction with an alien entry is written to a CodeBuilder, the CodeBuilder may emit a functionally equivalent ldc instruction instead, if the converted entry can be encoded in such an instruction.
To avoid the conversion of alien constant pool entries, such as for the accuracy of the generated class file, users can always supply constant pool entries obtained by calling the constant pool builder entry-bearing methods of the constant pools associated with the
ClassFileBuilder. Otherwise, the conversions have no impact on the behaviors of the generated class files.
- Since:
- 24
- See Also:
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
BootstrapMethodEntry |
bsmEntry |
Returns a BootstrapMethodEntry referring to a MethodHandleEntry and a list of LoadableConstantEntry. |
default BootstrapMethodEntry |
bsmEntry |
Returns a BootstrapMethodEntry describing the provided bootstrap method and arguments. |
boolean |
canWriteDirect |
Returns true if the index of any entry in the given constant pool refers to the same entry in this builder. |
ClassEntry |
classEntry |
Returns a ClassEntry referring to the provided Utf8Entry. |
default ClassEntry |
classEntry |
Returns a ClassEntry describing the same reference type as the provided ClassDesc. |
ConstantDynamicEntry |
constantDynamicEntry |
|
default ConstantDynamicEntry |
constantDynamicEntry |
Returns a ConstantDynamicEntry describing the dynamic constant as the provided DynamicConstantDesc. |
default ConstantValueEntry |
constantValueEntry |
|
DoubleEntry |
doubleEntry |
Returns a DoubleEntry describing the provided value. |
FieldRefEntry |
fieldRefEntry |
|
default FieldRefEntry |
fieldRefEntry |
Returns a FieldRefEntry describing a field of a class. |
FloatEntry |
floatEntry |
Returns a FloatEntry describing the provided value. |
IntegerEntry |
intEntry |
Returns an IntegerEntry describing the provided value. |
InterfaceMethodRefEntry |
interfaceMethodRefEntry |
|
default InterfaceMethodRefEntry |
interfaceMethodRefEntry |
Returns an InterfaceMethodRefEntry describing a method of an interface. |
InvokeDynamicEntry |
invokeDynamicEntry |
|
default InvokeDynamicEntry |
invokeDynamicEntry |
Returns an InvokeDynamicEntry describing the same dynamic call site as the provided DynamicCallSiteDesc. |
default LoadableConstantEntry |
loadableConstantEntry |
Returns a LoadableConstantEntry describing the provided constant value. |
LongEntry |
longEntry |
Returns a LongEntry describing the provided value. |
MethodHandleEntry |
methodHandleEntry |
Returns a MethodHandleEntry encoding a reference kind and referring to a MemberRefEntry. |
default MethodHandleEntry |
methodHandleEntry |
Returns a MethodHandleEntry describing the same method handle as the given DirectMethodHandleDesc. |
MethodRefEntry |
methodRefEntry |
|
default MethodRefEntry |
methodRefEntry |
Returns a MethodRefEntry describing a method of a class. |
MethodTypeEntry |
methodTypeEntry |
Returns a MethodTypeEntry referring to a Utf8Entry. |
MethodTypeEntry |
methodTypeEntry |
Returns a MethodTypeEntry describing the same method type as the provided MethodTypeDesc. |
ModuleEntry |
moduleEntry |
Returns a ModuleEntry referring to the provided Utf8Entry. |
default ModuleEntry |
moduleEntry |
Returns a ModuleEntry describing the same module as the provided ModuleDesc. |
NameAndTypeEntry |
nameAndTypeEntry |
Returns a NameAndTypeEntry referring to the provided name and type Utf8Entry. |
default NameAndTypeEntry |
nameAndTypeEntry |
Returns a NameAndTypeEntry describing the provided unqualified name and field descriptor. |
default NameAndTypeEntry |
nameAndTypeEntry |
Returns a NameAndTypeEntry describing the provided name and method descriptor. |
static ConstantPoolBuilder |
of() |
Returns a new constant pool builder. |
static ConstantPoolBuilder |
of |
Returns a new constant pool builder. |
PackageEntry |
packageEntry |
Returns a PackageEntry referring to the provided Utf8Entry. |
default PackageEntry |
packageEntry |
Returns a PackageEntry describing the same package as the provided PackageDesc. |
StringEntry |
stringEntry |
Returns a StringEntry referring to a Utf8Entry. |
default StringEntry |
stringEntry |
Returns a StringEntry describing the provided value. |
default Utf8Entry |
utf8Entry |
|
default Utf8Entry |
utf8Entry |
|
Utf8Entry |
utf8Entry |
Methods declared in interface ConstantPool
bootstrapMethodCount, bootstrapMethodEntry, entryByIndex, entryByIndex, iterator, size | Modifier and Type | Method | Description |
|---|---|---|
int |
bootstrapMethodCount() |
Returns the number of entries in the bootstrap method table. |
BootstrapMethodEntry |
bootstrapMethodEntry |
Returns the BootstrapMethodEntry at the specified index within the bootstrap method table. |
PoolEntry |
entryByIndex |
Returns the entry at the specified index. |
<T extends PoolEntry> |
entryByIndex |
Returns the entry of a given type at the specified index. |
default Iterator |
iterator() |
Returns an iterator over pool entries. |
int |
size() |
Returns the exclusive upper bound of the valid indices of this constant pool. |
Methods declared in interface Iterable
forEach, spliterator | Modifier and Type | Method | Description |
|---|---|---|
default void |
forEach |
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
default Spliterator |
spliterator() |
Creates a Spliterator over the elements described by this Iterable. |
Method Details
of
static ConstantPoolBuilder of(ClassModel classModel)
size() of the source pool.- Parameters:
-
classModel- the class to copy from - Returns:
- a new constant pool builder
- See Also:
of
static ConstantPoolBuilder of()
1.- Returns:
- a new constant pool builder
- See Also:
canWriteDirect
boolean canWriteDirect(ConstantPool constantPool)
true if the index of any entry in the given constant pool refers to the same entry in this builder. This may be because they are the same builder, or because this builder was pre-populated from the given constant pool. If the constant pool of an entry is not directly writable to this pool, it is alien to this pool, and a ClassFileBuilder associated with this constant pool will convert that alien constant pool entry.
- Parameters:
-
constantPool- the given constant pool - Returns:
-
trueif the index of any entry in the given constant pool refers to the same entry in this builder - See Also:
utf8Entry
utf8Entry
default Utf8Entry utf8Entry(ClassDesc desc)
- API Note:
- The resulting
Utf8Entryis usually not referable by aClassEntry, which uses internal form of binary names. - Parameters:
-
desc- the symbolic descriptor for the class - Returns:
- a
Utf8Entrydescribing the field descriptor string of the providedClassDesc
utf8Entry
default Utf8Entry utf8Entry(MethodTypeDesc desc)
- Parameters:
-
desc- the symbolic descriptor for the method type - Returns:
- a
Utf8Entrydescribing the method descriptor string of the providedMethodTypeDesc
classEntry
ClassEntry classEntry(Utf8Entry ne)
ClassEntry referring to the provided Utf8Entry. The Utf8Entry describes the internal form of the binary name of a class or interface or the field descriptor string of an array type.- Parameters:
-
ne- theUtf8Entry - Returns:
- a
ClassEntryreferring to the providedUtf8Entry - See Also:
classEntry
default ClassEntry classEntry(ClassDesc classDesc)
ClassEntry describing the same reference type as the provided ClassDesc.- Parameters:
-
classDesc- the symbolic descriptor for the reference type - Returns:
- a
ClassEntrydescribing the same reference type as the providedClassDesc - Throws:
-
IllegalArgumentException- ifclassDescrepresents a primitive type - See Also:
packageEntry
PackageEntry packageEntry(Utf8Entry nameEntry)
PackageEntry referring to the provided Utf8Entry. The Utf8Entry describes the internal form of the name of a package.- Parameters:
-
nameEntry- theUtf8Entry - Returns:
- a
PackageEntryreferring to the providedUtf8Entry - See Also:
packageEntry
default PackageEntry packageEntry(PackageDesc packageDesc)
PackageEntry describing the same package as the provided PackageDesc.- Parameters:
-
packageDesc- the symbolic descriptor for the package - Returns:
- a
PackageEntrydescribing the same package as the providedPackageDesc - See Also:
moduleEntry
ModuleEntry moduleEntry(Utf8Entry moduleName)
- Parameters:
-
moduleName- the constant pool entry describing the module name - Returns:
- a
ModuleEntryreferring to the providedUtf8Entry - See Also:
moduleEntry
default ModuleEntry moduleEntry(ModuleDesc moduleDesc)
ModuleEntry describing the same module as the provided ModuleDesc.- Parameters:
-
moduleDesc- the symbolic descriptor for the module - Returns:
- a
ModuleEntrydescribing the same module as the providedModuleDesc - See Also:
nameAndTypeEntry
NameAndTypeEntry nameAndTypeEntry(Utf8Entry nameEntry, Utf8Entry typeEntry)
NameAndTypeEntry referring to the provided name and type Utf8Entry. The name Utf8Entry describes an unqualified name or the special name "<init>", and the type Utf8Entry describes a field or method descriptor string.- Parameters:
-
nameEntry- the nameUtf8Entry -
typeEntry- the typeUtf8Entry - Returns:
- a
NameAndTypeEntryreferring to the provided name and typeUtf8Entry - See Also:
nameAndTypeEntry
default NameAndTypeEntry nameAndTypeEntry(String name, ClassDesc type)
NameAndTypeEntry describing the provided unqualified name and field descriptor.- Parameters:
-
name- the unqualified name -
type- the field descriptor - Returns:
- a
NameAndTypeEntrydescribing the provided unqualified name and field descriptor
nameAndTypeEntry
default NameAndTypeEntry nameAndTypeEntry(String name, MethodTypeDesc type)
NameAndTypeEntry describing the provided name and method descriptor. The name can be an unqualified name or the special name "<init>".- Parameters:
-
name- the unqualified name, or "<init>" -
type- the method descriptor - Returns:
- a
NameAndTypeEntrydescribing the provided name and method descriptor
fieldRefEntry
FieldRefEntry fieldRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
FieldRefEntry referring to a ClassEntry and a NameAndTypeEntry. The ClassEntry describes a class or interface that has this field as a member, and the
NameAndTypeEntry describes the unqualified name and the field descriptor for this field.- Parameters:
-
owner- theClassEntry -
nameAndType- theNameAndTypeEntry - Returns:
- a
FieldRefEntryreferring to aClassEntryand aNameAndTypeEntry - See Also:
fieldRefEntry
default FieldRefEntry fieldRefEntry(ClassDesc owner, String name, ClassDesc type)
FieldRefEntry describing a field of a class.- Parameters:
-
owner- the class or interface the field is a member of -
name- the unqualified name of the field -
type- the field descriptor - Returns:
- a
FieldRefEntrydescribing a field of a class - Throws:
-
IllegalArgumentException- ifownerrepresents a primitive type - See Also:
methodRefEntry
MethodRefEntry methodRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
MethodRefEntry referring to a ClassEntry and a NameAndTypeEntry. The ClassEntry describes a class that has this method as a member, and the NameAndTypeEntry describes the unqualified name or the special name "<init>" and the method descriptor for this method.- Parameters:
-
owner- theClassEntry -
nameAndType- theNameAndTypeEntry - Returns:
- a
MethodRefEntryreferring to aClassEntryand aNameAndTypeEntry - See Also:
methodRefEntry
default MethodRefEntry methodRefEntry(ClassDesc owner, String name, MethodTypeDesc type)
MethodRefEntry describing a method of a class.- Parameters:
-
owner- the class the method is a member of -
name- the unqualified name, or special name "<init>", of the method -
type- the method descriptor - Returns:
- a
MethodRefEntrydescribing a method of a class - Throws:
-
IllegalArgumentException- ifownerrepresents a primitive type - See Also:
interfaceMethodRefEntry
InterfaceMethodRefEntry interfaceMethodRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
InterfaceMethodRefEntry referring to a ClassEntry and a NameAndTypeEntry. The ClassEntry describes an interface that has this method as a member, and the
NameAndTypeEntry describes the unqualified name and the method descriptor for this method.- Parameters:
-
owner- theClassEntry -
nameAndType- theNameAndTypeEntry - Returns:
- an
InterfaceMethodRefEntryreferring to aClassEntryand aNameAndTypeEntry - See Also:
interfaceMethodRefEntry
default InterfaceMethodRefEntry interfaceMethodRefEntry(ClassDesc owner, String name, MethodTypeDesc type)
InterfaceMethodRefEntry describing a method of an interface.- Parameters:
-
owner- the interface the method is a member of -
name- the unqualified name of the method -
type- the method descriptor - Returns:
- an
InterfaceMethodRefEntrydescribing a method of an interface - Throws:
-
IllegalArgumentException- ifownerrepresents a primitive type - See Also:
methodTypeEntry
MethodTypeEntry methodTypeEntry(MethodTypeDesc descriptor)
MethodTypeEntry describing the same method type as the provided MethodTypeDesc.- Parameters:
-
descriptor- the symbolic descriptor of the method type - Returns:
- a
MethodTypeEntrydescribing the same method type as the providedMethodTypeDesc - See Also:
methodTypeEntry
MethodTypeEntry methodTypeEntry(Utf8Entry descriptor)
MethodTypeEntry referring to a Utf8Entry. The Utf8Entry represents a method descriptor string.- Parameters:
-
descriptor- theUtf8Entry - Returns:
- a
MethodTypeEntryreferring to aUtf8Entry - See Also:
methodHandleEntry
default MethodHandleEntry methodHandleEntry(DirectMethodHandleDesc descriptor)
MethodHandleEntry describing the same method handle as the given DirectMethodHandleDesc.- Parameters:
-
descriptor- the symbolic descriptor of the method handle - Returns:
- a
MethodHandleEntrydescribing the same method handle as the givenDirectMethodHandleDesc - See Also:
methodHandleEntry
MethodHandleEntry methodHandleEntry(int refKind, MemberRefEntry reference)
MethodHandleEntry encoding a reference kind and referring to a MemberRefEntry. The reference kind is in [1, 9], and the MemberRefEntry is subject to various restrictions based on the reference kind (JVMS §4.4.8).- Parameters:
-
refKind- the reference kind of the method handle -
reference- theMemberRefEntry - Returns:
- a
MethodHandleEntryencoding a reference kind and referring to aMemberRefEntry - Throws:
-
IllegalArgumentException- ifrefKindis notu1 - See Also:
invokeDynamicEntry
default InvokeDynamicEntry invokeDynamicEntry(DynamicCallSiteDesc dcsd)
InvokeDynamicEntry describing the same dynamic call site as the provided DynamicCallSiteDesc.- Parameters:
-
dcsd- the symbolic descriptor of the dynamic call site - Returns:
- an
InvokeDynamicEntrydescribing the same dynamic call site as the providedDynamicCallSiteDesc - See Also:
invokeDynamicEntry
InvokeDynamicEntry invokeDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType)
InvokeDynamicEntry referring to a BootstrapMethodEntry and a NameAndTypeEntry. The BootstrapMethodEntry describes the bootstrap method and its invocation arguments in addition to the name and type, and the NameAndTypeEntry a name and a method descriptor.- Parameters:
-
bootstrapMethodEntry- theBootstrapMethodEntry -
nameAndType- theNameAndTypeEntry - Returns:
- an
InvokeDynamicEntryreferring to aBootstrapMethodEntryand aNameAndTypeEntry - See Also:
constantDynamicEntry
default ConstantDynamicEntry constantDynamicEntry(DynamicConstantDesc<?> dcd)
ConstantDynamicEntry describing the dynamic constant as the provided DynamicConstantDesc.- Parameters:
-
dcd- the symbolic descriptor of the constant - Returns:
- a
ConstantDynamicEntrydescribing the dynamic constant as the providedDynamicConstantDesc - See Also:
constantDynamicEntry
ConstantDynamicEntry constantDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType)
ConstantDynamicEntry referring to a BootstrapMethodEntry and a NameAndTypeEntry. The BootstrapMethodEntry describes the bootstrap method and its invocation arguments in addition to the name and type, and the NameAndTypeEntry a name and a field descriptor.- Parameters:
-
bootstrapMethodEntry- theBootstrapMethodEntry -
nameAndType- theNameAndTypeEntry - Returns:
- a
ConstantDynamicEntryreferring to aBootstrapMethodEntryand aNameAndTypeEntry - See Also:
intEntry
IntegerEntry intEntry(int value)
IntegerEntry describing the provided value.- Parameters:
-
value- the value - Returns:
- an
IntegerEntrydescribing the provided value - See Also:
floatEntry
FloatEntry floatEntry(float value)
FloatEntry describing the provided value. All NaN values of the float may or may not be collapsed into a single "canonical" NaN value.
- Parameters:
-
value- the value - Returns:
- a
FloatEntrydescribing the provided value - See Also:
longEntry
doubleEntry
DoubleEntry doubleEntry(double value)
DoubleEntry describing the provided value. All NaN values of the double may or may not be collapsed into a single "canonical" NaN value.
- Parameters:
-
value- the value - Returns:
- a
DoubleEntrydescribing the provided value - See Also:
stringEntry
StringEntry stringEntry(Utf8Entry utf8)
- Parameters:
-
utf8- theUtf8Entry - Returns:
- a
StringEntryreferring to aUtf8Entry - See Also:
stringEntry
default StringEntry stringEntry(String value)
StringEntry describing the provided value.- Parameters:
-
value- the value - Returns:
- a
StringEntrydescribing the provided value - See Also:
constantValueEntry
default ConstantValueEntry constantValueEntry(ConstantDesc c)
ConstantValueEntry describing the provided constant Integer, Long, Float, Double, or String value.- Parameters:
-
c- the provided constant value - Returns:
- a
ConstantValueEntrydescribing the provided constantInteger,Long,Float,Double, orStringvalue - Throws:
-
IllegalArgumentException- if the value is not one ofInteger,Long,Float,Double, orString - See Also:
loadableConstantEntry
default LoadableConstantEntry loadableConstantEntry(ConstantDesc c)
LoadableConstantEntry describing the provided constant value.- Parameters:
-
c- the nominal descriptor for the constant - Returns:
- a
LoadableConstantEntrydescribing the provided constant value
bsmEntry
default BootstrapMethodEntry bsmEntry(DirectMethodHandleDesc methodReference, List<ConstantDesc> arguments)
BootstrapMethodEntry describing the provided bootstrap method and arguments.- Parameters:
-
methodReference- the bootstrap method -
arguments- the arguments - Returns:
- a
BootstrapMethodEntrydescribing the provided bootstrap method and arguments - Throws:
-
IllegalArgumentException- if the number of arguments exceeds the limit ofu2
bsmEntry
BootstrapMethodEntry bsmEntry(MethodHandleEntry methodReference, List<LoadableConstantEntry> arguments)
BootstrapMethodEntry referring to a MethodHandleEntry and a list of LoadableConstantEntry. The MethodHandleEntry is the bootstrap method, and the list of LoadableConstantEntry is the arguments.- Parameters:
-
methodReference- theMethodHandleEntry -
arguments- the list ofLoadableConstantEntry - Returns:
- a
BootstrapMethodEntryreferring to aMethodHandleEntryand a list ofLoadableConstantEntry - Throws:
-
IllegalArgumentException- if the number of arguments exceeds the limit ofu2 - See Also:
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.