Class ConstantPoolGen
java.lang.Object
org.apache.bcel.generic.ConstantPoolGen
This class is used to build up a constant pool. The user adds constants via 'addXXX' methods, 'addString', 'addClass', and so on. These methods return an
index into the constant pool. Finally, 'getFinalConstantPool()' returns the constant pool built up. Intermediate versions of the constant pool can be
obtained with 'getConstantPool()'. A constant pool has capacity for Constants.MAX_SHORT entries. Note that the first (0) is used by the JVM and that Double
and Long constants need two slots.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Constant[]Deprecated.(since 6.0) will be made private; do not access directly, use getter/setterprotected intDeprecated.(since 6.0) will be made private; do not access directly, use getSize()protected intDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new empty constant pool.ConstantPoolGen(Constant[] cs) Constructs a new instance with the given array of constants.Constructs a new instance with the given constant pool. -
Method Summary
Modifier and TypeMethodDescriptionintaddArrayClass(ArrayType type) Add a reference to an array class (for example, String[][]) as needed by MULTIANEWARRAY instruction, for example, to the ConstantPool.intAdd a new Class reference to the ConstantPool, if it is not already in there.intaddClass(ObjectType type) Add a new Class reference to the ConstantPool for a given type.intaddConstant(Constant constant, ConstantPoolGen cpGen) Adds a constant from another ConstantPool and returns the new index.intaddDouble(double n) Add a new double constant to the ConstantPool, if it is not already in there.intaddFieldref(String className, String fieldName, String signature) Add a new Fieldref constant to the ConstantPool, if it is not already in there.intaddFloat(float n) Add a new Float constant to the ConstantPool, if it is not already in there.intaddInteger(int n) Add a new Integer constant to the ConstantPool, if it is not already in there.intaddInterfaceMethodref(String className, String methodName, String signature) Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.intaddInterfaceMethodref(MethodGen method) Adds a new InterfaceMethodref constant to the ConstantPool.intaddLong(long n) Add a new long constant to the ConstantPool, if it is not already in there.intaddMethodref(String className, String methodName, String signature) Add a new Methodref constant to the ConstantPool, if it is not already in there.intaddMethodref(MethodGen method) Adds a new Methodref constant to the ConstantPool.intaddNameAndType(String name, String signature) Add a new NameAndType constant to the ConstantPool if it is not already in there.intAdd a new String constant to the ConstantPool, if it is not already in there.intAdd a new Utf8 constant to the ConstantPool, if it is not already in there.protected voidResize internal array of constants.getConstant(int i) Gets a constant pool entry at the specified index.Gets the intermediate constant pool.Gets the constant pool with proper length.intgetSize()Gets the current size of constant pool.intlookupClass(String str) Look for ConstantClass in ConstantPool named 'str'.intlookupDouble(double n) Look for ConstantDouble in ConstantPool.intlookupFieldref(String className, String fieldName, String signature) Look for ConstantFieldref in ConstantPool.intlookupFloat(float n) Look for ConstantFloat in ConstantPool.intlookupInteger(int n) Look for ConstantInteger in ConstantPool.intlookupInterfaceMethodref(String className, String methodName, String signature) Look for ConstantInterfaceMethodref in ConstantPool.intlookupInterfaceMethodref(MethodGen method) Looks up an InterfaceMethodref in the ConstantPool.intlookupLong(long n) Look for ConstantLong in ConstantPool.intlookupMethodref(String className, String methodName, String signature) Look for ConstantMethodref in ConstantPool.intlookupMethodref(MethodGen method) Looks up a Methodref in the ConstantPool.intlookupNameAndType(String name, String signature) Look for ConstantNameAndType in ConstantPool.intlookupString(String str) Look for ConstantString in ConstantPool containing String 'str'.intlookupUtf8(String n) Look for ConstantUtf8 in ConstantPool.voidsetConstant(int i, Constant c) Use with care!toString()
-
Field Details
-
size
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter -
constants
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter -
index
Deprecated.(since 6.0) will be made private; do not access directly, use getSize()
-
-
Constructor Details
-
ConstantPoolGen
public ConstantPoolGen()Constructs a new empty constant pool. -
ConstantPoolGen
Constructs a new instance with the given array of constants.- Parameters:
cs- array of given constants, new ones will be appended.
-
ConstantPoolGen
Constructs a new instance with the given constant pool.- Parameters:
cp- the constant pool.
-
-
Method Details
-
addArrayClass
Add a reference to an array class (for example, String[][]) as needed by MULTIANEWARRAY instruction, for example, to the ConstantPool.- Parameters:
type- type of array class.- Returns:
- index of entry.
-
addClass
Add a new Class reference to the ConstantPool for a given type.- Parameters:
type- Class to add.- Returns:
- index of entry.
-
addClass
-
addConstant
Adds a constant from another ConstantPool and returns the new index.- Parameters:
constant- The constant to add.cpGen- Source pool.- Returns:
- index of entry.
-
addDouble
Add a new double constant to the ConstantPool, if it is not already in there.- Parameters:
n- Double number to add.- Returns:
- index of entry.
-
addFieldref
Add a new Fieldref constant to the ConstantPool, if it is not already in there.- Parameters:
className- class name string to add.fieldName- field name string to add.signature- signature string to add.- Returns:
- index of entry.
-
addFloat
Add a new Float constant to the ConstantPool, if it is not already in there.- Parameters:
n- Float number to add.- Returns:
- index of entry.
-
addInteger
Add a new Integer constant to the ConstantPool, if it is not already in there.- Parameters:
n- integer number to add.- Returns:
- index of entry.
-
addInterfaceMethodref
Adds a new InterfaceMethodref constant to the ConstantPool.- Parameters:
method- the method to add.- Returns:
- index of entry.
-
addInterfaceMethodref
Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.- Parameters:
className- class name string to add.methodName- method name string to add.signature- signature string to add.- Returns:
- index of entry.
-
addLong
Add a new long constant to the ConstantPool, if it is not already in there.- Parameters:
n- Long number to add.- Returns:
- index of entry.
-
addMethodref
Adds a new Methodref constant to the ConstantPool.- Parameters:
method- the method to add.- Returns:
- index of entry.
-
addMethodref
Add a new Methodref constant to the ConstantPool, if it is not already in there.- Parameters:
className- class name string to add.methodName- method name string to add.signature- method signature string to add.- Returns:
- index of entry.
-
addNameAndType
Add a new NameAndType constant to the ConstantPool if it is not already in there.- Parameters:
name- Name string to add.signature- signature string to add.- Returns:
- index of entry.
-
addString
-
addUtf8
-
adjustSize
Resize internal array of constants. -
getConstant
Gets a constant pool entry at the specified index.- Parameters:
i- index in constant pool.- Returns:
- constant pool entry at index i.
-
getConstantPool
Gets the intermediate constant pool.- Returns:
- intermediate constant pool.
-
getFinalConstantPool
Gets the constant pool with proper length.- Returns:
- constant pool with proper length.
-
getSize
Gets the current size of constant pool.- Returns:
- current size of constant pool.
-
lookupClass
Look for ConstantClass in ConstantPool named 'str'.- Parameters:
str- String to search for.- Returns:
- index on success, -1 otherwise.
-
lookupDouble
Look for ConstantDouble in ConstantPool.- Parameters:
n- Double number to look for.- Returns:
- index on success, -1 otherwise.
-
lookupFieldref
Look for ConstantFieldref in ConstantPool.- Parameters:
className- Where to find method.fieldName- Guess what.signature- return and argument types.- Returns:
- index on success, -1 otherwise.
-
lookupFloat
Look for ConstantFloat in ConstantPool.- Parameters:
n- Float number to look for.- Returns:
- index on success, -1 otherwise.
-
lookupInteger
Look for ConstantInteger in ConstantPool.- Parameters:
n- integer number to look for.- Returns:
- index on success, -1 otherwise.
-
lookupInterfaceMethodref
Looks up an InterfaceMethodref in the ConstantPool.- Parameters:
method- the method to look for.- Returns:
- index on success, -1 otherwise.
-
lookupInterfaceMethodref
Look for ConstantInterfaceMethodref in ConstantPool.- Parameters:
className- Where to find method.methodName- Guess what.signature- return and argument types.- Returns:
- index on success, -1 otherwise.
-
lookupLong
Look for ConstantLong in ConstantPool.- Parameters:
n- Long number to look for.- Returns:
- index on success, -1 otherwise.
-
lookupMethodref
Looks up a Methodref in the ConstantPool.- Parameters:
method- the method to look for.- Returns:
- index on success, -1 otherwise.
-
lookupMethodref
Look for ConstantMethodref in ConstantPool.- Parameters:
className- Where to find method.methodName- Guess what.signature- return and argument types.- Returns:
- index on success, -1 otherwise.
-
lookupNameAndType
Look for ConstantNameAndType in ConstantPool.- Parameters:
name- of variable/method.signature- of variable/method.- Returns:
- index on success, -1 otherwise.
-
lookupString
Look for ConstantString in ConstantPool containing String 'str'.- Parameters:
str- String to search for.- Returns:
- index on success, -1 otherwise.
-
lookupUtf8
Look for ConstantUtf8 in ConstantPool.- Parameters:
n- Utf8 string to look for.- Returns:
- index on success, -1 otherwise.
-
setConstant
Use with care!- Parameters:
i- index in constant pool.c- new constant pool entry at index i.
-
toString
-