java.lang.classfile library.java.lang.classfile.constantpool package contains interfaces describing constant pool entries in the
class file format. Constant pool entries are low-level models to faithfully represent the exact structure
of a class file.
APIs in this package perform null and unrepresentable argument checks, unless otherwise noted.
class files, the pool entries are lazily inflated; the contents of these entries, besides the
bare structure, are not evaluated to speed up parsing. Entries to users interest, usually accessed from other models
and elements, have their contents read on demand. For example, to search for methods, a user should filter first by
access flags and then by method name, and use Utf8Entry#equalsString(String) instead of checking equality
against Utf8Entry#stringValue(). This avoids inflation of UTF-8 entries as much as possible:
boolean isStaticWorkMethod(MethodModel method) { // check static flag first to avoid unnecessary evaluation of UTF-8 entry return (method.flags().flagsMask() & ClassFile.ACC_STATIC) != 0 // use equalsString to avoid full conversion to String for comparison // the Utf8Entry can also act as a basic CharSequence without full conversion && method.methodName().equalsString("work"); }
The entries also define accessors to validated symbolic information with nominal descriptor abstractions from the
java. package. These symbolic information accessors perform validation against the read
class files, and throw IllegalArgumentException when the accessed constant pool entry contains
invalid data. The nominal descriptors represent validated data, which saves users from extra validations in future
processing.
Due to the lazy nature of class file parsing, IllegalArgumentException indicating malformed
class file data can be thrown at any method invocation. For example, an exception may come from a ClassEntry when it is first read from the constant pool (referring to an invalid index or wrong type of entry), when
its referred UTF-8 entry is expanded (malformed UTF-8 data), or when its symbolic information is accessed (the string
is not valid for a class entry).
class files. Most Class-File API models and elements have two sets of factory methods: one that accepts symbolic
information representing the uses, and another that accepts constant pool entries. The constant pool builder
associated with class file builders, ClassFileBuilder#constantPool, automatically creates or reuses
pool entries from the symbolic information. Validated data in symbolic information helps class file
generation by avoiding extraneous parsing of raw constant pool entry data.
As always, users can use factories that accept constant pool entries if they already have them by hand, or if they
desire fine-grained control over class file generation.
If many models and elements are reused from another ClassModel in class building, the class building process
can use a constant pool builder that extends from the given ClassModel, available through ConstantPoolBuilder::of(ClassModel), so that byte data with constant pool
references can be copied in batch, speeding up class building. This is especially applicable to class transformations,
and ConstantPoolSharingOption exists to control this behavior.
| Modifier and Type | Interface and Description |
|---|---|
| public interface | AnnotationConstantValueEntry
Marker interface for constant pool entries that can represent constant values associated with elements of annotations. |
| public interface | ClassEntry
Models a |
| public interface | ConstantDynamicEntry
Models a |
| public interface | ConstantPool
Provides read access to the constant pool and the bootstrap method table of a
|
| public interface | ConstantPoolBuilder
Builder for the constant pool of a |
| public interface | ConstantValueEntry
Marker interface for constant pool entries that can represent constant values
in the |
| public interface | DoubleEntry
Models a |
| public interface | DynamicConstantPoolEntry
Superinterface modeling dynamically-computed constant pool entries, which
include |
| public interface | FieldRefEntry
Models a |
| public interface | FloatEntry
Models a |
| public interface | IntegerEntry
Models a |
| public interface | InterfaceMethodRefEntry
Models a |
| public interface | InvokeDynamicEntry
Models a |
| public interface | LoadableConstantEntry
Marker interface for constant pool entries suitable for loading via the
|
| public interface | LongEntry
Models a |
| public interface | MemberRefEntry
Superinterface modeling symbolic references to a member of a class or interface
in the constant pool of a |
| public interface | MethodHandleEntry
Models a |
| public interface | MethodRefEntry
Models a |
| public interface | MethodTypeEntry
Models a |
| public interface | ModuleEntry
Models a |
| public interface | NameAndTypeEntry
Models a |
| public interface | PackageEntry
Models a |
| public interface | PoolEntry
Models an entry in the constant pool of a |
| public interface | StringEntry
Models a |
| public interface | Utf8Entry
Models a |
| Modifier and Type | Class and Description |
|---|---|
| public class | ConstantPoolException
Thrown to indicate that requested entry cannot be obtained from the constant pool or the bootstrap method table. |