Top Description Fields Constructors Methods
com.sun.org.apache.bcel.internal.classfile

public Class ConstantPool

extends Object
implements Cloneable, Node, Iterable<Constant>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable, com.sun.org.apache.bcel.internal.classfile.Node, java.lang.Cloneable
Imports
java.io.DataInput, .DataOutputStream, .IOException, java.util.Arrays, .Iterator, com.sun.org.apache.bcel.internal.Const

This class represents the constant pool, i.e., 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
Constant, com.sun.org.apache.bcel.internal.generic.ConstantPoolGen

Field Summary

Modifier and TypeField and Description
private Constant[]

Constructor Summary

AccessConstructor and Description
public
ConstantPool(final Constant[]
Array of constants
constantPool
)

public
ConstantPool(final DataInput
Input stream
input
)

Reads constants from given input stream.

Method Summary

Modifier and TypeMethod and Description
public void
accept(final Visitor
Visitor object
v
)

Implements com.sun.org.apache.bcel.internal.classfile.Node.accept.

Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
public String

Returns:

String representation
constantToString
(Constant
Constant to be printed
c
)

Resolves constant to a string representation.

public String

Returns:

String representation
constantToString
(final int
of constant in constant pool
index
,
final byte
expected type
tag
)

Retrieves constant at 'index' from constant pool and resolve it to a string representation.

public ConstantPool

Returns:

deep copy of this constant pool
copy
()

public void
dump(final DataOutputStream
Output file stream
file
)

Dump constant pool to file stream in binary format.

private static String
escape(final String str)

public <T extends Constant> T

Returns:

Constant value
getConstant
(final int
Index in constant pool
index
)

Gets constant from constant pool.

public <T extends Constant> T

Returns:

Constant value
getConstant
(final int
Index in constant pool
index
,
final byte
Tag of expected constant, i.e., its type
tag
)

Gets constant from constant pool and check whether it has the expected type.

public <T extends Constant> T

Returns:

Constant value
getConstant
(final int
Index in constant pool
index
,
final byte
Tag of expected constant, i.e., its type
tag
,
final Class<T> castTo)

Gets constant from constant pool and check whether it has the expected type.

public <
A Constant subclass
T extends Constant
>
T

Returns:

Constant value
getConstant
(final int
Index in constant pool
index
,
final Class<T>
The Constant subclass to cast to.
castTo
)

Gets constant from constant pool.

public ConstantInteger

Returns:

ConstantInteger value
getConstantInteger
(final int
Index in constant pool
index
)

Gets constant from constant pool and check whether it has the expected type.

public Constant[]

Returns:

Array of constants.
getConstantPool
()

public String

Returns:

Contents of string reference
getConstantString
(final int
Index in constant pool
index
,
final byte
Tag of expected constant, either ConstantClass or ConstantString
tag
)

Gets string from constant pool and bypass the indirection of 'ConstantClass' and 'ConstantString' objects.

public ConstantUtf8

Returns:

ConstantUtf8 value
getConstantUtf8
(final int
Index in constant pool
index
)

Gets constant from constant pool and check whether it has the expected type.

public int

Returns:

Length of constant pool.
getLength
()

public Iterator<Constant>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.
public void
setConstant(final int index, final Constant
Constant to set
constant
)

public void
setConstantPool(final Constant[] constantPool)

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

constantPoolback to summary
private Constant[] constantPool

Constructor Detail

ConstantPoolback to summary
public ConstantPool(final Constant[] constantPool)
Parameters
constantPool:Constant[]

Array of constants

ConstantPoolback to summary
public ConstantPool(final DataInput input) throws IOException

Reads constants from given input stream.

Parameters
input:DataInput

Input stream

Exceptions
IOException:
if problem in readUnsignedShort or readConstant

Method Detail

acceptback to summary
public void accept(final Visitor v)

Implements com.sun.org.apache.bcel.internal.classfile.Node.accept.

Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.

Parameters
v:Visitor

Visitor object

Annotations
@Override
constantToStringback to summary
public String constantToString(Constant c) throws IllegalArgumentException

Resolves constant to a string representation.

Parameters
c:Constant

Constant to be printed

Returns:String

String representation

Exceptions
IllegalArgumentException:
if c is unknown constant type
constantToStringback to summary
public String constantToString(final int index, final byte tag)

Retrieves constant at 'index' from constant pool and resolve it to a string representation.

Parameters
index:int

of constant in constant pool

tag:byte

expected type

Returns:String

String representation

copyback to summary
public ConstantPool copy()
Returns:ConstantPool

deep copy of this constant pool

dumpback to summary
public void dump(final DataOutputStream file) throws IOException

Dump constant pool to file stream in binary format.

Parameters
file:DataOutputStream

Output file stream

Exceptions
IOException:
if problem in writeShort or dump
escapeback to summary
private static String escape(final String str)
getConstantback to summary
public <T extends Constant> T getConstant(final int index) throws ClassFormatException

Gets constant from constant pool.

Parameters
index:int

Index in constant pool

Returns:T

Constant value

Annotations
@SuppressWarnings:unchecked
Exceptions
ClassFormatException:
if index is invalid
See Also
Constant
getConstantback to summary
public <T extends Constant> T getConstant(final int index, final byte tag) throws ClassFormatException

Gets constant from constant pool and check whether it has the expected type.

Parameters
index:int

Index in constant pool

tag:byte

Tag of expected constant, i.e., its type

Returns:T

Constant value

Annotations
@SuppressWarnings:unchecked
Exceptions
ClassFormatException:
if constant type does not match tag
See Also
Constant
getConstantback to summary
public <T extends Constant> T getConstant(final int index, final byte tag, final Class<T> castTo) throws ClassFormatException

Gets constant from constant pool and check whether it has the expected type.

Parameters
index:int

Index in constant pool

tag:byte

Tag of expected constant, i.e., its type

Returns:T

Constant value

Exceptions
ClassFormatException:
if constant type does not match tag
Since
6.6.0
See Also
Constant
getConstantback to summary
public <T extends Constant> T getConstant(final int index, final Class<T> castTo) throws ClassFormatException

Gets constant from constant pool.

Parameters
<T>
A Constant subclass
index:int

Index in constant pool

castTo:Class<T>

The Constant subclass to cast to.

Returns:T

Constant value

Exceptions
ClassFormatException:
if index is invalid
Since
6.6.0
See Also
Constant
getConstantIntegerback to summary
public ConstantInteger getConstantInteger(final int index)

Gets constant from constant pool and check whether it has the expected type.

Parameters
index:int

Index in constant pool

Returns:ConstantInteger

ConstantInteger value

Exceptions
ClassFormatException:
if constant type does not match tag
See Also
ConstantInteger
getConstantPoolback to summary
public Constant[] getConstantPool()
Returns:Constant[]

Array of constants.

See Also
Constant
getConstantStringback to summary
public String getConstantString(final int index, final byte tag) throws IllegalArgumentException

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:int

Index in constant pool

tag:byte

Tag of expected constant, either ConstantClass or ConstantString

Returns:String

Contents of string reference

Exceptions
IllegalArgumentException:
if tag is invalid
See Also
ConstantClass, ConstantString
getConstantUtf8back to summary
public ConstantUtf8 getConstantUtf8(final int index) throws ClassFormatException

Gets constant from constant pool and check whether it has the expected type.

Parameters
index:int

Index in constant pool

Returns:ConstantUtf8

ConstantUtf8 value

Exceptions
ClassFormatException:
if constant type does not match tag
See Also
ConstantUtf8
getLengthback to summary
public int getLength()
Returns:int

Length of constant pool.

iteratorback to summary
public Iterator<Constant> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Returns:Iterator<Constant>

an Iterator.

Annotations
@Override
setConstantback to summary
public void setConstant(final int index, final Constant constant)
Parameters
constant:Constant

Constant to set

setConstantPoolback to summary
public void setConstantPool(final Constant[] constantPool)
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