W3cubDocs

/OpenJDK 25

Interface ConstantPoolBuilder

All Superinterfaces:
ConstantPool, Iterable<PoolEntry>
public sealed interface ConstantPoolBuilder extends ConstantPool
Builder for the constant pool of a 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

In class 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(MethodHandleEntry methodReference, List<LoadableConstantEntry> arguments)
Returns a BootstrapMethodEntry referring to a MethodHandleEntry and a list of LoadableConstantEntry.
default BootstrapMethodEntry bsmEntry(DirectMethodHandleDesc methodReference, List<ConstantDesc> arguments)
Returns a BootstrapMethodEntry describing the provided bootstrap method and arguments.
boolean canWriteDirect(ConstantPool constantPool)
Returns true if the index of any entry in the given constant pool refers to the same entry in this builder.
ClassEntry classEntry(Utf8Entry ne)
Returns a ClassEntry referring to the provided Utf8Entry.
default ClassEntry classEntry(ClassDesc classDesc)
Returns a ClassEntry describing the same reference type as the provided ClassDesc.
ConstantDynamicEntry constantDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType)
default ConstantDynamicEntry constantDynamicEntry(DynamicConstantDesc<?> dcd)
Returns a ConstantDynamicEntry describing the dynamic constant as the provided DynamicConstantDesc.
default ConstantValueEntry constantValueEntry(ConstantDesc c)
Returns a ConstantValueEntry describing the provided constant Integer, Long, Float, Double, or String value.
DoubleEntry doubleEntry(double value)
Returns a DoubleEntry describing the provided value.
FieldRefEntry fieldRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
Returns a FieldRefEntry referring to a ClassEntry and a NameAndTypeEntry.
default FieldRefEntry fieldRefEntry(ClassDesc owner, String name, ClassDesc type)
Returns a FieldRefEntry describing a field of a class.
FloatEntry floatEntry(float value)
Returns a FloatEntry describing the provided value.
IntegerEntry intEntry(int value)
Returns an IntegerEntry describing the provided value.
InterfaceMethodRefEntry interfaceMethodRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
Returns an InterfaceMethodRefEntry referring to a ClassEntry and a NameAndTypeEntry.
default InterfaceMethodRefEntry interfaceMethodRefEntry(ClassDesc owner, String name, MethodTypeDesc type)
Returns an InterfaceMethodRefEntry describing a method of an interface.
InvokeDynamicEntry invokeDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType)
default InvokeDynamicEntry invokeDynamicEntry(DynamicCallSiteDesc dcsd)
Returns an InvokeDynamicEntry describing the same dynamic call site as the provided DynamicCallSiteDesc.
default LoadableConstantEntry loadableConstantEntry(ConstantDesc c)
Returns a LoadableConstantEntry describing the provided constant value.
LongEntry longEntry(long value)
Returns a LongEntry describing the provided value.
MethodHandleEntry methodHandleEntry(int refKind, MemberRefEntry reference)
Returns a MethodHandleEntry encoding a reference kind and referring to a MemberRefEntry.
default MethodHandleEntry methodHandleEntry(DirectMethodHandleDesc descriptor)
Returns a MethodHandleEntry describing the same method handle as the given DirectMethodHandleDesc.
MethodRefEntry methodRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
Returns a MethodRefEntry referring to a ClassEntry and a NameAndTypeEntry.
default MethodRefEntry methodRefEntry(ClassDesc owner, String name, MethodTypeDesc type)
Returns a MethodRefEntry describing a method of a class.
MethodTypeEntry methodTypeEntry(Utf8Entry descriptor)
Returns a MethodTypeEntry referring to a Utf8Entry.
MethodTypeEntry methodTypeEntry(MethodTypeDesc descriptor)
Returns a MethodTypeEntry describing the same method type as the provided MethodTypeDesc.
ModuleEntry moduleEntry(Utf8Entry moduleName)
Returns a ModuleEntry referring to the provided Utf8Entry.
default ModuleEntry moduleEntry(ModuleDesc moduleDesc)
Returns a ModuleEntry describing the same module as the provided ModuleDesc.
NameAndTypeEntry nameAndTypeEntry(Utf8Entry nameEntry, Utf8Entry typeEntry)
Returns a NameAndTypeEntry referring to the provided name and type Utf8Entry.
default NameAndTypeEntry nameAndTypeEntry(String name, ClassDesc type)
Returns a NameAndTypeEntry describing the provided unqualified name and field descriptor.
default NameAndTypeEntry nameAndTypeEntry(String name, MethodTypeDesc type)
Returns a NameAndTypeEntry describing the provided name and method descriptor.
static ConstantPoolBuilder of()
Returns a new constant pool builder.
static ConstantPoolBuilder of(ClassModel classModel)
Returns a new constant pool builder.
PackageEntry packageEntry(Utf8Entry nameEntry)
Returns a PackageEntry referring to the provided Utf8Entry.
default PackageEntry packageEntry(PackageDesc packageDesc)
Returns a PackageEntry describing the same package as the provided PackageDesc.
StringEntry stringEntry(Utf8Entry utf8)
Returns a StringEntry referring to a Utf8Entry.
default StringEntry stringEntry(String value)
Returns a StringEntry describing the provided value.
default Utf8Entry utf8Entry(ClassDesc desc)
Returns a Utf8Entry describing the field descriptor string of the provided ClassDesc.
default Utf8Entry utf8Entry(MethodTypeDesc desc)
Returns a Utf8Entry describing the method descriptor string of the provided MethodTypeDesc.
Utf8Entry utf8Entry(String s)
Returns a Utf8Entry describing the provided String.

Methods declared in interface Iterable

forEach, spliterator

Method Details

of

static ConstantPoolBuilder of(ClassModel classModel)
Returns a new constant pool builder. The new constant pool builder will be pre-populated with the contents of the constant pool associated with the given class model. The index of new entries will start from the size() of the source pool.
Parameters:
classModel - the class to copy from
Returns:
a new constant pool builder
See Also:

of

static ConstantPoolBuilder of()
Returns a new constant pool builder. The new constant pool builder will be empty. The index of new entries will start from 1.
Returns:
a new constant pool builder
See Also:

canWriteDirect

boolean canWriteDirect(ConstantPool constantPool)
Returns 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:
true if the index of any entry in the given constant pool refers to the same entry in this builder
See Also:

utf8Entry

Utf8Entry utf8Entry(String s)
Returns a Utf8Entry describing the provided String.
Parameters:
s - the string
Returns:
a Utf8Entry describing the provided String
See Also:

utf8Entry

default Utf8Entry utf8Entry(ClassDesc desc)
Returns a Utf8Entry describing the field descriptor string of the provided ClassDesc.
API Note:
The resulting Utf8Entry is usually not referable by a ClassEntry, which uses internal form of binary names.
Parameters:
desc - the symbolic descriptor for the class
Returns:
a Utf8Entry describing the field descriptor string of the provided ClassDesc

utf8Entry

default Utf8Entry utf8Entry(MethodTypeDesc desc)
Returns a Utf8Entry describing the method descriptor string of the provided MethodTypeDesc.
Parameters:
desc - the symbolic descriptor for the method type
Returns:
a Utf8Entry describing the method descriptor string of the provided MethodTypeDesc

classEntry

ClassEntry classEntry(Utf8Entry ne)
Returns a 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 - the Utf8Entry
Returns:
a ClassEntry referring to the provided Utf8Entry
See Also:

classEntry

default ClassEntry classEntry(ClassDesc classDesc)
Returns a ClassEntry describing the same reference type as the provided ClassDesc.
Parameters:
classDesc - the symbolic descriptor for the reference type
Returns:
a ClassEntry describing the same reference type as the provided ClassDesc
Throws:
IllegalArgumentException - if classDesc represents a primitive type
See Also:

packageEntry

PackageEntry packageEntry(Utf8Entry nameEntry)
Returns a PackageEntry referring to the provided Utf8Entry. The Utf8Entry describes the internal form of the name of a package.
Parameters:
nameEntry - the Utf8Entry
Returns:
a PackageEntry referring to the provided Utf8Entry
See Also:

packageEntry

default PackageEntry packageEntry(PackageDesc packageDesc)
Returns a PackageEntry describing the same package as the provided PackageDesc.
Parameters:
packageDesc - the symbolic descriptor for the package
Returns:
a PackageEntry describing the same package as the provided PackageDesc
See Also:

moduleEntry

ModuleEntry moduleEntry(Utf8Entry moduleName)
Returns a ModuleEntry referring to the provided Utf8Entry. The Utf8Entry describes the module name.
Parameters:
moduleName - the constant pool entry describing the module name
Returns:
a ModuleEntry referring to the provided Utf8Entry
See Also:

moduleEntry

default ModuleEntry moduleEntry(ModuleDesc moduleDesc)
Returns a ModuleEntry describing the same module as the provided ModuleDesc.
Parameters:
moduleDesc - the symbolic descriptor for the module
Returns:
a ModuleEntry describing the same module as the provided ModuleDesc
See Also:

nameAndTypeEntry

NameAndTypeEntry nameAndTypeEntry(Utf8Entry nameEntry, Utf8Entry typeEntry)
Returns a 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 name Utf8Entry
typeEntry - the type Utf8Entry
Returns:
a NameAndTypeEntry referring to the provided name and type Utf8Entry
See Also:

nameAndTypeEntry

default NameAndTypeEntry nameAndTypeEntry(String name, ClassDesc type)
Returns a NameAndTypeEntry describing the provided unqualified name and field descriptor.
Parameters:
name - the unqualified name
type - the field descriptor
Returns:
a NameAndTypeEntry describing the provided unqualified name and field descriptor

nameAndTypeEntry

default NameAndTypeEntry nameAndTypeEntry(String name, MethodTypeDesc type)
Returns a 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 NameAndTypeEntry describing the provided name and method descriptor

fieldRefEntry

FieldRefEntry fieldRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
Returns a 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 - the ClassEntry
nameAndType - the NameAndTypeEntry
Returns:
a FieldRefEntry referring to a ClassEntry and a NameAndTypeEntry
See Also:

fieldRefEntry

default FieldRefEntry fieldRefEntry(ClassDesc owner, String name, ClassDesc type)
Returns a 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 FieldRefEntry describing a field of a class
Throws:
IllegalArgumentException - if owner represents a primitive type
See Also:

methodRefEntry

MethodRefEntry methodRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
Returns a 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 - the ClassEntry
nameAndType - the NameAndTypeEntry
Returns:
a MethodRefEntry referring to a ClassEntry and a NameAndTypeEntry
See Also:

methodRefEntry

default MethodRefEntry methodRefEntry(ClassDesc owner, String name, MethodTypeDesc type)
Returns a 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 MethodRefEntry describing a method of a class
Throws:
IllegalArgumentException - if owner represents a primitive type
See Also:

interfaceMethodRefEntry

InterfaceMethodRefEntry interfaceMethodRefEntry(ClassEntry owner, NameAndTypeEntry nameAndType)
Returns an 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 - the ClassEntry
nameAndType - the NameAndTypeEntry
Returns:
an InterfaceMethodRefEntry referring to a ClassEntry and a NameAndTypeEntry
See Also:

interfaceMethodRefEntry

default InterfaceMethodRefEntry interfaceMethodRefEntry(ClassDesc owner, String name, MethodTypeDesc type)
Returns an 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 InterfaceMethodRefEntry describing a method of an interface
Throws:
IllegalArgumentException - if owner represents a primitive type
See Also:

methodTypeEntry

MethodTypeEntry methodTypeEntry(MethodTypeDesc descriptor)
Returns a MethodTypeEntry describing the same method type as the provided MethodTypeDesc.
Parameters:
descriptor - the symbolic descriptor of the method type
Returns:
a MethodTypeEntry describing the same method type as the provided MethodTypeDesc
See Also:

methodTypeEntry

MethodTypeEntry methodTypeEntry(Utf8Entry descriptor)
Returns a MethodTypeEntry referring to a Utf8Entry. The Utf8Entry represents a method descriptor string.
Parameters:
descriptor - the Utf8Entry
Returns:
a MethodTypeEntry referring to a Utf8Entry
See Also:

methodHandleEntry

default MethodHandleEntry methodHandleEntry(DirectMethodHandleDesc descriptor)
Returns a MethodHandleEntry describing the same method handle as the given DirectMethodHandleDesc.
Parameters:
descriptor - the symbolic descriptor of the method handle
Returns:
a MethodHandleEntry describing the same method handle as the given DirectMethodHandleDesc
See Also:

methodHandleEntry

MethodHandleEntry methodHandleEntry(int refKind, MemberRefEntry reference)
Returns a MethodHandleEntry encoding a reference kind and referring to a MemberRefEntry. The reference kind must be 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 - the MemberRefEntry
Returns:
a MethodHandleEntry encoding a reference kind and referring to a MemberRefEntry
See Also:

invokeDynamicEntry

default InvokeDynamicEntry invokeDynamicEntry(DynamicCallSiteDesc dcsd)
Returns an InvokeDynamicEntry describing the same dynamic call site as the provided DynamicCallSiteDesc.
Parameters:
dcsd - the symbolic descriptor of the dynamic call site
Returns:
an InvokeDynamicEntry describing the same dynamic call site as the provided DynamicCallSiteDesc
See Also:

invokeDynamicEntry

InvokeDynamicEntry invokeDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType)
Returns an 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 - the BootstrapMethodEntry
nameAndType - the NameAndTypeEntry
Returns:
an InvokeDynamicEntry referring to a BootstrapMethodEntry and a NameAndTypeEntry
See Also:

constantDynamicEntry

default ConstantDynamicEntry constantDynamicEntry(DynamicConstantDesc<?> dcd)
Returns a ConstantDynamicEntry describing the dynamic constant as the provided DynamicConstantDesc.
Parameters:
dcd - the symbolic descriptor of the constant
Returns:
a ConstantDynamicEntry describing the dynamic constant as the provided DynamicConstantDesc
See Also:

constantDynamicEntry

ConstantDynamicEntry constantDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType)
Returns a 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 - the BootstrapMethodEntry
nameAndType - the NameAndTypeEntry
Returns:
a ConstantDynamicEntry referring to a BootstrapMethodEntry and a NameAndTypeEntry
See Also:

intEntry

IntegerEntry intEntry(int value)
Returns an IntegerEntry describing the provided value.
Parameters:
value - the value
Returns:
an IntegerEntry describing the provided value
See Also:

floatEntry

FloatEntry floatEntry(float value)
Returns a 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 FloatEntry describing the provided value
See Also:

longEntry

LongEntry longEntry(long value)
Returns a LongEntry describing the provided value.
Parameters:
value - the value
Returns:
a LongEntry describing the provided value
See Also:

doubleEntry

DoubleEntry doubleEntry(double value)
Returns a 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 DoubleEntry describing the provided value
See Also:

stringEntry

StringEntry stringEntry(Utf8Entry utf8)
Returns a StringEntry referring to a Utf8Entry. The Utf8Entry describes the string value.
Parameters:
utf8 - the Utf8Entry
Returns:
a StringEntry referring to a Utf8Entry
See Also:

stringEntry

default StringEntry stringEntry(String value)
Returns a StringEntry describing the provided value.
Parameters:
value - the value
Returns:
a StringEntry describing the provided value
See Also:

constantValueEntry

default ConstantValueEntry constantValueEntry(ConstantDesc c)
Returns a ConstantValueEntry describing the provided constant Integer, Long, Float, Double, or String value.
Parameters:
c - the provided constant value
Returns:
a ConstantValueEntry describing the provided constant Integer, Long, Float, Double, or String value
Throws:
IllegalArgumentException - if the value is not one of Integer, Long, Float, Double, or String
See Also:

loadableConstantEntry

default LoadableConstantEntry loadableConstantEntry(ConstantDesc c)
Returns a LoadableConstantEntry describing the provided constant value.
Parameters:
c - the nominal descriptor for the constant
Returns:
a LoadableConstantEntry describing the provided constant value

bsmEntry

default BootstrapMethodEntry bsmEntry(DirectMethodHandleDesc methodReference, List<ConstantDesc> arguments)
Returns a BootstrapMethodEntry describing the provided bootstrap method and arguments.
Parameters:
methodReference - the bootstrap method
arguments - the arguments
Returns:
a BootstrapMethodEntry describing the provided bootstrap method and arguments

bsmEntry

BootstrapMethodEntry bsmEntry(MethodHandleEntry methodReference, List<LoadableConstantEntry> arguments)
Returns a 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 - the MethodHandleEntry
arguments - the list of LoadableConstantEntry
Returns:
a BootstrapMethodEntry referring to a MethodHandleEntry and a list of LoadableConstantEntry
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.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/classfile/constantpool/ConstantPoolBuilder.html