org.apache.bcel.classfile
Class ConstantPool

java.lang.Object
  extended by org.apache.bcel.classfile.ConstantPool
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Node

public class ConstantPool
extends java.lang.Object
implements java.lang.Cloneable, Node, java.io.Serializable

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 programatically should see ConstantPoolGen.

Version:
$Id: ConstantPool.java 386056 2006-03-15 11:31:56Z tcurdt $
Author:
M. Dahm
See Also:
Constant, ConstantPoolGen, Serialized Form

Field Summary
private  Constant[] constant_pool
           
private  int constant_pool_count
           
 
Constructor Summary
ConstantPool(Constant[] constant_pool)
           
ConstantPool(java.io.DataInputStream file)
          Read constants from given file stream.
 
Method Summary
 void accept(Visitor v)
          Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
 java.lang.String constantToString(Constant c)
          Resolve constant to a string representation.
 java.lang.String constantToString(int index, byte tag)
          Retrieve constant at `index' from constant pool and resolve it to a string representation.
 ConstantPool copy()
           
 void dump(java.io.DataOutputStream file)
          Dump constant pool to file stream in binary format.
private static java.lang.String escape(java.lang.String str)
           
 Constant getConstant(int index)
          Get constant from constant pool.
 Constant getConstant(int index, byte tag)
          Get constant from constant pool and check whether it has the expected type.
 Constant[] getConstantPool()
           
 java.lang.String getConstantString(int index, byte tag)
          Get string from constant pool and bypass the indirection of `ConstantClass' and `ConstantString' objects.
 int getLength()
           
 void setConstant(int index, Constant constant)
           
 void setConstantPool(Constant[] constant_pool)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

constant_pool_count

private int constant_pool_count

constant_pool

private Constant[] constant_pool
Constructor Detail

ConstantPool

public ConstantPool(Constant[] constant_pool)
Parameters:
constant_pool - Array of constants

ConstantPool

ConstantPool(java.io.DataInputStream file)
       throws java.io.IOException,
              ClassFormatException
Read constants from given file stream.

Parameters:
file - Input stream
Throws:
java.io.IOException
ClassFormatException
Method Detail

accept

public void accept(Visitor v)
Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.

Specified by:
accept in interface Node
Parameters:
v - Visitor object

constantToString

public java.lang.String constantToString(Constant c)
                                  throws ClassFormatException
Resolve constant to a string representation.

Parameters:
c - Constant to be printed
Returns:
String representation
Throws:
ClassFormatException

escape

private static final java.lang.String escape(java.lang.String str)

constantToString

public java.lang.String constantToString(int index,
                                         byte tag)
                                  throws ClassFormatException
Retrieve constant at `index' from constant pool and resolve it to a string representation.

Parameters:
index - of constant in constant pool
tag - expected type
Returns:
String representation
Throws:
ClassFormatException

dump

public void dump(java.io.DataOutputStream file)
          throws java.io.IOException
Dump constant pool to file stream in binary format.

Parameters:
file - Output file stream
Throws:
java.io.IOException

getConstant

public Constant getConstant(int index)
Get constant from constant pool.

Parameters:
index - Index in constant pool
Returns:
Constant value
See Also:
Constant

getConstant

public Constant getConstant(int index,
                            byte tag)
                     throws ClassFormatException
Get constant from constant pool and check whether it has the expected type.

Parameters:
index - Index in constant pool
tag - Tag of expected constant, i.e., its type
Returns:
Constant value
Throws:
ClassFormatException
See Also:
Constant

getConstantPool

public Constant[] getConstantPool()
Returns:
Array of constants.
See Also:
Constant

getConstantString

public java.lang.String getConstantString(int index,
                                          byte tag)
                                   throws ClassFormatException
Get 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 - Index in constant pool
tag - Tag of expected constant, either ConstantClass or ConstantString
Returns:
Contents of string reference
Throws:
ClassFormatException
See Also:
ConstantClass, ConstantString

getLength

public int getLength()
Returns:
Length of constant pool.

setConstant

public void setConstant(int index,
                        Constant constant)
Parameters:
constant - Constant to set

setConstantPool

public void setConstantPool(Constant[] constant_pool)
Parameters:
constant_pool -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
String representation.

copy

public ConstantPool copy()
Returns:
deep copy of this constant pool