Package com.tonic.parser
Class ClassFile
- java.lang.Object
-
- com.tonic.parser.AbstractParser
-
- com.tonic.parser.ClassFile
-
public class ClassFile extends AbstractParser
Represents a Java class file with full parsing and manipulation capabilities.
-
-
Field Summary
Fields Modifier and Type Field Description static intCLASS_MAGIC
-
Constructor Summary
Constructors Constructor Description ClassFile(InputStream inputStream)Creates a ClassFile from an InputStream.ClassFile(String className, int accessFlags)Creates a new, empty class file from scratch (no members).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(AbstractClassVisitor visitor)Dispatches the visitor over the constant pool, class attributes, fields, and methods.intaddBootstrapMethod(int methodHandleIndex, List<Integer> arguments)Find-or-adds a bootstrap method (its method-handle index plus static-argument indices, all in this class's pool) to theBootstrapMethodsattribute, creating the attribute if absent.voidaddInterface(String interfaceName)Adds a new interface to the interfaces list.FieldEntrycreateNewField(int accessFlags, String fieldName, String fieldDescriptor, List<Attribute> attributes)Adds a new field to the class.MethodEntrycreateNewMethod(int accessFlags, String methodName, Class<?> returnType, Class<?>... parameterTypes)Creates a new method with Class-based parameter types.MethodEntrycreateNewMethod(int accessFlags, String methodName, String returnType, String... parameterTypes)Creates a new method with String-based parameter types.MethodEntrycreateNewMethodWithDescriptor(int accessFlags, String methodName, String methodDescriptor)Creates a new method with a complete method descriptor.intgetAccess()BootstrapMethodsAttributegetBootstrapMethodsAttribute()Finds this class'sBootstrapMethodsAttribute.List<Attribute>getClassAttributes()StringgetClassName()Retrieves the class name from the constant pool.ClassPoolgetClassPool()ConstPoolgetConstPool()List<MethodEntry>getConstructors()FieldEntrygetField(String name)Finds the first field with the given name.List<FieldEntry>getFields()List<String>getInterfaceNames()Resolves the internal names of the directly-implemented interfaces.List<Integer>getInterfaces()intgetMajorVersion()MethodEntrygetMethod(String name)Finds the first method with the given name.MethodEntrygetMethod(String name, String descriptor)Finds the method with the exact name and descriptor.List<MethodEntry>getMethods()List<MethodEntry>getMethods(String name)Collects every method with the given name (the overload set).intgetMinorVersion()MethodEntrygetStaticInitializer()intgetSuperClass()StringgetSuperClassName()Retrieves the superclass name from the constant pool.intgetThisClass()intredirectClassReferences(String fromInternal, String toInternal)Rewrites every constant-pool reference tofromInternalso it resolves totoInternal.intredirectOwner(String fromInternal, String toInternal)Canonicalizes every reference to classfromInternalin this class so it namestoInternal- the complete form ofredirectClassReferences(java.lang.String, java.lang.String).booleanremoveField(String fieldName, String fieldDescriptor)Removes a field from the class.booleanremoveMethod(String methodName, String methodDescriptor)Removes a method by name and descriptor.StringresolveClassName(int classIndex)Resolves a constant-pool class reference to its internal name.voidsetClassName(String newName)Sets the class name (internal name like "com/tonic/TestClass").voidsetClassPool(ClassPool classPool)voidsetConstPool(ConstPool constPool)voidsetMajorVersion(int majorVersion)voidsetMinorVersion(int minorVersion)voidsetSuperClassName(String newSuperName)Sets the superclass name (internal name like "java/lang/Object").intstripStackMapTables()Removes the StackMapTable attribute from every method, so the class serializes "frameless".StringtoString()byte[]write()Rebuilds this ClassFile into a fresh byte array using the current in-memory state.-
Methods inherited from class com.tonic.parser.AbstractParser
getBytes, getIndex, getLength, readByte, readBytes, readDouble, readFloat, readInt, readLong, readShort, readUnsignedByte, readUnsignedInt, readUnsignedLong, readUnsignedShort, readUtf8, rebuild
-
-
-
-
Field Detail
-
CLASS_MAGIC
public static final int CLASS_MAGIC
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ClassFile
public ClassFile(InputStream inputStream) throws IOException
Creates a ClassFile from an InputStream.- Parameters:
inputStream- the input stream containing class file data- Throws:
IOException- if reading from the stream fails
-
ClassFile
public ClassFile(String className, int accessFlags)
Creates a new, empty class file from scratch (no members).- Parameters:
className- the internal class name (e.g., "com/example/MyClass")accessFlags- the class access flags
-
-
Method Detail
-
getMinorVersion
public int getMinorVersion()
- Returns:
- the minor version
-
getMajorVersion
public int getMajorVersion()
- Returns:
- the major version
-
getAccess
public int getAccess()
- Returns:
- the access
-
getThisClass
public int getThisClass()
- Returns:
- the this class
-
getSuperClass
public int getSuperClass()
- Returns:
- the super class
-
getConstPool
public ConstPool getConstPool()
- Returns:
- the const pool
-
getFields
public List<FieldEntry> getFields()
- Returns:
- the fields
-
getMethods
public List<MethodEntry> getMethods()
- Returns:
- the methods
-
getClassPool
public ClassPool getClassPool()
- Returns:
- the pool that loaded this class, if any, so cross-class lookups can resolve siblings
-
setMinorVersion
public void setMinorVersion(int minorVersion)
- Parameters:
minorVersion- the class file minor version
-
setMajorVersion
public void setMajorVersion(int majorVersion)
- Parameters:
majorVersion- the class file major version
-
setConstPool
public void setConstPool(ConstPool constPool)
- Parameters:
constPool- the constant pool to use
-
setClassPool
public void setClassPool(ClassPool classPool)
- Parameters:
classPool- the pool this class belongs to
-
getClassName
public String getClassName()
Retrieves the class name from the constant pool.- Returns:
- The class name as a String.
-
getSuperClassName
public String getSuperClassName()
Retrieves the superclass name from the constant pool.- Returns:
- The superclass name as a String.
-
setClassName
public void setClassName(String newName)
Sets the class name (internal name like "com/tonic/TestClass").- Parameters:
newName- the new internal class name (e.g., "com/tonic/NewName")
-
setSuperClassName
public void setSuperClassName(String newSuperName)
Sets the superclass name (internal name like "java/lang/Object").- Parameters:
newSuperName- the new internal name for the superclass (e.g., "java/lang/String")
-
addInterface
public void addInterface(String interfaceName)
Adds a new interface to the interfaces list.- Parameters:
interfaceName- the internal name of the interface (e.g., "java/util/List")
-
createNewField
public FieldEntry createNewField(int accessFlags, String fieldName, String fieldDescriptor, List<Attribute> attributes)
Adds a new field to the class.- Parameters:
accessFlags- the access flags for the field (e.g., Modifiers.PUBLIC)fieldName- the name of the fieldfieldDescriptor- the descriptor of the field (e.g., "Ljava/lang/String;")attributes- a list of attributes for the field (can be null)- Returns:
- the created FieldEntry
-
removeField
public boolean removeField(String fieldName, String fieldDescriptor)
Removes a field from the class.- Parameters:
fieldName- the name of the field to removefieldDescriptor- the descriptor of the field to remove- Returns:
- true if the field was found and removed
-
removeMethod
public boolean removeMethod(String methodName, String methodDescriptor)
Removes a method by name and descriptor.- Parameters:
methodName- the method namemethodDescriptor- the method descriptor- Returns:
- true if a matching method was removed
-
getMethod
public MethodEntry getMethod(String name)
Finds the first method with the given name.- Parameters:
name- the method name- Returns:
- the first matching method, or null if none
-
getMethod
public MethodEntry getMethod(String name, String descriptor)
Finds the method with the exact name and descriptor.- Parameters:
name- the method namedescriptor- the method descriptor- Returns:
- the matching method, or null if none
-
getMethods
public List<MethodEntry> getMethods(String name)
Collects every method with the given name (the overload set).- Parameters:
name- the method name- Returns:
- the matching methods, possibly empty
-
getConstructors
public List<MethodEntry> getConstructors()
- Returns:
- the constructors (
<init>methods)
-
getStaticInitializer
public MethodEntry getStaticInitializer()
- Returns:
- the static initializer (
<clinit>), or null if none
-
getField
public FieldEntry getField(String name)
Finds the first field with the given name.- Parameters:
name- the field name- Returns:
- the first matching field, or null if none
-
getInterfaceNames
public List<String> getInterfaceNames()
Resolves the internal names of the directly-implemented interfaces.- Returns:
- the interface internal names
-
redirectOwner
public int redirectOwner(String fromInternal, String toInternal)
Canonicalizes every reference to classfromInternalin this class so it namestoInternal- the complete form ofredirectClassReferences(java.lang.String, java.lang.String).- Parameters:
fromInternal- the current class internal name (e.g."pkg/A")toInternal- the new class internal name (e.g."pkg/B")- Returns:
- the number of constants rewritten
-
redirectClassReferences
public int redirectClassReferences(String fromInternal, String toInternal)
Rewrites every constant-pool reference tofromInternalso it resolves totoInternal.- Parameters:
fromInternal- the current class internal nametoInternal- the new class internal name- Returns:
- the number of constants rewritten
-
getBootstrapMethodsAttribute
public BootstrapMethodsAttribute getBootstrapMethodsAttribute()
Finds this class'sBootstrapMethodsAttribute.- Returns:
- the attribute, or null if the class has none
-
addBootstrapMethod
public int addBootstrapMethod(int methodHandleIndex, List<Integer> arguments)Find-or-adds a bootstrap method (its method-handle index plus static-argument indices, all in this class's pool) to theBootstrapMethodsattribute, creating the attribute if absent.- Parameters:
methodHandleIndex- constant-pool index of the bootstrap method handlearguments- constant-pool indices of the static bootstrap arguments- Returns:
- the index of the (existing or newly appended) bootstrap method
-
stripStackMapTables
public int stripStackMapTables()
Removes the StackMapTable attribute from every method, so the class serializes "frameless".- Returns:
- the number of StackMapTable attributes removed
-
write
public byte[] write() throws IOExceptionRebuilds this ClassFile into a fresh byte array using the current in-memory state.- Returns:
- a new byte array representing the updated class file
- Throws:
IOException- if an I/O error occurs while writing
-
resolveClassName
public String resolveClassName(int classIndex)
Resolves a constant-pool class reference to its internal name.- Parameters:
classIndex- constant-pool index of a class reference item- Returns:
- the internal class name, or a placeholder string when the index is not a valid class reference
-
createNewMethod
public MethodEntry createNewMethod(int accessFlags, String methodName, Class<?> returnType, Class<?>... parameterTypes)
Creates a new method with Class-based parameter types.- Parameters:
accessFlags- the access flags for the methodmethodName- the name of the methodreturnType- the return type as a Class objectparameterTypes- the parameter types as Class objects- Returns:
- the created MethodEntry
-
createNewMethod
public MethodEntry createNewMethod(int accessFlags, String methodName, String returnType, String... parameterTypes)
Creates a new method with String-based parameter types.- Parameters:
accessFlags- the access flags for the methodmethodName- the name of the methodreturnType- the return type as a descriptor stringparameterTypes- the parameter types as descriptor strings- Returns:
- the created MethodEntry
-
createNewMethodWithDescriptor
public MethodEntry createNewMethodWithDescriptor(int accessFlags, String methodName, String methodDescriptor)
Creates a new method with a complete method descriptor.- Parameters:
accessFlags- the access flags for the methodmethodName- the name of the methodmethodDescriptor- the complete method descriptor (e.g., "(Ljava/lang/String;I)V")- Returns:
- the created MethodEntry
-
accept
public void accept(AbstractClassVisitor visitor)
Dispatches the visitor over the constant pool, class attributes, fields, and methods.- Parameters:
visitor- the visitor to apply
-
-