Package com.tonic.parser
Class ConstPool
- java.lang.Object
-
- com.tonic.parser.ConstPool
-
public class ConstPool extends Object
Represents the Constant Pool of a Java class file.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddInvokeDynamic(int bootstrapMethodAttrIndex, int nameAndTypeIndex)Adds an InvokeDynamic constant pool entry.intaddItem(Item<?> newItem)Adds a new item to the constant pool.intaddMethodHandle(int referenceKind, int referenceIndex)Adds a MethodHandle constant pool entry by reference index.intaddMethodRef(String owner, String name, String descriptor)Adds a MethodRef constant pool entry.intaddMethodType(String descriptor)Adds a MethodType constant pool entry.intaddNameAndType(String name, String descriptor)Adds a NameAndType constant pool entry.Utf8ItemaddUtf8(String value)Always adds a new Utf8Item with the specified value.ClassRefItemfindOrAddClass(String className)Finds or adds a ClassRef entry (and its name Utf8) for the given internal class name.ClassRefItemfindOrAddClassRef(int nameIndex)Finds an existing ClassRefItem with the given name index or adds a new one.ConstantDynamicItemfindOrAddConstantDynamic(int bootstrapMethodAttrIndex, String name, String descriptor)Finds or adds a ConstantDynamic entry for the given bootstrap method and name-and-type.DoubleItemfindOrAddDouble(double value)Finds an existing DoubleItem with the given value or adds a new one to the constant pool.FieldRefItemfindOrAddField(String className, String fieldName, String fieldType)Finds an existing FieldRefItem for the specified class, field name, and field type, or adds a new one to the constant pool.FieldRefItemfindOrAddFieldRef(String owner, String name, String descriptor)Finds an existing FieldRefItem or creates a new one using string parameters.FloatItemfindOrAddFloat(float value)Finds an existing FloatItem with the given value or adds a new one to the constant pool.IntegerItemfindOrAddInteger(int value)Finds an existing IntegerItem with the given value or adds a new one to the constant pool.InterfaceRefItemfindOrAddInterfaceRef(int classIndex, int nameAndTypeIndex)Finds an existing InterfaceRefItem with the given class and name-and-type indices, or creates a new one if none exists.InterfaceRefItemfindOrAddInterfaceRef(String owner, String name, String descriptor)Finds an existing InterfaceRefItem or creates a new one using string parameters.LongItemfindOrAddLong(long value)Finds an existing LongItem with the given value or adds a new one to the constant pool.MethodHandleItemfindOrAddMethodHandle(int refKind, String owner, String name, String desc)Finds or adds a MethodHandle constant pool entry.MethodHandleItemfindOrAddMethodHandle(int refKind, String owner, String name, String desc, boolean ownerIsInterface)AsfindOrAddMethodHandle(int, String, String, String), with the owner's interface-ness made explicit.MethodRefItemfindOrAddMethodRef(int classIndex, int nameAndTypeIndex)Finds an existing MethodRefItem with the given class and nameAndType indices or adds a new one.MethodRefItemfindOrAddMethodRef(String owner, String name, String descriptor)Finds an existing MethodRefItem or creates a new one using string parameters.MethodTypeItemfindOrAddMethodType(String descriptor)Finds or adds a MethodType constant pool entry.NameAndTypeRefItemfindOrAddNameAndType(int nameIndex, int descIndex)Finds or adds a NameAndTypeRefItem with the specified name and descriptor indices.NameAndTypeRefItemfindOrAddNameAndType(String name, String descriptor)Finds or adds a NameAndType entry (and its name/descriptor Utf8s) for the given strings.StringRefItemfindOrAddString(String value)Finds or adds a String constant (and its backing Utf8) for the given value.Utf8ItemfindOrAddUtf8(String value)Finds or adds a Utf8Item with the specified value.ClassFilegetClassFile()StringgetClassName(int classIndex)Gets the class name from a ClassRefItem at the given constant pool index.intgetIndexOf(Item<?> item)Returns the pool index of the given item, by identity.Item<?>getItem(int index)Retrieves the constant pool item at the specified index.List<Item<?>>getItems()intredirectClassReferences(String fromInternal, String toInternal)Rewrites every constant-pool reference to classfromInternalso it resolves totoInternal, leaving no live constant namingfromInternal.intredirectOwner(String fromInternal, String toInternal)Canonicalizes every reference to classfromInternalso it namestoInternal- the complete form ofredirectClassReferences(java.lang.String, java.lang.String).voidsetClassFile(ClassFile classFile)Sets the ClassFile reference for this constant pool.StringtoString()intutf8Index(String value)Finds or adds a Utf8Item for the value and returns its constant-pool index.
-
-
-
Constructor Detail
-
ConstPool
public ConstPool(ClassFile classFile)
Constructs a ConstPool by parsing constant pool entries from the given ClassFile.- Parameters:
classFile- the ClassFile to read data from
-
ConstPool
public ConstPool()
Constructs an empty ConstPool.
-
-
Method Detail
-
getClassFile
public ClassFile getClassFile()
- Returns:
- the class file
-
setClassFile
public void setClassFile(ClassFile classFile)
Sets the ClassFile reference for this constant pool.- Parameters:
classFile- the ClassFile to associate with this pool
-
getItem
public Item<?> getItem(int index)
Retrieves the constant pool item at the specified index.- Parameters:
index- the 1-based index of the constant pool item- Returns:
- the constant pool item, or null if pointing to an unused slot
- Throws:
IllegalArgumentException- if the index is out of bounds
-
addItem
public int addItem(Item<?> newItem)
Adds a new item to the constant pool.- Parameters:
newItem- the item to add- Returns:
- the index where the item was added
- Throws:
IllegalStateException- if the ClassFile reference is not set
-
findOrAddClassRef
public ClassRefItem findOrAddClassRef(int nameIndex)
Finds an existing ClassRefItem with the given name index or adds a new one.- Parameters:
nameIndex- the index of the class name in the constant pool- Returns:
- the existing or newly added ClassRefItem
-
findOrAddMethodRef
public MethodRefItem findOrAddMethodRef(int classIndex, int nameAndTypeIndex)
Finds an existing MethodRefItem with the given class and nameAndType indices or adds a new one.- Parameters:
classIndex- The index of the class in the constant pool.nameAndTypeIndex- The index of the NameAndType in the constant pool.- Returns:
- The existing or newly added MethodRefItem.
-
findOrAddUtf8
public Utf8Item findOrAddUtf8(String value)
Finds or adds a Utf8Item with the specified value.- Parameters:
value- The UTF-8 string to find or add.- Returns:
- The existing or newly added Utf8Item.
-
utf8Index
public int utf8Index(String value)
Finds or adds a Utf8Item for the value and returns its constant-pool index.- Parameters:
value- The UTF-8 string to find or add.- Returns:
- the 1-based constant-pool index of the Utf8Item.
-
addUtf8
public Utf8Item addUtf8(String value)
Always adds a new Utf8Item with the specified value.- Parameters:
value- The UTF-8 string to add.- Returns:
- The newly added Utf8Item.
-
findOrAddNameAndType
public NameAndTypeRefItem findOrAddNameAndType(int nameIndex, int descIndex)
Finds or adds a NameAndTypeRefItem with the specified name and descriptor indices.- Parameters:
nameIndex- The index of the name Utf8Item.descIndex- The index of the descriptor Utf8Item.- Returns:
- The existing or newly added NameAndTypeRefItem.
-
findOrAddNameAndType
public NameAndTypeRefItem findOrAddNameAndType(String name, String descriptor)
Finds or adds a NameAndType entry (and its name/descriptor Utf8s) for the given strings.- Parameters:
name- the member namedescriptor- the member descriptor- Returns:
- the existing or newly added NameAndTypeRefItem
-
getIndexOf
public int getIndexOf(Item<?> item)
Returns the pool index of the given item, by identity.- Parameters:
item- the item to locate- Returns:
- the constant-pool index of the item
- Throws:
IllegalArgumentException- if the item is not in this pool
-
findOrAddDouble
public DoubleItem findOrAddDouble(double value)
Finds an existing DoubleItem with the given value or adds a new one to the constant pool.- Parameters:
value- The double value to find or add.- Returns:
- The existing or newly added DoubleItem.
-
findOrAddFloat
public FloatItem findOrAddFloat(float value)
Finds an existing FloatItem with the given value or adds a new one to the constant pool.- Parameters:
value- The float value to find or add.- Returns:
- The existing or newly added FloatItem.
-
findOrAddLong
public LongItem findOrAddLong(long value)
Finds an existing LongItem with the given value or adds a new one to the constant pool.- Parameters:
value- The long value to find or add.- Returns:
- The existing or newly added LongItem.
-
findOrAddInteger
public IntegerItem findOrAddInteger(int value)
Finds an existing IntegerItem with the given value or adds a new one to the constant pool.- Parameters:
value- The integer value to find or add.- Returns:
- The existing or newly added IntegerItem.
-
findOrAddField
public FieldRefItem findOrAddField(String className, String fieldName, String fieldType)
Finds an existing FieldRefItem for the specified class, field name, and field type, or adds a new one to the constant pool.- Parameters:
className- The fully qualified name of the class (e.g., "com/tonic/TestCase").fieldName- The name of the field (e.g., "testIntField").fieldType- The descriptor of the field (e.g., "I" for int).- Returns:
- The index of the FieldRefItem in the constant pool.
-
findOrAddString
public StringRefItem findOrAddString(String value)
Finds or adds a String constant (and its backing Utf8) for the given value.- Parameters:
value- the string value- Returns:
- the existing or newly added StringRefItem
-
findOrAddClass
public ClassRefItem findOrAddClass(String className)
Finds or adds a ClassRef entry (and its name Utf8) for the given internal class name.- Parameters:
className- the internal class name (e.g., "java/lang/Object")- Returns:
- the existing or newly added ClassRefItem
-
redirectOwner
public int redirectOwner(String fromInternal, String toInternal)
Canonicalizes every reference to classfromInternalso it namestoInternal- the complete form ofredirectClassReferences(java.lang.String, java.lang.String).- Parameters:
fromInternal- the current class internal nametoInternal- the new class internal name- Returns:
- the count of constants rewritten
-
redirectClassReferences
public int redirectClassReferences(String fromInternal, String toInternal)
Rewrites every constant-pool reference to classfromInternalso it resolves totoInternal, leaving no live constant namingfromInternal.- Parameters:
fromInternal- the current class internal nametoInternal- the new class internal name- Returns:
- the count of constants rewritten
-
findOrAddMethodRef
public MethodRefItem findOrAddMethodRef(String owner, String name, String descriptor)
Finds an existing MethodRefItem or creates a new one using string parameters.- Parameters:
owner- The fully qualified class name (e.g., "java/lang/String").name- The method name.descriptor- The method descriptor (e.g., "(II)I").- Returns:
- The existing or newly added MethodRefItem.
-
findOrAddFieldRef
public FieldRefItem findOrAddFieldRef(String owner, String name, String descriptor)
Finds an existing FieldRefItem or creates a new one using string parameters.- Parameters:
owner- The fully qualified class name (e.g., "java/lang/String").name- The field name.descriptor- The field descriptor (e.g., "I", "Ljava/lang/String;").- Returns:
- The existing or newly added FieldRefItem.
-
findOrAddInterfaceRef
public InterfaceRefItem findOrAddInterfaceRef(int classIndex, int nameAndTypeIndex)
Finds an existing InterfaceRefItem with the given class and name-and-type indices, or creates a new one if none exists.- Parameters:
classIndex- The constant pool index of the class reference.nameAndTypeIndex- The constant pool index of the name-and-type reference.- Returns:
- The existing or newly added InterfaceRefItem.
-
findOrAddInterfaceRef
public InterfaceRefItem findOrAddInterfaceRef(String owner, String name, String descriptor)
Finds an existing InterfaceRefItem or creates a new one using string parameters.- Parameters:
owner- The fully qualified interface name (e.g., "java/util/stream/Stream").name- The method name.descriptor- The method descriptor (e.g., "(Ljava/util/function/Consumer;)V").- Returns:
- The existing or newly added InterfaceRefItem.
-
getClassName
public String getClassName(int classIndex)
Gets the class name from a ClassRefItem at the given constant pool index.- Parameters:
classIndex- The constant pool index of the ClassRefItem.- Returns:
- The class name (internal form), or null if not found.
-
findOrAddMethodHandle
public MethodHandleItem findOrAddMethodHandle(int refKind, String owner, String name, String desc)
Finds or adds a MethodHandle constant pool entry.- Parameters:
refKind- The reference kind (1-9 per JVM spec).owner- The class containing the referenced member.name- The name of the referenced member.desc- The descriptor of the referenced member.- Returns:
- The MethodHandleItem.
-
findOrAddMethodHandle
public MethodHandleItem findOrAddMethodHandle(int refKind, String owner, String name, String desc, boolean ownerIsInterface)
AsfindOrAddMethodHandle(int, String, String, String), with the owner's interface-ness made explicit.- Parameters:
refKind- The reference kind, 1-4 for field handles and 5-9 for method handles.owner- The class containing the referenced member.name- The name of the referenced member.desc- The descriptor of the referenced member.ownerIsInterface- Whether the owner is an interface.- Returns:
- The MethodHandleItem.
-
findOrAddMethodType
public MethodTypeItem findOrAddMethodType(String descriptor)
Finds or adds a MethodType constant pool entry.- Parameters:
descriptor- The method descriptor (e.g., "(II)V").- Returns:
- The MethodTypeItem.
-
addMethodHandle
public int addMethodHandle(int referenceKind, int referenceIndex)Adds a MethodHandle constant pool entry by reference index.- Parameters:
referenceKind- The reference kind (1-9 per JVM spec).referenceIndex- The constant pool index of the referenced member.- Returns:
- The index of the newly added MethodHandleItem.
-
addMethodType
public int addMethodType(String descriptor)
Adds a MethodType constant pool entry.- Parameters:
descriptor- The method descriptor (e.g., "(II)V").- Returns:
- The index of the newly added MethodTypeItem.
-
addInvokeDynamic
public int addInvokeDynamic(int bootstrapMethodAttrIndex, int nameAndTypeIndex)Adds an InvokeDynamic constant pool entry.- Parameters:
bootstrapMethodAttrIndex- The index into the bootstrap methods table.nameAndTypeIndex- The constant pool index of the name and type.- Returns:
- The index of the newly added InvokeDynamicItem.
-
findOrAddConstantDynamic
public ConstantDynamicItem findOrAddConstantDynamic(int bootstrapMethodAttrIndex, String name, String descriptor)
Finds or adds a ConstantDynamic entry for the given bootstrap method and name-and-type.- Parameters:
bootstrapMethodAttrIndex- index into the bootstrap methods tablename- the constant namedescriptor- the constant field descriptor- Returns:
- the existing or newly added ConstantDynamicItem
-
addMethodRef
public int addMethodRef(String owner, String name, String descriptor)
Adds a MethodRef constant pool entry.- Parameters:
owner- The fully qualified class name (e.g., "java/lang/String").name- The method name.descriptor- The method descriptor (e.g., "(II)I").- Returns:
- The index of the newly added MethodRefItem.
-
-