Class ConstantPool
java.lang.Object
org.apache.bcel.classfile.ConstantPool
This class represents the constant pool, that is, a table of constants, of a parsed classfile. It may contain null references, due to the JVM specification that
skips an entry after an 8-byte constant (double, long) entry. Those interested in generating constant pools programmatically should see
ConstantPoolGen.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstantPool(DataInput input) Reads constants from given input stream.ConstantPool(Constant... constantPool) Constructs a ConstantPool. -
Method Summary
Modifier and TypeMethodDescriptionvoidCalled by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.constantToString(int index, byte tag) Retrieves constant at 'index' from constant pool and resolve it to a string representation.Resolves constant to a string representation.copy()Creates a deep copy of this constant pool.voiddump(DataOutputStream file) Dumps constant pool to file stream in binary format.<T extends Constant>
TgetConstant(int index) Gets constant from constant pool.<T extends Constant>
TgetConstant(int index, byte tag) Gets constant from constant pool and check whether it has the expected type.<T extends Constant>
TgetConstant(int index, byte tag, Class<T> castTo) Gets constant from constant pool and check whether it has the expected type.<T extends Constant>
TgetConstant(int index, Class<T> castTo) Gets constant from constant pool.getConstantInteger(int index) Gets constant from constant pool and check whether it has the expected type.Constant[]Gets the array of constants.getConstantString(int index, byte tag) Gets string from constant pool and bypass the indirection of 'ConstantClass' and 'ConstantString' objects.getConstantUtf8(int index) Gets constant from constant pool and check whether it has the expected type.intGets the length of constant pool.iterator()voidsetConstant(int index, Constant constant) Sets a constant at the specified index.voidsetConstantPool(Constant[] constantPool) Sets the constant pool.toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Constructor Details
-
ConstantPool
Constructs a ConstantPool.- Parameters:
constantPool- Array of constants.
-
ConstantPool
Reads constants from given input stream.- Parameters:
input- Input stream.- Throws:
IOException- if problem in readUnsignedShort or readConstant.
-
-
Method Details
-
accept
-
constantToString
Resolves constant to a string representation.- Parameters:
c- Constant to be printed.- Returns:
- String representation.
- Throws:
IllegalArgumentException- if c is unknown constant type.
-
constantToString
Retrieves constant at 'index' from constant pool and resolve it to a string representation.- Parameters:
index- of constant in constant pool.tag- expected type.- Returns:
- String representation.
-
copy
Creates a deep copy of this constant pool.- Returns:
- deep copy of this constant pool.
-
dump
Dumps constant pool to file stream in binary format.- Parameters:
file- Output file stream.- Throws:
IOException- if problem in writeShort or dump
-
getConstant
Gets constant from constant pool.- Type Parameters:
T- the type of the constant.- Parameters:
index- Index in constant pool.- Returns:
- Constant value.
- Throws:
ClassFormatException- if index is invalid.- See Also:
-
getConstant
Gets constant from constant pool and check whether it has the expected type.- Type Parameters:
T- the type of the constant.- Parameters:
index- Index in constant pool.tag- Tag of expected constant, that is, its type.- Returns:
- Constant value.
- Throws:
ClassFormatException- if constant type does not match tag.- See Also:
-
getConstant
public <T extends Constant> T getConstant(int index, byte tag, Class<T> castTo) throws ClassFormatException Gets constant from constant pool and check whether it has the expected type.- Type Parameters:
T- the type of the constant.- Parameters:
index- Index in constant pool.tag- Tag of expected constant, that is, its type.castTo- the class to cast to.- Returns:
- Constant value.
- Throws:
ClassFormatException- if constant type does not match tag.- Since:
- 6.6.0
- See Also:
-
getConstant
Gets constant from constant pool.- Type Parameters:
T- AConstantsubclass.- Parameters:
index- Index in constant pool.castTo- TheConstantsubclass to cast to.- Returns:
- Constant value.
- Throws:
ClassFormatException- if index is invalid.- Since:
- 6.6.0
- See Also:
-
getConstantInteger
Gets constant from constant pool and check whether it has the expected type.- Parameters:
index- Index in constant pool.- Returns:
- ConstantInteger value.
- Throws:
ClassFormatException- if constant type does not match tag.- See Also:
-
getConstantPool
Gets the array of constants.- Returns:
- Array of constants.
- See Also:
-
getConstantString
Gets string from constant pool and bypass the indirection of 'ConstantClass' and 'ConstantString' objects. I.e. these classes have an index field that points to another entry of the constant pool of type 'ConstantUtf8' which contains the real data.- Parameters:
index- Index in constant pool.tag- Tag of expected constant, either ConstantClass or ConstantString.- Returns:
- Contents of string reference.
- Throws:
IllegalArgumentException- if tag is invalid.- See Also:
-
getConstantUtf8
Gets constant from constant pool and check whether it has the expected type.- Parameters:
index- Index in constant pool.- Returns:
- ConstantUtf8 value.
- Throws:
ClassFormatException- if constant type does not match tag.- See Also:
-
getLength
-
iterator
-
setConstant
Sets a constant at the specified index.- Parameters:
index- the index in the constant pool.constant- Constant to set.
-
setConstantPool
Sets the constant pool.- Parameters:
constantPool- the constant pool array.
-
toString
-