Class ClassFile


  • public class ClassFile
    extends AbstractParser
    Represents a Java class file with full parsing and manipulation capabilities.
    • 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
      • getClassAttributes

        public List<Attribute> getClassAttributes()
        Returns:
        the class attributes
      • getInterfaces

        public List<Integer> getInterfaces()
        Returns:
        the interfaces
      • 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 field
        fieldDescriptor - 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 remove
        fieldDescriptor - 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 name
        methodDescriptor - 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 name
        descriptor - 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 class fromInternal in this class so it names toInternal - the complete form of redirectClassReferences(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 to fromInternal so it resolves to toInternal.
        Parameters:
        fromInternal - the current class internal name
        toInternal - the new class internal name
        Returns:
        the number of constants rewritten
      • 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 the BootstrapMethods attribute, creating the attribute if absent.
        Parameters:
        methodHandleIndex - constant-pool index of the bootstrap method handle
        arguments - 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 IOException
        Rebuilds 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 method
        methodName - the name of the method
        returnType - the return type as a Class object
        parameterTypes - 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 method
        methodName - the name of the method
        returnType - the return type as a descriptor string
        parameterTypes - 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 method
        methodName - the name of the method
        methodDescriptor - 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