edu.wisc.cs.java
Class ConstantPool

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--java.util.Vector
                    |
                    +--edu.wisc.cs.java.ConstantPool

public class ConstantPool
extends java.util.Vector

This class maintains a constant pool for a single class as specified by the Java Virtual Machine specification. Valid indices of constants range from one to (size() - 1) method. As per the Java Virtual Machine specification, the constant at index zero is not in the constant pool. Strange, but true.

See Also:
Serialized Form

Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ConstantPool(java.io.DataInputStream in)
          Constructs a new constant pool object by reading the table from a stream.
 
Method Summary
 int addClass(java.lang.String classname)
          Adds a new CLASS constant with the specified name.
 int addField(java.lang.String classname, java.lang.String name, java.lang.String desc)
          Adds constants to describe another field.
 int addMethod(java.lang.String classname, java.lang.String name, java.lang.String desc)
          Adds constants to describe another method.
 int addNameAndType(java.lang.String name, java.lang.String type)
          Adds a new NAME_AND_TYPE constant with the specified name and type values.
 int addUTFConstant(java.lang.String text)
          Adds a new UTF8 constant to the constant pool.
 int getClassIndex(java.lang.String s)
          Returns the index of a CLASS constant representing a class with the specified name.
 Constant getConstant(int i)
          Returns a constant from the constant pool.
 int getMethodIndex(java.lang.String cls, java.lang.String mname, java.lang.String mdesc)
          Searches for a reference to a method in the constant pool.
 int getNameAndTypeIndex(java.lang.String name, java.lang.String type)
          Returns the index of a NAME_AND_TYPE constant with the specified name and type strings.
 java.lang.String getString(int index)
          Returns a string representation of the desired constant.
 int getUTFIndex(java.lang.String s)
          Returns the index of an entry in the constant pool which contains the desired string.
 void printData(java.io.PrintStream out)
          Writes textual information about the class represented by this ConstantPool object to the specified destination.
 int size()
          Returns number of constants in this pool.
 void writeTo(java.io.DataOutputStream out)
          Writes the constant pool to the specified stream in the format specified by the classfile format.
 
Methods inherited from class java.util.Vector
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, subList, toArray, toArray, toString, trimToSize
 
Methods inherited from class java.util.AbstractList
iterator, listIterator, listIterator
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ConstantPool

public ConstantPool(java.io.DataInputStream in)
             throws java.io.IOException
Constructs a new constant pool object by reading the table from a stream.
Parameters:
in - the stream from which to read the constant pool information.
Throws:
java.io.IOException - if there is a problem reading from the stream.
Method Detail

getConstant

public Constant getConstant(int i)
Returns a constant from the constant pool. Note that unlike other arrays and vectors, the constant pool has one based indexing.
Parameters:
i - the index of the desired constant.
Returns:
a constant from the pool.
Throws:
ArrayIndexOutOfBoundsException - if the index is invalid.

getString

public java.lang.String getString(int index)
Returns a string representation of the desired constant. If that constant references another, as in the case of a FIELD_REF or METHOD_REF constnat, the referenced constants are combined in a meaningful way.
Parameters:
index - the index of the constant to be represented as a string.
Returns:
a string representing the resired constant.

size

public int size()
Returns number of constants in this pool.
Overrides:
size in class java.util.Vector
Returns:
the number of constants in the pool.

getUTFIndex

public int getUTFIndex(java.lang.String s)
Returns the index of an entry in the constant pool which contains the desired string.
Parameters:
s - the string to be located in the constant pool.
Returns:
the index of the requested string, -1 otherwize.

addUTFConstant

public int addUTFConstant(java.lang.String text)
Adds a new UTF8 constant to the constant pool.
Parameters:
text - the text for the new UTF8 constant.
Returns:
the index of the new constant.

getClassIndex

public int getClassIndex(java.lang.String s)
Returns the index of a CLASS constant representing a class with the specified name.
Returns:
the index of a CLASS constant, or -1 if not found.

getNameAndTypeIndex

public int getNameAndTypeIndex(java.lang.String name,
                               java.lang.String type)
Returns the index of a NAME_AND_TYPE constant with the specified name and type strings. Returns the index of a NAME_AND_TYPE constant, or -1 if not found.

addField

public int addField(java.lang.String classname,
                    java.lang.String name,
                    java.lang.String desc)
Adds constants to describe another field.
Parameters:
classname - the index into the constant pool containing the CLASS of which the new field will be a member.
name - the name of the new field.
desc - a string describing the type of the field.
Returns:
the index of the new FIELD_REF constant.

addMethod

public int addMethod(java.lang.String classname,
                     java.lang.String name,
                     java.lang.String desc)
Adds constants to describe another method.
Parameters:
classname - the index into the constant pool containing the CLASS of which the new method will be a member.
name - the name of the new method.
desc - a string describing the type of the method.
Returns:
the index of the new METHOD_REF constant.

addClass

public int addClass(java.lang.String classname)
Adds a new CLASS constant with the specified name.
Parameters:
classname - the name of the class to be represented by the new constant.
Returns:
the index of the new CLASS constant.

addNameAndType

public int addNameAndType(java.lang.String name,
                          java.lang.String type)
Adds a new NAME_AND_TYPE constant with the specified name and type values.
Parameters:
name - the name of the new variable.
type - the type of the new variable.
Returns:
the index of the new NAME_AND_TYPE constant.

getMethodIndex

public int getMethodIndex(java.lang.String cls,
                          java.lang.String mname,
                          java.lang.String mdesc)
Searches for a reference to a method in the constant pool. The class and method names, as well as the method descriptor must match excatly. If a match is found, the index of that match is returned, if not, negative one is returned.
Parameters:
cls - the name of the class which the method is a member of.
mname - the name of the method.
mdesc - the descriptor for the method.
Returns:
the index of the requested method, -1 if the method is not found.

printData

public void printData(java.io.PrintStream out)
Writes textual information about the class represented by this ConstantPool object to the specified destination. The destination is usually System.out or System.err. This method is primarily for debugging purposes.
Parameters:
out - a PrintStream connected to the desired destination.

writeTo

public void writeTo(java.io.DataOutputStream out)
             throws java.io.IOException
Writes the constant pool to the specified stream in the format specified by the classfile format. See the Java Virtual Machine specification for more details.
Parameters:
out - a destination stream to write the classfile data to.
Throws:
java.io.IOException - if there is a problem writing to the stream.