TechTrader Bytecode Toolkit

com.techtrader.modules.tools.bytecode.lowlevel
Class ConstantPool

java.lang.Object
  |
  +--com.techtrader.modules.tools.bytecode.lowlevel.ConstantPool

public class ConstantPool
extends Object
implements LowLevelConstants, VisitAcceptor

Represents a class constant pool, containing entries for all strings, constants, classes, etc referenced in the class structure and opcodes. In keeping with the low-level bytecode representation, all pool indexes are 1-based.

NOTE: Entries are not meant to be manipulated manually. If you change entries by hand, make sure to call the rehash(com.techtrader.modules.tools.bytecode.lowlevel.Entry, int) method of the ConstantPool so that the entry is hashed correctly for quick lookups and to avoid duplicates.

NOTE: LongEntries and DoubleEntries are always followed by a PlaceHolderEntry in the pool, as they take up 2 pool indeces. When manually adding a new entry of these types, it is not necessary to insert the PlaceHolder as well; the system will do this automatically.

Author:
Abe White

Fields inherited from interface com.techtrader.modules.tools.bytecode.lowlevel.LowLevelConstants
ENTRY_CLASS, ENTRY_DOUBLE, ENTRY_FIELD, ENTRY_FLOAT, ENTRY_INT, ENTRY_INTMETHOD, ENTRY_LONG, ENTRY_METHOD, ENTRY_NAME_AND_TYPE, ENTRY_PLACEHOLDER, ENTRY_STRING, ENTRY_UTF8
 
Constructor Summary
ConstantPool(BCClass owner)
           
 
Method Summary
 void acceptVisit(BCVisitor visit)
          Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this object.
 int addEntry(Entry entry)
          Add an entry to the pool.
 void addEntry(int index, Entry entry)
          Set the entry at the given 1-based index.
static Entry createEntry(int type)
          Create an Entry based on its one-byte code: one of the constants in the LowLevelConstants class.
 int getClassIndex(String name)
          Get the constant pool index of the entry for the given class name.
 String getClassName(int index)
          Get the value of the entry at the given index.
 int getComplexIndex(String name, String desc, String owner, Class type)
          Get the constant pool index of the entry for the given complex entry.
 String getComplexName(int index)
          Get the value of the entry at the given index.
 String getComplexOwnerTypeName(int index)
          Get the value of the entry at the given index.
 String getComplexTypeName(int index)
          Get the value of the entry at the given index.
 Object getConstant(int index)
          Get the value of the entry at the given index.
 int getConstantIndex(Object value)
          Get the constant pool index of the entry for the given constant value.
 Entry[] getEntries()
          Get the entries in the pool.
 Entry getEntry(int index)
          Retrieve the entry at the specified 1-based index.
 int getNameAndTypeIndex(String name, String desc)
          Get the constant pool index of the entry for the given name+type.
 String getUTF(int index)
          Get the value of the entry at the given index.
 int getUTFIndex(String name)
          Get the constant pool index of the entry for the given UTF value.
 void readData(DataInput in)
           
 void rehash(Entry entry, int poolIndex)
          Rehash the given entry after modification; this allows for quick lookups and guarantees that entries won't be repeated in the pool.
 boolean removeEntry(Entry entry)
          Remove the given entry from the pool.
 Entry removeEntry(int index)
          Remove the entry at the given index.
 int setClassName(int index, String name)
          Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
 int setComplex(int index, String name, String desc, String owner, Class entryType)
          Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
 int setConstant(int index, Object value)
          Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
 Entry setEntry(int index, Entry entry)
          Set the entry at the given 1-based index.
 int setNameAndType(int index, String name, String desc)
          Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
 int setUTF(int index, String name)
          Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
 int size()
          Return the number of entries in the pool, including placeholder entries.
 void writeData(DataOutput out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConstantPool

public ConstantPool(BCClass owner)
Method Detail

createEntry

public static Entry createEntry(int type)
Create an Entry based on its one-byte code: one of the constants in the LowLevelConstants class.

getEntries

public Entry[] getEntries()
Get the entries in the pool.

getEntry

public Entry getEntry(int index)
Retrieve the entry at the specified 1-based index.
Returns:
the specified entry, or null if invalid index

setEntry

public Entry setEntry(int index,
                      Entry entry)
Set the entry at the given 1-based index.
Returns:
the entry that was replaced

addEntry

public void addEntry(int index,
                     Entry entry)
Set the entry at the given 1-based index.

addEntry

public int addEntry(Entry entry)
Add an entry to the pool.
Returns:
the index at which the entry was added

removeEntry

public Entry removeEntry(int index)
Remove the entry at the given index.
Returns:
the removed entry, or null if none

removeEntry

public boolean removeEntry(Entry entry)
Remove the given entry from the pool.
Returns:
false if the entry is not in the pool, true otherwise

size

public int size()
Return the number of entries in the pool, including placeholder entries.

getUTFIndex

public int getUTFIndex(String name)
Get the constant pool index of the entry for the given UTF value.
Returns:
the index of the matching entry, or 0 if no match

getUTF

public String getUTF(int index)
Get the value of the entry at the given index.
Returns:
the value of the given entry, or empty string if the entry does not exist

setUTF

public int setUTF(int index,
                  String name)
Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
Returns:
the index of the entry with the given value, whether an existing one was found/modified or a new one was added

getClassIndex

public int getClassIndex(String name)
Get the constant pool index of the entry for the given class name.
Returns:
the index of the matching entry, or 0 if no match

getClassName

public String getClassName(int index)
Get the value of the entry at the given index.
Returns:
the value of the given entry, or empty string if the entry does not exist

setClassName

public int setClassName(int index,
                        String name)
Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
Returns:
the index of the entry with the given value, whether an existing one was found/modified or a new one was added

getNameAndTypeIndex

public int getNameAndTypeIndex(String name,
                               String desc)
Get the constant pool index of the entry for the given name+type.
Returns:
the index of the matching entry, or 0 if no match

setNameAndType

public int setNameAndType(int index,
                          String name,
                          String desc)
Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
Returns:
the index of the entry with the given value, whether an existing one was found/modified or a new one was added

getComplexIndex

public int getComplexIndex(String name,
                           String desc,
                           String owner,
                           Class type)
Get the constant pool index of the entry for the given complex entry.
Returns:
the index of the matching entry, or 0 if no match

setComplex

public int setComplex(int index,
                      String name,
                      String desc,
                      String owner,
                      Class entryType)
Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
Returns:
the index of the entry with the given value, whether an existing one was found/modified or a new one was added

getComplexName

public String getComplexName(int index)
Get the value of the entry at the given index.
Returns:
the value of the given entry, or empty string if the entry does not exist

getComplexTypeName

public String getComplexTypeName(int index)
Get the value of the entry at the given index.
Returns:
the value of the given entry, or empty string if the entry does not exist

getComplexOwnerTypeName

public String getComplexOwnerTypeName(int index)
Get the value of the entry at the given index.
Returns:
the value of the given entry, or empty string if the entry does not exist

getConstantIndex

public int getConstantIndex(Object value)
Get the constant pool index of the entry for the given constant value.
Returns:
the index of the matching entry, or 0 if no match

getConstant

public Object getConstant(int index)
Get the value of the entry at the given index.
Returns:
the value of the given entry, or null if the entry does not exist

setConstant

public int setConstant(int index,
                       Object value)
Set the entry at the given index; if the given index is <= 0, a search will be performed for an entry with the given value, and, if it fails, a new entry will be added to the pool.
Returns:
the index of the entry with the given value, whether an existing one was found/modified or a new one was added

rehash

public void rehash(Entry entry,
                   int poolIndex)
Rehash the given entry after modification; this allows for quick lookups and guarantees that entries won't be repeated in the pool.

readData

public void readData(DataInput in)
              throws IOException

writeData

public void writeData(DataOutput out)
               throws IOException

acceptVisit

public void acceptVisit(BCVisitor visit)
Description copied from interface: VisitAcceptor
Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this object.
Specified by:
acceptVisit in interface VisitAcceptor

TechTrader Bytecode Toolkit