Package serp.bytecode.lowlevel
Class ConstantPool
- java.lang.Object
-
- serp.bytecode.lowlevel.ConstantPool
-
- All Implemented Interfaces:
VisitAcceptor
public class ConstantPool extends java.lang.Object implements VisitAcceptor
A bytecode constant pool, containing entries for all strings, constants, classes, etc referenced in the class structure and method opcodes. In keeping with the low-level bytecode representation, all pool indexes are 1-based andLongEntrys andDoubleEntrys each occupy two indexes in the pool.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classConstantPool.ClassKeyKey for class entries.private static classConstantPool.DoublePtrKeyBase class key for entries with two ptr to other entries.private static classConstantPool.FieldKeyKey for field entries.private static classConstantPool.InterfaceMethodKeyKey for interface method entries.private static classConstantPool.InvokeDynamicKeyKey for method entries.private static classConstantPool.MethodKeyKey for method entries.private static classConstantPool.NameAndTypeKeyKey for name and type entries.private static classConstantPool.PtrKeyBase class key for entries with one ptr to another entry.private static classConstantPool.StringKeyKey for string entries.
-
Constructor Summary
Constructors Constructor Description ConstantPool()Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidacceptVisit(BCVisitor visit)Accept a visit from aBCVisitor, 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 one.private intaddEntry(java.lang.Object key, Entry entry)Add an entry to the pool using the given key.intaddEntry(Entry entry)Add an entry to the pool.voidclear()Clear all entries from the pool.private intfind(java.lang.Object key)Returns the constant pool index of the entry with the given key.intfindClassEntry(java.lang.String name, boolean add)Return the constant pool index of theClassEntryfor the given class name, or 0 if it does not exist.private intfindComplexEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, int type, boolean add)Return the constant pool index of theComplexEntryfor the given name, descriptor, and owner class name.intfindDoubleEntry(double value, boolean add)Return the constant pool index of theDoubleEntryfor the given value, or 0 if it does not exist.intfindFieldEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theFieldEntryfor the given name, descriptor, and owner class name.intfindFloatEntry(float value, boolean add)Return the constant pool index of theFloatEntryfor the given value, or 0 if it does not exist.intfindIntEntry(int value, boolean add)Return the constant pool index of theIntEntryfor the given value, or 0 if it does not exist.intfindInterfaceMethodEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theInterfaceMethodEntryfor the given name, descriptor, and owner class name.intfindInvokeDynamicEntry(int bootstrapMethodIndex, java.lang.String name, java.lang.String desc, boolean add)intfindLongEntry(long value, boolean add)Return the constant pool index of theLongEntryfor the given value, or 0 if it does not exist.intfindMethodEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theMethodEntryfor the given name, descriptor, and owner class name.intfindNameAndTypeEntry(java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theNameAndTypeEntryfor the given name and descriptor, or 0 if it does not exist.intfindStringEntry(java.lang.String value, boolean add)Return the constant pool index of theStringEntryfor the given string value, or 0 if it does not exist.intfindUTF8Entry(java.lang.String value, boolean add)Return the index of theUTF8Entrywith the given value, or 0 if it does not exist.Entry[]getEntries()Return all the entries in the pool.EntrygetEntry(int index)Retrieve the entry at the specified 1-based index.(package private) static java.lang.ObjectgetKey(Entry entry)Return the hash key used for the specified entry.intindexOf(Entry entry)Return the index of the given entry, or 0 if it is not in the pool.(package private) voidmodifyEntry(java.lang.Object origKey, Entry entry)Called by constant pool entries when they are mutated.voidread(java.io.DataInput in)Fill the constant pool from the given bytecode stream.booleanremoveEntry(Entry entry)Remove the given entry from the pool.intsize()Return the number of places occupied in the pool, including the fact that long and double entries occupy two places.voidwrite(java.io.DataOutput out)Write the constant pool to the given bytecode stream.
-
-
-
Method Detail
-
getEntries
public Entry[] getEntries()
Return all the entries in the pool.
-
getEntry
public Entry getEntry(int index)
Retrieve the entry at the specified 1-based index.- Throws:
java.lang.IndexOutOfBoundsException- if index is invalid, including the case that it points to the second slot of a long or double entry
-
indexOf
public int indexOf(Entry entry)
Return the index of the given entry, or 0 if it is not in the pool.
-
addEntry
public int addEntry(Entry entry)
Add an entry to the pool.- Returns:
- the index at which the entry was added
-
addEntry
private int addEntry(java.lang.Object key, Entry entry)Add an entry to the pool using the given key.
-
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
-
clear
public void clear()
Clear all entries from the pool.
-
size
public int size()
Return the number of places occupied in the pool, including the fact that long and double entries occupy two places.
-
findUTF8Entry
public int findUTF8Entry(java.lang.String value, boolean add)Return the index of theUTF8Entrywith the given value, or 0 if it does not exist.- Parameters:
add- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findDoubleEntry
public int findDoubleEntry(double value, boolean add)Return the constant pool index of theDoubleEntryfor the given value, or 0 if it does not exist.- Parameters:
value- the value to findadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findFloatEntry
public int findFloatEntry(float value, boolean add)Return the constant pool index of theFloatEntryfor the given value, or 0 if it does not exist.- Parameters:
value- the value to findadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findIntEntry
public int findIntEntry(int value, boolean add)Return the constant pool index of theIntEntryfor the given value, or 0 if it does not exist.- Parameters:
value- the value to findadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findLongEntry
public int findLongEntry(long value, boolean add)Return the constant pool index of theLongEntryfor the given value, or 0 if it does not exist.- Parameters:
value- the value to findadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findStringEntry
public int findStringEntry(java.lang.String value, boolean add)Return the constant pool index of theStringEntryfor the given string value, or 0 if it does not exist.- Parameters:
value- the value to findadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findClassEntry
public int findClassEntry(java.lang.String name, boolean add)Return the constant pool index of theClassEntryfor the given class name, or 0 if it does not exist.- Parameters:
name- the class name in internal formadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findNameAndTypeEntry
public int findNameAndTypeEntry(java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theNameAndTypeEntryfor the given name and descriptor, or 0 if it does not exist.- Parameters:
name- the name of the entitydesc- the descriptor of the entity in internal formadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findFieldEntry
public int findFieldEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theFieldEntryfor the given name, descriptor, and owner class name.- Parameters:
owner- the name of the field's owning class in internal formname- the name of the fielddesc- the descriptor of the field in internal formadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findMethodEntry
public int findMethodEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theMethodEntryfor the given name, descriptor, and owner class name.- Parameters:
owner- the name of the method's owning class in internal formname- the name of the methoddesc- the descriptor of the method in internal formadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findInterfaceMethodEntry
public int findInterfaceMethodEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, boolean add)Return the constant pool index of theInterfaceMethodEntryfor the given name, descriptor, and owner class name.- Parameters:
owner- the name of the method's owning class in internal formname- the name of the methoddesc- the descriptor of the method in internal formadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
findInvokeDynamicEntry
public int findInvokeDynamicEntry(int bootstrapMethodIndex, java.lang.String name, java.lang.String desc, boolean add)
-
findComplexEntry
private int findComplexEntry(java.lang.String owner, java.lang.String name, java.lang.String desc, int type, boolean add)Return the constant pool index of theComplexEntryfor the given name, descriptor, and owner class name.- Parameters:
owner- the name of the owning class in internal formname- the name of the entitydesc- the descriptor of the entity in internal formtype- the type of entry: field, method, interface methodadd- if true, the entry will be added if it does not already exist, and the new entry's index returned
-
acceptVisit
public void acceptVisit(BCVisitor visit)
Description copied from interface:VisitAcceptorAccept a visit from aBCVisitor, 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 one.- Specified by:
acceptVisitin interfaceVisitAcceptor
-
read
public void read(java.io.DataInput in) throws java.io.IOExceptionFill the constant pool from the given bytecode stream.- Throws:
java.io.IOException
-
write
public void write(java.io.DataOutput out) throws java.io.IOExceptionWrite the constant pool to the given bytecode stream.- Throws:
java.io.IOException
-
modifyEntry
void modifyEntry(java.lang.Object origKey, Entry entry)Called by constant pool entries when they are mutated.
-
find
private int find(java.lang.Object key)
Returns the constant pool index of the entry with the given key.
-
getKey
static java.lang.Object getKey(Entry entry)
Return the hash key used for the specified entry.
-
-