Package org.apache.bcel.generic
Class ConstantPoolGen
- java.lang.Object
-
- org.apache.bcel.generic.ConstantPoolGen
-
public class ConstantPoolGen extends java.lang.ObjectThis 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:
Constant
-
-
Field Summary
Fields Modifier and Type Field Description protected Constant[]constantsDeprecated.(since 6.0) will be made private; do not access directly, use getter/setterprotected intindexDeprecated.(since 6.0) will be made private; do not access directly, use getSize()protected intsizeDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter
-
Constructor Summary
Constructors Constructor Description ConstantPoolGen()Create empty constant pool.ConstantPoolGen(Constant[] cs)Initialize with given array of constants.ConstantPoolGen(ConstantPool cp)Initialize with given constant pool.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddArrayClass(ArrayType type)Add a reference to an array class (e.g.intaddClass(java.lang.String str)Add 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 c, ConstantPoolGen cp)Import constant from another ConstantPool and return new index.intaddDouble(double n)Add a new double constant to the ConstantPool, if it is not already in there.intaddFieldref(java.lang.String class_name, java.lang.String field_name, java.lang.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(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.intaddInterfaceMethodref(MethodGen method)intaddLong(long n)Add a new long constant to the ConstantPool, if it is not already in there.intaddMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Add a new Methodref constant to the ConstantPool, if it is not already in there.intaddMethodref(MethodGen method)intaddNameAndType(java.lang.String name, java.lang.String signature)Add a new NameAndType constant to the ConstantPool if it is not already in there.intaddString(java.lang.String str)Add a new String constant to the ConstantPool, if it is not already in there.intaddUtf8(java.lang.String n)Add a new Utf8 constant to the ConstantPool, if it is not already in there.protected voidadjustSize()Resize internal array of constants.ConstantgetConstant(int i)ConstantPoolgetConstantPool()ConstantPoolgetFinalConstantPool()intgetSize()intlookupClass(java.lang.String str)Look for ConstantClass in ConstantPool named `str'.intlookupDouble(double n)Look for ConstantDouble in ConstantPool.intlookupFieldref(java.lang.String class_name, java.lang.String field_name, java.lang.String signature)Look for ConstantFieldref in ConstantPool.intlookupFloat(float n)Look for ConstantFloat in ConstantPool.intlookupInteger(int n)Look for ConstantInteger in ConstantPool.intlookupInterfaceMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Look for ConstantInterfaceMethodref in ConstantPool.intlookupInterfaceMethodref(MethodGen method)intlookupLong(long n)Look for ConstantLong in ConstantPool.intlookupMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Look for ConstantMethodref in ConstantPool.intlookupMethodref(MethodGen method)intlookupNameAndType(java.lang.String name, java.lang.String signature)Look for ConstantNameAndType in ConstantPool.intlookupString(java.lang.String str)Look for ConstantString in ConstantPool containing String `str'.intlookupUtf8(java.lang.String n)Look for ConstantUtf8 in ConstantPool.voidsetConstant(int i, Constant c)Use with care!java.lang.StringtoString()
-
-
-
Field Detail
-
size
@Deprecated protected int size
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter
-
constants
@Deprecated protected Constant[] constants
Deprecated.(since 6.0) will be made private; do not access directly, use getter/setter
-
index
@Deprecated protected int index
Deprecated.(since 6.0) will be made private; do not access directly, use getSize()
-
-
Constructor Detail
-
ConstantPoolGen
public ConstantPoolGen(Constant[] cs)
Initialize with given array of constants.- Parameters:
cs- array of given constants, new ones will be appended
-
ConstantPoolGen
public ConstantPoolGen(ConstantPool cp)
Initialize with given constant pool.
-
ConstantPoolGen
public ConstantPoolGen()
Create empty constant pool.
-
-
Method Detail
-
adjustSize
protected void adjustSize()
Resize internal array of constants.
-
lookupString
public int lookupString(java.lang.String str)
Look for ConstantString in ConstantPool containing String `str'.- Parameters:
str- String to search for- Returns:
- index on success, -1 otherwise
-
addString
public int addString(java.lang.String str)
Add a new String constant to the ConstantPool, if it is not already in there.- Parameters:
str- String to add- Returns:
- index of entry
-
lookupClass
public int lookupClass(java.lang.String str)
Look for ConstantClass in ConstantPool named `str'.- Parameters:
str- String to search for- Returns:
- index on success, -1 otherwise
-
addClass
public int addClass(java.lang.String str)
Add a new Class reference to the ConstantPool, if it is not already in there.- Parameters:
str- Class to add- Returns:
- index of entry
-
addClass
public int addClass(ObjectType type)
Add a new Class reference to the ConstantPool for a given type.- Parameters:
type- Class to add- Returns:
- index of entry
-
addArrayClass
public int addArrayClass(ArrayType type)
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
-
lookupInteger
public int lookupInteger(int n)
Look for ConstantInteger in ConstantPool.- Parameters:
n- integer number to look for- Returns:
- index on success, -1 otherwise
-
addInteger
public int addInteger(int n)
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
-
lookupFloat
public int lookupFloat(float n)
Look for ConstantFloat in ConstantPool.- Parameters:
n- Float number to look for- Returns:
- index on success, -1 otherwise
-
addFloat
public int addFloat(float n)
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
-
lookupUtf8
public int lookupUtf8(java.lang.String n)
Look for ConstantUtf8 in ConstantPool.- Parameters:
n- Utf8 string to look for- Returns:
- index on success, -1 otherwise
-
addUtf8
public int addUtf8(java.lang.String n)
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
-
lookupLong
public int lookupLong(long n)
Look for ConstantLong in ConstantPool.- Parameters:
n- Long number to look for- Returns:
- index on success, -1 otherwise
-
addLong
public int addLong(long n)
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
-
lookupDouble
public int lookupDouble(double n)
Look for ConstantDouble in ConstantPool.- Parameters:
n- Double number to look for- Returns:
- index on success, -1 otherwise
-
addDouble
public int addDouble(double n)
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
-
lookupNameAndType
public int lookupNameAndType(java.lang.String name, java.lang.String signature)Look for ConstantNameAndType in ConstantPool.- Parameters:
name- of variable/methodsignature- of variable/method- Returns:
- index on success, -1 otherwise
-
addNameAndType
public int addNameAndType(java.lang.String name, java.lang.String signature)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
-
lookupMethodref
public int lookupMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Look for ConstantMethodref in ConstantPool.- Parameters:
class_name- Where to find methodmethod_name- Guess whatsignature- return and argument types- Returns:
- index on success, -1 otherwise
-
lookupMethodref
public int lookupMethodref(MethodGen method)
-
addMethodref
public int addMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Add a new Methodref constant to the ConstantPool, if it is not already in there.- Parameters:
class_name- class name string to addmethod_name- method name string to addsignature- method signature string to add- Returns:
- index of entry
-
addMethodref
public int addMethodref(MethodGen method)
-
lookupInterfaceMethodref
public int lookupInterfaceMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Look for ConstantInterfaceMethodref in ConstantPool.- Parameters:
class_name- Where to find methodmethod_name- Guess whatsignature- return and argument types- Returns:
- index on success, -1 otherwise
-
lookupInterfaceMethodref
public int lookupInterfaceMethodref(MethodGen method)
-
addInterfaceMethodref
public int addInterfaceMethodref(java.lang.String class_name, java.lang.String method_name, java.lang.String signature)Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.- Parameters:
class_name- class name string to addmethod_name- method name string to addsignature- signature string to add- Returns:
- index of entry
-
addInterfaceMethodref
public int addInterfaceMethodref(MethodGen method)
-
lookupFieldref
public int lookupFieldref(java.lang.String class_name, java.lang.String field_name, java.lang.String signature)Look for ConstantFieldref in ConstantPool.- Parameters:
class_name- Where to find methodfield_name- Guess whatsignature- return and argument types- Returns:
- index on success, -1 otherwise
-
addFieldref
public int addFieldref(java.lang.String class_name, java.lang.String field_name, java.lang.String signature)Add a new Fieldref constant to the ConstantPool, if it is not already in there.- Parameters:
class_name- class name string to addfield_name- field name string to addsignature- signature string to add- Returns:
- index of entry
-
getConstant
public Constant getConstant(int i)
- Parameters:
i- index in constant pool- Returns:
- constant pool entry at index i
-
setConstant
public void setConstant(int i, Constant c)Use with care!- Parameters:
i- index in constant poolc- new constant pool entry at index i
-
getConstantPool
public ConstantPool getConstantPool()
- Returns:
- intermediate constant pool
-
getSize
public int getSize()
- Returns:
- current size of constant pool
-
getFinalConstantPool
public ConstantPool getFinalConstantPool()
- Returns:
- constant pool with proper length
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- String representation.
-
addConstant
public int addConstant(Constant c, ConstantPoolGen cp)
Import constant from another ConstantPool and return new index.
-
-