Top Description Fields Constructors Methods
org.apache.bcel.generic

public Class ConstantPoolGen

extends Object
Class Inheritance
Imports
java.util.Arrays, .HashMap, .Map, org.apache.bcel.Const, org.apache.bcel.classfile.Constant, .ConstantCP, .ConstantClass, .ConstantDouble, .ConstantDynamic, .ConstantFieldref, .ConstantFloat, .ConstantInteger, .ConstantInterfaceMethodref, .ConstantInvokeDynamic, .ConstantLong, .ConstantMethodref, .ConstantNameAndType, .ConstantPool, .ConstantString, .ConstantUtf8, .Utility

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
Constant

Field Summary

Modifier and TypeField and Description
private final Map<String, Integer>
protected Constant[]
constants

Deprecated (since 6.0) will be made private; do not access directly, use getter/setter
private final Map<String, Integer>
private static final int
private static final String
private static final String
protected int
index

Deprecated (since 6.0) will be made private; do not access directly, use getSize()
private static final String
private static final String
private final Map<String, Integer>
protected int
size

Deprecated (since 6.0) will be made private; do not access directly, use getter/setter
private final Map<String, Integer>
private final Map<String, Integer>

Constructor Summary

AccessConstructor and Description
public
ConstantPoolGen()

Constructs a new empty constant pool.

public
ConstantPoolGen(final Constant[]
array of given constants, new ones will be appended
cs
)

Constructs a new instance with the given array of constants.

public
ConstantPoolGen(final ConstantPool
the constant pool.
cp
)

Constructs a new instance with the given constant pool.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

index of entry
addArrayClass
(final ArrayType
type of array class
type
)

Add a reference to an array class (e.g. String[][]) as needed by MULTIANEWARRAY instruction, e.g. to the ConstantPool.

public int

Returns:

index of entry
addClass
(final ObjectType
Class to add
type
)

Add a new Class reference to the ConstantPool for a given type.

public int

Returns:

index of entry
addClass
(final String
Class to add
str
)

Add a new Class reference to the ConstantPool, if it is not already in there.

private int
addClass_(final String clazz)

public int

Returns:

index of entry
addConstant
(final Constant
The constant to add.
constant
,
final ConstantPoolGen
Source pool.
cpGen
)

Adds a constant from another ConstantPool and returns the new index.

public int

Returns:

index of entry
addDouble
(final double
Double number to add
n
)

Add a new double constant to the ConstantPool, if it is not already in there.

public int

Returns:

index of entry
addFieldref
(final String
class name string to add
className
,
final String
field name string to add
fieldName
,
final String
signature string to add
signature
)

Add a new Fieldref constant to the ConstantPool, if it is not already in there.

public int

Returns:

index of entry
addFloat
(final float
Float number to add
n
)

Add a new Float constant to the ConstantPool, if it is not already in there.

public int

Returns:

index of entry
addInteger
(final int
integer number to add
n
)

Add a new Integer constant to the ConstantPool, if it is not already in there.

public int
public int

Returns:

index of entry
addInterfaceMethodref
(final String
class name string to add
className
,
final String
method name string to add
methodName
,
final String
signature string to add
signature
)

Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.

public int

Returns:

index of entry
addLong
(final long
Long number to add
n
)

Add a new long constant to the ConstantPool, if it is not already in there.

public int
addMethodref(final MethodGen method)

public int

Returns:

index of entry
addMethodref
(final String
class name string to add
className
,
final String
method name string to add
methodName
,
final String
method signature string to add
signature
)

Add a new Methodref constant to the ConstantPool, if it is not already in there.

public int

Returns:

index of entry
addNameAndType
(final String
Name string to add
name
,
final String
signature string to add
signature
)

Add a new NameAndType constant to the ConstantPool if it is not already in there.

public int

Returns:

index of entry
addString
(final String
String to add
str
)

Add a new String constant to the ConstantPool, if it is not already in there.

public int

Returns:

index of entry
addUtf8
(final String
Utf8 string to add
n
)

Add a new Utf8 constant to the ConstantPool, if it is not already in there.

protected void
adjustSize()

Resize internal array of constants.

private int
computeIfAbsent(final Map<String, Integer> map, final String key, final int value)

public Constant

Returns:

constant pool entry at index i
getConstant
(final int
index in constant pool
i
)

public ConstantPool

Returns:

intermediate constant pool
getConstantPool
()

public ConstantPool

Returns:

constant pool with proper length
getFinalConstantPool
()

private int
getIndex(final Map<String, Integer> map, final String key)

public int

Returns:

current size of constant pool
getSize
()

public int

Returns:

index on success, -1 otherwise
lookupClass
(final String
String to search for
str
)

Look for ConstantClass in ConstantPool named 'str'.

public int

Returns:

index on success, -1 otherwise
lookupDouble
(final double
Double number to look for
n
)

Look for ConstantDouble in ConstantPool.

public int

Returns:

index on success, -1 otherwise
lookupFieldref
(final String
Where to find method
className
,
final String
Guess what
fieldName
,
final String
return and argument types
signature
)

Look for ConstantFieldref in ConstantPool.

public int

Returns:

index on success, -1 otherwise
lookupFloat
(final float
Float number to look for
n
)

Look for ConstantFloat in ConstantPool.

public int

Returns:

index on success, -1 otherwise
lookupInteger
(final int
integer number to look for
n
)

Look for ConstantInteger in ConstantPool.

public int
public int

Returns:

index on success, -1 otherwise
lookupInterfaceMethodref
(final String
Where to find method
className
,
final String
Guess what
methodName
,
final String
return and argument types
signature
)

Look for ConstantInterfaceMethodref in ConstantPool.

public int

Returns:

index on success, -1 otherwise
lookupLong
(final long
Long number to look for
n
)

Look for ConstantLong in ConstantPool.

public int
lookupMethodref(final MethodGen method)

public int

Returns:

index on success, -1 otherwise
lookupMethodref
(final String
Where to find method
className
,
final String
Guess what
methodName
,
final String
return and argument types
signature
)

Look for ConstantMethodref in ConstantPool.

public int

Returns:

index on success, -1 otherwise
lookupNameAndType
(final String
of variable/method
name
,
final String
of variable/method
signature
)

Look for ConstantNameAndType in ConstantPool.

public int

Returns:

index on success, -1 otherwise
lookupString
(final String
String to search for
str
)

Look for ConstantString in ConstantPool containing String 'str'.

public int

Returns:

index on success, -1 otherwise
lookupUtf8
(final String
Utf8 string to look for
n
)

Look for ConstantUtf8 in ConstantPool.

public void
setConstant(final int
index in constant pool
i
,
final Constant
new constant pool entry at index i
c
)

Use with care!

private int
toIndex(final Integer index)

public String

Returns:

String representation.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

classTableback to summary
private final Map<String, Integer> classTable
constantsback to summary
protected Constant[] constants

Deprecated

(since 6.0) will be made private; do not access directly, use getter/setter

Annotations
@Deprecated
cpTableback to summary
private final Map<String, Integer> cpTable
DEFAULT_BUFFER_SIZEback to summary
private static final int DEFAULT_BUFFER_SIZE
FIELDREF_DELIMback to summary
private static final String FIELDREF_DELIM
IMETHODREF_DELIMback to summary
private static final String IMETHODREF_DELIM
indexback to summary
protected int index

Deprecated

(since 6.0) will be made private; do not access directly, use getSize()

Annotations
@Deprecated
METHODREF_DELIMback to summary
private static final String METHODREF_DELIM
NAT_DELIMback to summary
private static final String NAT_DELIM
natTableback to summary
private final Map<String, Integer> natTable
sizeback to summary
protected int size

Deprecated

(since 6.0) will be made private; do not access directly, use getter/setter

Annotations
@Deprecated
stringTableback to summary
private final Map<String, Integer> stringTable
utf8Tableback to summary
private final Map<String, Integer> utf8Table

Constructor Detail

ConstantPoolGenback to summary
public ConstantPoolGen()

Constructs a new empty constant pool.

ConstantPoolGenback to summary
public ConstantPoolGen(final Constant[] cs)

Constructs a new instance with the given array of constants.

Parameters
cs:Constant[]

array of given constants, new ones will be appended

ConstantPoolGenback to summary
public ConstantPoolGen(final ConstantPool cp)

Constructs a new instance with the given constant pool.

Parameters
cp:ConstantPool

the constant pool.

Method Detail

addArrayClassback to summary
public int addArrayClass(final ArrayType type)

Add a reference to an array class (e.g. String[][]) as needed by MULTIANEWARRAY instruction, e.g. to the ConstantPool.

Parameters
type:ArrayType

type of array class

Returns:int

index of entry

addClassback to summary
public int addClass(final ObjectType type)

Add a new Class reference to the ConstantPool for a given type.

Parameters
type:ObjectType

Class to add

Returns:int

index of entry

addClassback to summary
public int addClass(final String str)

Add a new Class reference to the ConstantPool, if it is not already in there.

Parameters
str:String

Class to add

Returns:int

index of entry

addClass_back to summary
private int addClass_(final String clazz)
addConstantback to summary
public int addConstant(final Constant constant, final ConstantPoolGen cpGen)

Adds a constant from another ConstantPool and returns the new index.

Parameters
constant:Constant

The constant to add.

cpGen:ConstantPoolGen

Source pool.

Returns:int

index of entry

addDoubleback to summary
public int addDouble(final double n)

Add a new double constant to the ConstantPool, if it is not already in there.

Parameters
n:double

Double number to add

Returns:int

index of entry

addFieldrefback to summary
public int addFieldref(final String className, final String fieldName, final String signature)

Add a new Fieldref constant to the ConstantPool, if it is not already in there.

Parameters
className:String

class name string to add

fieldName:String

field name string to add

signature:String

signature string to add

Returns:int

index of entry

addFloatback to summary
public int addFloat(final float n)

Add a new Float constant to the ConstantPool, if it is not already in there.

Parameters
n:float

Float number to add

Returns:int

index of entry

addIntegerback to summary
public int addInteger(final int n)

Add a new Integer constant to the ConstantPool, if it is not already in there.

Parameters
n:int

integer number to add

Returns:int

index of entry

addInterfaceMethodrefback to summary
public int addInterfaceMethodref(final MethodGen method)
addInterfaceMethodrefback to summary
public int addInterfaceMethodref(final String className, final String methodName, final String signature)

Add a new InterfaceMethodref constant to the ConstantPool, if it is not already in there.

Parameters
className:String

class name string to add

methodName:String

method name string to add

signature:String

signature string to add

Returns:int

index of entry

addLongback to summary
public int addLong(final long n)

Add a new long constant to the ConstantPool, if it is not already in there.

Parameters
n:long

Long number to add

Returns:int

index of entry

addMethodrefback to summary
public int addMethodref(final MethodGen method)
addMethodrefback to summary
public int addMethodref(final String className, final String methodName, final String signature)

Add a new Methodref constant to the ConstantPool, if it is not already in there.

Parameters
className:String

class name string to add

methodName:String

method name string to add

signature:String

method signature string to add

Returns:int

index of entry

addNameAndTypeback to summary
public int addNameAndType(final String name, final String signature)

Add a new NameAndType constant to the ConstantPool if it is not already in there.

Parameters
name:String

Name string to add

signature:String

signature string to add

Returns:int

index of entry

addStringback to summary
public int addString(final String str)

Add a new String constant to the ConstantPool, if it is not already in there.

Parameters
str:String

String to add

Returns:int

index of entry

addUtf8back to summary
public int addUtf8(final String n)

Add a new Utf8 constant to the ConstantPool, if it is not already in there.

Parameters
n:String

Utf8 string to add

Returns:int

index of entry

adjustSizeback to summary
protected void adjustSize()

Resize internal array of constants.

computeIfAbsentback to summary
private int computeIfAbsent(final Map<String, Integer> map, final String key, final int value)
getConstantback to summary
public Constant getConstant(final int i)
Parameters
i:int

index in constant pool

Returns:Constant

constant pool entry at index i

getConstantPoolback to summary
public ConstantPool getConstantPool()
Returns:ConstantPool

intermediate constant pool

getFinalConstantPoolback to summary
public ConstantPool getFinalConstantPool()
Returns:ConstantPool

constant pool with proper length

getIndexback to summary
private int getIndex(final Map<String, Integer> map, final String key)
getSizeback to summary
public int getSize()
Returns:int

current size of constant pool

lookupClassback to summary
public int lookupClass(final String str)

Look for ConstantClass in ConstantPool named 'str'.

Parameters
str:String

String to search for

Returns:int

index on success, -1 otherwise

lookupDoubleback to summary
public int lookupDouble(final double n)

Look for ConstantDouble in ConstantPool.

Parameters
n:double

Double number to look for

Returns:int

index on success, -1 otherwise

lookupFieldrefback to summary
public int lookupFieldref(final String className, final String fieldName, final String signature)

Look for ConstantFieldref in ConstantPool.

Parameters
className:String

Where to find method

fieldName:String

Guess what

signature:String

return and argument types

Returns:int

index on success, -1 otherwise

lookupFloatback to summary
public int lookupFloat(final float n)

Look for ConstantFloat in ConstantPool.

Parameters
n:float

Float number to look for

Returns:int

index on success, -1 otherwise

lookupIntegerback to summary
public int lookupInteger(final int n)

Look for ConstantInteger in ConstantPool.

Parameters
n:int

integer number to look for

Returns:int

index on success, -1 otherwise

lookupInterfaceMethodrefback to summary
public int lookupInterfaceMethodref(final MethodGen method)
lookupInterfaceMethodrefback to summary
public int lookupInterfaceMethodref(final String className, final String methodName, final String signature)

Look for ConstantInterfaceMethodref in ConstantPool.

Parameters
className:String

Where to find method

methodName:String

Guess what

signature:String

return and argument types

Returns:int

index on success, -1 otherwise

lookupLongback to summary
public int lookupLong(final long n)

Look for ConstantLong in ConstantPool.

Parameters
n:long

Long number to look for

Returns:int

index on success, -1 otherwise

lookupMethodrefback to summary
public int lookupMethodref(final MethodGen method)
lookupMethodrefback to summary
public int lookupMethodref(final String className, final String methodName, final String signature)

Look for ConstantMethodref in ConstantPool.

Parameters
className:String

Where to find method

methodName:String

Guess what

signature:String

return and argument types

Returns:int

index on success, -1 otherwise

lookupNameAndTypeback to summary
public int lookupNameAndType(final String name, final String signature)

Look for ConstantNameAndType in ConstantPool.

Parameters
name:String

of variable/method

signature:String

of variable/method

Returns:int

index on success, -1 otherwise

lookupStringback to summary
public int lookupString(final String str)

Look for ConstantString in ConstantPool containing String 'str'.

Parameters
str:String

String to search for

Returns:int

index on success, -1 otherwise

lookupUtf8back to summary
public int lookupUtf8(final String n)

Look for ConstantUtf8 in ConstantPool.

Parameters
n:String

Utf8 string to look for

Returns:int

index on success, -1 otherwise

setConstantback to summary
public void setConstant(final int i, final Constant c)

Use with care!

Parameters
i:int

index in constant pool

c:Constant

new constant pool entry at index i

toIndexback to summary
private int toIndex(final Integer index)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

String representation.

Annotations
@Override