com.go.trove.classfile
Class ConstantPool

java.lang.Object
  |
  +--com.go.trove.classfile.ConstantPool

public class ConstantPool
extends java.lang.Object

This class corresponds to the constant_pool structure as defined in section 4.4 of The Java Virtual Machine Specification.

ConstantPool entries are not written out in the order in which they were added to it. Instead, their ordering is changed such that String, Integer and Float constants are written out first. This provides a slight optimization for referencing these constants from a code attribute. It means that Opcode.LDC will more likely be used (one-byte index) than Opcode.LDC_W (two-byte index).

Version:
23 , 9/07/00
Author:
Brian S O'Neill
See Also:
CodeAttr, Opcode

Method Summary
 ConstantInfo addConstant(ConstantInfo constant)
          Will only insert into the pool if the constant is not already in the pool.
 ConstantClassInfo addConstantClass(java.lang.String className)
          Get or create a constant from the constant pool representing a class.
 ConstantClassInfo addConstantClass(java.lang.String className, int dim)
          Get or create a constant from the constant pool representing an array class.
 ConstantClassInfo addConstantClass(TypeDescriptor type)
          Get or create a constant from the constant pool representing a class.
 ConstantMethodInfo addConstantConstructor(java.lang.String className, TypeDescriptor[] params)
          Get or create a constant from the constant pool representing a constructor in any class.
 ConstantDoubleInfo addConstantDouble(double value)
          Get or create a constant double from the constant pool.
 ConstantFieldInfo addConstantField(java.lang.String className, java.lang.String fieldName, TypeDescriptor type)
          Get or create a constant from the constant pool representing a field in any class.
 ConstantFloatInfo addConstantFloat(float value)
          Get or create a constant float from the constant pool.
 ConstantIntegerInfo addConstantInteger(int value)
          Get or create a constant integer from the constant pool.
 ConstantInterfaceMethodInfo addConstantInterfaceMethod(java.lang.String className, java.lang.String methodName, TypeDescriptor ret, TypeDescriptor[] params)
          Get or create a constant from the constant pool representing an interface method in any interface.
 ConstantLongInfo addConstantLong(long value)
          Get or create a constant long from the constant pool.
 ConstantMethodInfo addConstantMethod(java.lang.String className, java.lang.String methodName, TypeDescriptor ret, TypeDescriptor[] params)
          Get or create a constant from the constant pool representing a method in any class.
 ConstantNameAndTypeInfo addConstantNameAndType(java.lang.String name, Descriptor type)
          Get or create a constant name and type structure from the constant pool.
 ConstantStringInfo addConstantString(java.lang.String str)
          Get or create a constant string from the constant pool.
 ConstantUTFInfo addConstantUTF(java.lang.String str)
          Get or create a constant UTF string from the constant pool.
 java.util.Set getAllConstants()
          Returns all the constants in the pool, in no particular order.
 ConstantInfo getConstant(int index)
          Returns a constant from the pool by index, or throws an exception if not found.
 int getSize()
          Returns the number of constants in the pool.
static ConstantPool readFrom(java.io.DataInput din)
           
 void writeTo(java.io.DataOutput dout)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getConstant

public ConstantInfo getConstant(int index)
Returns a constant from the pool by index, or throws an exception if not found. If this constant pool has not yet been written or was not created by the read method, indexes are not assigned.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if index is out of range.

getAllConstants

public java.util.Set getAllConstants()
Returns all the constants in the pool, in no particular order.

getSize

public int getSize()
Returns the number of constants in the pool.

addConstantClass

public ConstantClassInfo addConstantClass(java.lang.String className)
Get or create a constant from the constant pool representing a class.

addConstantClass

public ConstantClassInfo addConstantClass(java.lang.String className,
                                          int dim)
Get or create a constant from the constant pool representing an array class.
Parameters:
dim - Number of array dimensions.

addConstantClass

public ConstantClassInfo addConstantClass(TypeDescriptor type)
Get or create a constant from the constant pool representing a class.

addConstantField

public ConstantFieldInfo addConstantField(java.lang.String className,
                                          java.lang.String fieldName,
                                          TypeDescriptor type)
Get or create a constant from the constant pool representing a field in any class.

addConstantMethod

public ConstantMethodInfo addConstantMethod(java.lang.String className,
                                            java.lang.String methodName,
                                            TypeDescriptor ret,
                                            TypeDescriptor[] params)
Get or create a constant from the constant pool representing a method in any class. If the method returns void, set ret to null.

addConstantInterfaceMethod

public ConstantInterfaceMethodInfo addConstantInterfaceMethod(java.lang.String className,
                                                              java.lang.String methodName,
                                                              TypeDescriptor ret,
                                                              TypeDescriptor[] params)
Get or create a constant from the constant pool representing an interface method in any interface.

addConstantConstructor

public ConstantMethodInfo addConstantConstructor(java.lang.String className,
                                                 TypeDescriptor[] params)
Get or create a constant from the constant pool representing a constructor in any class.

addConstantInteger

public ConstantIntegerInfo addConstantInteger(int value)
Get or create a constant integer from the constant pool.

addConstantLong

public ConstantLongInfo addConstantLong(long value)
Get or create a constant long from the constant pool.

addConstantFloat

public ConstantFloatInfo addConstantFloat(float value)
Get or create a constant float from the constant pool.

addConstantDouble

public ConstantDoubleInfo addConstantDouble(double value)
Get or create a constant double from the constant pool.

addConstantString

public ConstantStringInfo addConstantString(java.lang.String str)
Get or create a constant string from the constant pool.

addConstantUTF

public ConstantUTFInfo addConstantUTF(java.lang.String str)
Get or create a constant UTF string from the constant pool.

addConstantNameAndType

public ConstantNameAndTypeInfo addConstantNameAndType(java.lang.String name,
                                                      Descriptor type)
Get or create a constant name and type structure from the constant pool.

addConstant

public ConstantInfo addConstant(ConstantInfo constant)
Will only insert into the pool if the constant is not already in the pool.
Returns:
The actual constant in the pool.

writeTo

public void writeTo(java.io.DataOutput dout)
             throws java.io.IOException

readFrom

public static ConstantPool readFrom(java.io.DataInput din)
                             throws java.io.IOException