Package org.apache.bcel.generic
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', etc.. 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
Modifier and TypeFieldDescriptionprotected Constant[]
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setterprotected int
Deprecated.(since 6.0) will be made private; do not access directly, use getSize()protected int
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter -
Constructor Summary
ConstructorDescriptionConstructs 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 TypeMethodDescriptionint
addArrayClass
(ArrayType type) Add a reference to an array class (e.g.int
Add a new Class reference to the ConstantPool, if it is not already in there.int
addClass
(ObjectType type) Add a new Class reference to the ConstantPool for a given type.int
addConstant
(Constant constant, ConstantPoolGen cpGen) Adds a constant from another ConstantPool and returns the new index.int
addDouble
(double n) Add a new double constant to the ConstantPool, if it is not already in there.int
addFieldref
(String className, String fieldName, String signature) Add a new Fieldref constant to the ConstantPool, if it is not already in there.int
addFloat
(float n) Add a new Float constant to the ConstantPool, if it is not already in there.int
addInteger
(int n) Add a new Integer constant to the ConstantPool, if it is not already in there.int
addInterfaceMethodref
(String className, String methodName, String signature) Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.int
addInterfaceMethodref
(MethodGen method) int
addLong
(long n) Add a new long constant to the ConstantPool, if it is not already in there.int
addMethodref
(String className, String methodName, String signature) Add a new Methodref constant to the ConstantPool, if it is not already in there.int
addMethodref
(MethodGen method) int
addNameAndType
(String name, String signature) Add a new NameAndType constant to the ConstantPool if it is not already in there.int
Add a new String constant to the ConstantPool, if it is not already in there.int
Add a new Utf8 constant to the ConstantPool, if it is not already in there.protected void
Resize internal array of constants.getConstant
(int i) int
getSize()
int
lookupClass
(String str) Look for ConstantClass in ConstantPool named 'str'.int
lookupDouble
(double n) Look for ConstantDouble in ConstantPool.int
lookupFieldref
(String className, String fieldName, String signature) Look for ConstantFieldref in ConstantPool.int
lookupFloat
(float n) Look for ConstantFloat in ConstantPool.int
lookupInteger
(int n) Look for ConstantInteger in ConstantPool.int
lookupInterfaceMethodref
(String className, String methodName, String signature) Look for ConstantInterfaceMethodref in ConstantPool.int
lookupInterfaceMethodref
(MethodGen method) int
lookupLong
(long n) Look for ConstantLong in ConstantPool.int
lookupMethodref
(String className, String methodName, String signature) Look for ConstantMethodref in ConstantPool.int
lookupMethodref
(MethodGen method) int
lookupNameAndType
(String name, String signature) Look for ConstantNameAndType in ConstantPool.int
lookupString
(String str) Look for ConstantString in ConstantPool containing String 'str'.int
lookupUtf8
(String n) Look for ConstantUtf8 in ConstantPool.void
setConstant
(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 (e.g. String[][]) as needed by MULTIANEWARRAY instruction, e.g. 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
Add a new Class reference to the ConstantPool, if it is not already in there.- Parameters:
str
- Class to add- Returns:
- index of entry
-
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 addfieldName
- field name string to addsignature
- 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
-
addInterfaceMethodref
Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.- Parameters:
className
- class name string to addmethodName
- method name string to addsignature
- 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
-
addMethodref
Add a new Methodref constant to the ConstantPool, if it is not already in there.- Parameters:
className
- class name string to addmethodName
- method name string to addsignature
- 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 addsignature
- signature string to add- Returns:
- index of entry
-
addString
Add a new String constant to the ConstantPool, if it is not already in there.- Parameters:
str
- String to add- Returns:
- index of entry
-
addUtf8
Add a new Utf8 constant to the ConstantPool, if it is not already in there.- Parameters:
n
- Utf8 string to add- Returns:
- index of entry
-
adjustSize
Resize internal array of constants. -
getConstant
- Parameters:
i
- index in constant pool- Returns:
- constant pool entry at index i
-
getConstantPool
- Returns:
- intermediate constant pool
-
getFinalConstantPool
- Returns:
- constant pool with proper length
-
getSize
- 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 methodfieldName
- Guess whatsignature
- 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
-
lookupInterfaceMethodref
Look for ConstantInterfaceMethodref in ConstantPool.- Parameters:
className
- Where to find methodmethodName
- Guess whatsignature
- 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
-
lookupMethodref
Look for ConstantMethodref in ConstantPool.- Parameters:
className
- Where to find methodmethodName
- Guess whatsignature
- return and argument types- Returns:
- index on success, -1 otherwise
-
lookupNameAndType
Look for ConstantNameAndType in ConstantPool.- Parameters:
name
- of variable/methodsignature
- 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 poolc
- new constant pool entry at index i
-
toString
-