Interface ConstantPoolBuilder
- All Superinterfaces:
ConstantPool, Iterable<PoolEntry>
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 TypeMethodDescriptionbsmEntry
(MethodHandleEntry methodReference, List<LoadableConstantEntry> arguments) Returns aBootstrapMethodEntry
referring to aMethodHandleEntry
and a list ofLoadableConstantEntry
.default BootstrapMethodEntry
bsmEntry
(DirectMethodHandleDesc methodReference, List<ConstantDesc> arguments) Returns aBootstrapMethodEntry
describing the provided bootstrap method and arguments.boolean
canWriteDirect
(ConstantPool constantPool) Returnstrue
if the index of any entry in the given constant pool refers to the same entry in this builder.classEntry
(Utf8Entry ne) Returns aClassEntry
referring to the providedUtf8Entry
.default ClassEntry
classEntry
(ClassDesc classDesc) Returns aClassEntry
describing the same reference type as the providedClassDesc
.constantDynamicEntry
(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType) default ConstantDynamicEntry
Returns aConstantDynamicEntry
describing the dynamic constant as the providedDynamicConstantDesc
.default ConstantValueEntry
doubleEntry
(double value) Returns aDoubleEntry
describing the provided value.fieldRefEntry
(ClassEntry owner, NameAndTypeEntry nameAndType) default FieldRefEntry
fieldRefEntry
(ClassDesc owner, String name, ClassDesc type) Returns aFieldRefEntry
describing a field of a class.floatEntry
(float value) Returns aFloatEntry
describing the provided value.intEntry
(int value) Returns anIntegerEntry
describing the provided value.interfaceMethodRefEntry
(ClassEntry owner, NameAndTypeEntry nameAndType) default InterfaceMethodRefEntry
interfaceMethodRefEntry
(ClassDesc owner, String name, MethodTypeDesc type) Returns anInterfaceMethodRefEntry
describing a method of an interface.invokeDynamicEntry
(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType) default InvokeDynamicEntry
Returns anInvokeDynamicEntry
describing the same dynamic call site as the providedDynamicCallSiteDesc
.default LoadableConstantEntry
Returns aLoadableConstantEntry
describing the provided constant value.longEntry
(long value) Returns aLongEntry
describing the provided value.methodHandleEntry
(int refKind, MemberRefEntry reference) Returns aMethodHandleEntry
encoding a reference kind and referring to aMemberRefEntry
.default MethodHandleEntry
methodHandleEntry
(DirectMethodHandleDesc descriptor) Returns aMethodHandleEntry
describing the same method handle as the givenDirectMethodHandleDesc
.methodRefEntry
(ClassEntry owner, NameAndTypeEntry nameAndType) default MethodRefEntry
methodRefEntry
(ClassDesc owner, String name, MethodTypeDesc type) Returns aMethodRefEntry
describing a method of a class.methodTypeEntry
(Utf8Entry descriptor) Returns aMethodTypeEntry
referring to aUtf8Entry
.methodTypeEntry
(MethodTypeDesc descriptor) Returns aMethodTypeEntry
describing the same method type as the providedMethodTypeDesc
.moduleEntry
(Utf8Entry moduleName) Returns aModuleEntry
referring to the providedUtf8Entry
.default ModuleEntry
moduleEntry
(ModuleDesc moduleDesc) Returns aModuleEntry
describing the same module as the providedModuleDesc
.nameAndTypeEntry
(Utf8Entry nameEntry, Utf8Entry typeEntry) Returns aNameAndTypeEntry
referring to the provided name and typeUtf8Entry
.default NameAndTypeEntry
nameAndTypeEntry
(String name, ClassDesc type) Returns aNameAndTypeEntry
describing the provided unqualified name and field descriptor.default NameAndTypeEntry
nameAndTypeEntry
(String name, MethodTypeDesc type) Returns aNameAndTypeEntry
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
(Utf8Entry nameEntry) Returns aPackageEntry
referring to the providedUtf8Entry
.default PackageEntry
packageEntry
(PackageDesc packageDesc) Returns aPackageEntry
describing the same package as the providedPackageDesc
.stringEntry
(Utf8Entry utf8) Returns aStringEntry
referring to aUtf8Entry
.default StringEntry
stringEntry
(String value) Returns aStringEntry
describing the provided value.default Utf8Entry
default Utf8Entry
utf8Entry
(MethodTypeDesc desc) Methods inherited from interface ConstantPool
bootstrapMethodCount, bootstrapMethodEntry, entryByIndex, entryByIndex, iterator, size
Methods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
of
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 thesize()
of the source pool.- Parameters:
classModel
- the class to copy from- Returns:
- a new constant pool builder
- See Also:
-
of
Returns a new constant pool builder. The new constant pool builder will be empty. The index of new entries will start from1
.- Returns:
- a new constant pool builder
- See Also:
-
canWriteDirect
Returnstrue
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
- API Note:
- The resulting
Utf8Entry
is usually not referable by aClassEntry
, 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 providedClassDesc
-
utf8Entry
- Parameters:
desc
- the symbolic descriptor for the method type- Returns:
- a
Utf8Entry
describing the method descriptor string of the providedMethodTypeDesc
-
classEntry
Returns aClassEntry
referring to the providedUtf8Entry
. TheUtf8Entry
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
ClassEntry
referring to the providedUtf8Entry
- See Also:
-
classEntry
Returns aClassEntry
describing the same reference type as the providedClassDesc
.- Parameters:
classDesc
- the symbolic descriptor for the reference type- Returns:
- a
ClassEntry
describing the same reference type as the providedClassDesc
- Throws:
IllegalArgumentException
- ifclassDesc
represents a primitive type- See Also:
-
packageEntry
Returns aPackageEntry
referring to the providedUtf8Entry
. TheUtf8Entry
describes the internal form of the name of a package.- Parameters:
nameEntry
- theUtf8Entry
- Returns:
- a
PackageEntry
referring to the providedUtf8Entry
- See Also:
-
packageEntry
Returns aPackageEntry
describing the same package as the providedPackageDesc
.- Parameters:
packageDesc
- the symbolic descriptor for the package- Returns:
- a
PackageEntry
describing the same package as the providedPackageDesc
- See Also:
-
moduleEntry
- Parameters:
moduleName
- the constant pool entry describing the module name- Returns:
- a
ModuleEntry
referring to the providedUtf8Entry
- See Also:
-
moduleEntry
Returns aModuleEntry
describing the same module as the providedModuleDesc
.- Parameters:
moduleDesc
- the symbolic descriptor for the module- Returns:
- a
ModuleEntry
describing the same module as the providedModuleDesc
- See Also:
-
nameAndTypeEntry
Returns aNameAndTypeEntry
referring to the provided name and typeUtf8Entry
. The nameUtf8Entry
describes an unqualified name or the special name "<init>", and the typeUtf8Entry
describes a field or method descriptor string.- Parameters:
nameEntry
- the nameUtf8Entry
typeEntry
- the typeUtf8Entry
- Returns:
- a
NameAndTypeEntry
referring to the provided name and typeUtf8Entry
- See Also:
-
nameAndTypeEntry
Returns aNameAndTypeEntry
describing the provided unqualified name and field descriptor.- Parameters:
name
- the unqualified nametype
- the field descriptor- Returns:
- a
NameAndTypeEntry
describing the provided unqualified name and field descriptor
-
nameAndTypeEntry
Returns aNameAndTypeEntry
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
Returns aFieldRefEntry
referring to aClassEntry
and aNameAndTypeEntry
. TheClassEntry
describes a class or interface that has this field as a member, and theNameAndTypeEntry
describes the unqualified name and the field descriptor for this field.- Parameters:
owner
- theClassEntry
nameAndType
- theNameAndTypeEntry
- Returns:
- a
FieldRefEntry
referring to aClassEntry
and aNameAndTypeEntry
- See Also:
-
fieldRefEntry
Returns aFieldRefEntry
describing a field of a class.- Parameters:
owner
- the class or interface the field is a member ofname
- the unqualified name of the fieldtype
- the field descriptor- Returns:
- a
FieldRefEntry
describing a field of a class - Throws:
IllegalArgumentException
- ifowner
represents a primitive type- See Also:
-
methodRefEntry
Returns aMethodRefEntry
referring to aClassEntry
and aNameAndTypeEntry
. TheClassEntry
describes a class that has this method as a member, and theNameAndTypeEntry
describes the unqualified name or the special name "<init>" and the method descriptor for this method.- Parameters:
owner
- theClassEntry
nameAndType
- theNameAndTypeEntry
- Returns:
- a
MethodRefEntry
referring to aClassEntry
and aNameAndTypeEntry
- See Also:
-
methodRefEntry
Returns aMethodRefEntry
describing a method of a class.- Parameters:
owner
- the class the method is a member ofname
- the unqualified name, or special name "<init>", of the methodtype
- the method descriptor- Returns:
- a
MethodRefEntry
describing a method of a class - Throws:
IllegalArgumentException
- ifowner
represents a primitive type- See Also:
-
interfaceMethodRefEntry
Returns anInterfaceMethodRefEntry
referring to aClassEntry
and aNameAndTypeEntry
. TheClassEntry
describes an interface that has this method as a member, and theNameAndTypeEntry
describes the unqualified name and the method descriptor for this method.- Parameters:
owner
- theClassEntry
nameAndType
- theNameAndTypeEntry
- Returns:
- an
InterfaceMethodRefEntry
referring to aClassEntry
and aNameAndTypeEntry
- See Also:
-
interfaceMethodRefEntry
default InterfaceMethodRefEntry interfaceMethodRefEntry(ClassDesc owner, String name, MethodTypeDesc type) Returns anInterfaceMethodRefEntry
describing a method of an interface.- Parameters:
owner
- the interface the method is a member ofname
- the unqualified name of the methodtype
- the method descriptor- Returns:
- an
InterfaceMethodRefEntry
describing a method of an interface - Throws:
IllegalArgumentException
- ifowner
represents a primitive type- See Also:
-
methodTypeEntry
Returns aMethodTypeEntry
describing the same method type as the providedMethodTypeDesc
.- Parameters:
descriptor
- the symbolic descriptor of the method type- Returns:
- a
MethodTypeEntry
describing the same method type as the providedMethodTypeDesc
- See Also:
-
methodTypeEntry
Returns aMethodTypeEntry
referring to aUtf8Entry
. TheUtf8Entry
represents a method descriptor string.- Parameters:
descriptor
- theUtf8Entry
- Returns:
- a
MethodTypeEntry
referring to aUtf8Entry
- See Also:
-
methodHandleEntry
Returns aMethodHandleEntry
describing the same method handle as the givenDirectMethodHandleDesc
.- Parameters:
descriptor
- the symbolic descriptor of the method handle- Returns:
- a
MethodHandleEntry
describing the same method handle as the givenDirectMethodHandleDesc
- See Also:
-
methodHandleEntry
Returns aMethodHandleEntry
encoding a reference kind and referring to aMemberRefEntry
. The reference kind must be in[1, 9]
, and theMemberRefEntry
is subject to various restrictions based on the reference kind (JVMS 4.4.8).- Parameters:
refKind
- the reference kind of the method handlereference
- theMemberRefEntry
- Returns:
- a
MethodHandleEntry
encoding a reference kind and referring to aMemberRefEntry
- See Also:
-
invokeDynamicEntry
Returns anInvokeDynamicEntry
describing the same dynamic call site as the providedDynamicCallSiteDesc
.- Parameters:
dcsd
- the symbolic descriptor of the dynamic call site- Returns:
- an
InvokeDynamicEntry
describing the same dynamic call site as the providedDynamicCallSiteDesc
- See Also:
-
invokeDynamicEntry
InvokeDynamicEntry invokeDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType) Returns anInvokeDynamicEntry
referring to aBootstrapMethodEntry
and aNameAndTypeEntry
. TheBootstrapMethodEntry
describes the bootstrap method and its invocation arguments in addition to the name and type, and theNameAndTypeEntry
a name and a method descriptor.- Parameters:
bootstrapMethodEntry
- theBootstrapMethodEntry
nameAndType
- theNameAndTypeEntry
- Returns:
- an
InvokeDynamicEntry
referring to aBootstrapMethodEntry
and aNameAndTypeEntry
- See Also:
-
constantDynamicEntry
Returns aConstantDynamicEntry
describing the dynamic constant as the providedDynamicConstantDesc
.- Parameters:
dcd
- the symbolic descriptor of the constant- Returns:
- a
ConstantDynamicEntry
describing the dynamic constant as the providedDynamicConstantDesc
- See Also:
-
constantDynamicEntry
ConstantDynamicEntry constantDynamicEntry(BootstrapMethodEntry bootstrapMethodEntry, NameAndTypeEntry nameAndType) Returns aConstantDynamicEntry
referring to aBootstrapMethodEntry
and aNameAndTypeEntry
. TheBootstrapMethodEntry
describes the bootstrap method and its invocation arguments in addition to the name and type, and theNameAndTypeEntry
a name and a field descriptor.- Parameters:
bootstrapMethodEntry
- theBootstrapMethodEntry
nameAndType
- theNameAndTypeEntry
- Returns:
- a
ConstantDynamicEntry
referring to aBootstrapMethodEntry
and aNameAndTypeEntry
- See Also:
-
intEntry
Returns anIntegerEntry
describing the provided value.- Parameters:
value
- the value- Returns:
- an
IntegerEntry
describing the provided value - See Also:
-
floatEntry
Returns aFloatEntry
describing the provided value.- Parameters:
value
- the value- Returns:
- a
FloatEntry
describing the provided value - See Also:
-
longEntry
-
doubleEntry
Returns aDoubleEntry
describing the provided value.- Parameters:
value
- the value- Returns:
- a
DoubleEntry
describing the provided value - See Also:
-
stringEntry
- Parameters:
utf8
- theUtf8Entry
- Returns:
- a
StringEntry
referring to aUtf8Entry
- See Also:
-
stringEntry
Returns aStringEntry
describing the provided value.- Parameters:
value
- the value- Returns:
- a
StringEntry
describing the provided value - See Also:
-
constantValueEntry
Returns aConstantValueEntry
describing the provided constantInteger
,Long
,Float
,Double
, orString
value.- Parameters:
c
- the provided constant value- Returns:
- a
ConstantValueEntry
describing the provided constantInteger
,Long
,Float
,Double
, orString
value - Throws:
IllegalArgumentException
- if the value is not one ofInteger
,Long
,Float
,Double
, orString
- See Also:
-
loadableConstantEntry
Returns aLoadableConstantEntry
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 aBootstrapMethodEntry
describing the provided bootstrap method and arguments.- Parameters:
methodReference
- the bootstrap methodarguments
- the arguments- Returns:
- a
BootstrapMethodEntry
describing the provided bootstrap method and arguments
-
bsmEntry
BootstrapMethodEntry bsmEntry(MethodHandleEntry methodReference, List<LoadableConstantEntry> arguments) Returns aBootstrapMethodEntry
referring to aMethodHandleEntry
and a list ofLoadableConstantEntry
. TheMethodHandleEntry
is the bootstrap method, and the list ofLoadableConstantEntry
is the arguments.- Parameters:
methodReference
- theMethodHandleEntry
arguments
- the list ofLoadableConstantEntry
- Returns:
- a
BootstrapMethodEntry
referring to aMethodHandleEntry
and a list ofLoadableConstantEntry
- See Also:
-