org.cojen.classfile
Class ConstantPool

java.lang.Object
  extended by org.cojen.classfile.ConstantPool

public class ConstantPool
extends 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).

Author:
Brian S O'Neill
See Also:
Opcode

Method Summary
 ConstantInfo addConstant(ConstantInfo constant)
          Will only insert into the pool if the constant is not already in the pool.
 ConstantClassInfo addConstantClass(String className)
          Get or create a constant from the constant pool representing a class.
 ConstantClassInfo addConstantClass(String className, int dim)
          Get or create a constant from the constant pool representing an array class.
 ConstantClassInfo addConstantClass(TypeDesc type)
          Get or create a constant from the constant pool representing a class.
 ConstantMethodInfo addConstantConstructor(String className, TypeDesc[] 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(String className, String fieldName, TypeDesc 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(String className, String methodName, TypeDesc ret, TypeDesc[] 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(String className, String methodName, TypeDesc ret, TypeDesc[] params)
          Get or create a constant from the constant pool representing a method in any class.
 ConstantNameAndTypeInfo addConstantNameAndType(ConstantUTFInfo nameConstant, ConstantUTFInfo descConstant)
          Get or create a constant name and type structure from the constant pool.
 ConstantNameAndTypeInfo addConstantNameAndType(String name, Descriptor type)
          Get or create a constant name and type structure from the constant pool.
 ConstantStringInfo addConstantString(String str)
          Get or create a constant string from the constant pool.
 ConstantUTFInfo addConstantUTF(String str)
          Get or create a constant UTF string from the constant pool.
 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 null if not found.
 int getSize()
          Returns the number of constants in the pool.
static ConstantPool readFrom(DataInput din)
           
 void writeTo(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 null if not found. If this constant pool has not yet been written or was not created by the read method, indexes are not assigned, and an IllegalStateException is thrown.

Throws:
ArrayIndexOutOfBoundsException - if index is out of range.
IllegalStateException - if indexes are not assigned

getAllConstants

public 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(String className)
Get or create a constant from the constant pool representing a class.


addConstantClass

public ConstantClassInfo addConstantClass(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(TypeDesc type)
Get or create a constant from the constant pool representing a class.


addConstantField

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


addConstantMethod

public ConstantMethodInfo addConstantMethod(String className,
                                            String methodName,
                                            TypeDesc ret,
                                            TypeDesc[] 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(String className,
                                                              String methodName,
                                                              TypeDesc ret,
                                                              TypeDesc[] params)
Get or create a constant from the constant pool representing an interface method in any interface.


addConstantConstructor

public ConstantMethodInfo addConstantConstructor(String className,
                                                 TypeDesc[] 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(String str)
Get or create a constant string from the constant pool.


addConstantUTF

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


addConstantNameAndType

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


addConstantNameAndType

public ConstantNameAndTypeInfo addConstantNameAndType(ConstantUTFInfo nameConstant,
                                                      ConstantUTFInfo descConstant)
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(DataOutput dout)
             throws IOException
Throws:
IOException

readFrom

public static ConstantPool readFrom(DataInput din)
                             throws IOException
Throws:
IOException


Copyright © 2004-2011 Brian S O'Neill. All Rights Reserved.