final class ConstantPool extends java.lang.Object implements WritableData
Classes which access the constant pool to add constants must do so via
the get()
method. This method will accept an instance of
a Constant
subclass. If an equivalent object alreay exists
in the pool, it is returned. If not, the specified object is added to
the pool, then returned from the method. This mechanism preserves the
integrity of the pool, such that two equivalent constants are never
present.
The constant pool is written to an output stream as part of the class
file compilation. This process causes all constants to be indexed,
during which each is assigned a unique index associated with its
ordered position within the pool. Note that longs
and
doubles
take up two slots each; all other constants
take up one slot. It is these indices which are used to refer to a
particular constant from within the bytecode of Java methods.
Modifier and Type | Field and Description |
---|---|
private java.util.SortedMap |
constantMap
Map of all
Constant subclass instances. |
private int |
size
Size (in 32-bit slots) of the constant pool.
|
Constructor and Description |
---|
ConstantPool()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
(package private) Constant |
get(Constant constant)
This method checks whether the specified constant is already in the
pool.
|
private void |
indexPool()
This method is called just before writing the pool to an output
stream, presumably to create a Java class file.
|
static void |
main(java.lang.String[] args)
Test harness entry point.
|
void |
write(java.io.DataOutput out)
This method indexes the constant pool, which is already sorted in the
natural order of its constant objects, then writes the constant pool
data to the specified output stream, presumably as part of the
creation of a Java class file.
|
private java.util.SortedMap constantMap
Constant
subclass instances.private int size
Constant get(Constant constant)
constant
- Instance of the Constant
subclass. This
parameter is both used as a key to check for an equivalent
constant already in the pool, and as a constant instance
to add to the pool if an equivalent one is not yet present.constant
, if it was added to the
pool, else the equivalent constant which was already in the
pool.private void indexPool()
public void write(java.io.DataOutput out) throws java.io.IOException
write
in interface WritableData
out
- Output stream to which to write constant pool data.java.io.IOException
- If any error is encountered while writing the data.public static void main(java.lang.String[] args)
args
- Main function argumens.