Class ClassFile

  • All Implemented Interfaces:
    ClassInfo

    public class ClassFile
    extends java.lang.Object
    implements ClassInfo
    ClassFile basically represents a Java classfile as it is found on disk. The classfile is modeled according to the Java Virtual Machine Specification. Methods are provided to edit the classfile at a very low level.
    See Also:
    Attribute, Constant, Field, Method
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassFile​(int modifiers, int classIndex, int superClassIndex, int[] interfaceIndexes, java.util.List constants, ClassInfoLoader loader)
      Creates a new ClassFile from scratch.
      ClassFile​(java.io.File file, ClassInfoLoader loader, java.io.DataInputStream in)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      FieldInfo addNewField​(int modifiers, int typeIndex, int nameIndex)
      Creates a new field in this classfile
      FieldInfo addNewField​(int modifiers, int typeIndex, int nameIndex, int cvNameIndex, int constantValueIndex)
      Creates a new field in this classfile
      MethodInfo addNewMethod​(int modifiers, int typeIndex, int nameIndex, int exceptionIndex, int[] exceptionTypeIndices, int codeIndex)
      Creates a new method in this class
      int classIndex()
      Get the index into the constant pool of the name of the class.
      void commit()
      Commit any changes back to a file in the output directory.
      void commitOnly​(java.util.Set methods, java.util.Set fields)
      Commits only certain methods and fields.
      Constant[] constants()
      Get an array of the constants in the constant pool.
      void deleteField​(int nameIndex)
      Removes the field whose name is at the given index in the constant pool.
      void deleteMethod​(int nameIndex, int typeIndex)
      Deletes a method from this class
      FieldInfo[] fields()
      Get an array of FieldInfo structures for each field in the class.
      java.io.File file()
      Returns the File from which this ClassFile was created.
      int[] interfaceIndices()
      Get the indices into the constant pool of the names of the class's interfaces.
      ClassInfoLoader loader()
      Get the class info loader for the class.
      MethodInfo[] methods()
      Returns an array of MethodInfo structures for each method in the class.
      int modifiers()
      Get the modifiers of the class.
      java.lang.String name()
      Get the name of the class, including the package name.
      java.io.File outputFile()
      Creates a new File object to hold this class.
      void print​(java.io.PrintStream out)
      Prints a textual representation of this classfile to a PrintStream.
      void print​(java.io.PrintWriter out)  
      void setClassIndex​(int index)
      Set the index into the constant pool of the name of the class.
      void setConstants​(Constant[] constants)
      Set all the constants in the constant pool.
      void setInterfaceIndices​(int[] indices)
      Set the indices into the constant pool of the names of the class's interfaces.
      void setMethods​(MethodInfo[] methods)
      Sets the methods in this class.
      void setModifiers​(int modifiers)
      Set the modifiers of the class.
      void setSuperclassIndex​(int index)
      Set the index into the constant pool of the name of the class's superclass.
      int superclassIndex()
      Get the index into the constant pool of the name of the class's superclass.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ClassFile

        public ClassFile​(java.io.File file,
                         ClassInfoLoader loader,
                         java.io.DataInputStream in)
        Constructor. This constructor parses the class file from the input stream.
        Parameters:
        file - The file in which the class resides.
        loader - The class info loader which loaded the class.
        in - The data stream containing the class.
        Throws:
        java.lang.ClassFormatError - When the class could not be parsed.
      • ClassFile

        public ClassFile​(int modifiers,
                         int classIndex,
                         int superClassIndex,
                         int[] interfaceIndexes,
                         java.util.List constants,
                         ClassInfoLoader loader)
        Creates a new ClassFile from scratch. It has no fields or methods.
        Parameters:
        modifiers - The modifiers describing the newly-created class
        classIndex - The index of the type of the newly-created class in its constant pool
        superClassIndex - The index of the type of the newly-created class's superclass in its constant pool
        interfaceIndexes - The indexes of the types of the interfaces that the newly-created class implements
        constants - The constant pool for the newly created class (a list of Constants).
    • Method Detail

      • loader

        public ClassInfoLoader loader()
        Get the class info loader for the class.
        Specified by:
        loader in interface ClassInfo
        Returns:
        The class info loader for the class.
      • name

        public java.lang.String name()
        Get the name of the class, including the package name.
        Specified by:
        name in interface ClassInfo
        Returns:
        The name of the class.
      • setClassIndex

        public void setClassIndex​(int index)
        Set the index into the constant pool of the name of the class.
        Specified by:
        setClassIndex in interface ClassInfo
        Parameters:
        index - The index of the name of the class.
      • setSuperclassIndex

        public void setSuperclassIndex​(int index)
        Set the index into the constant pool of the name of the class's superclass.
        Specified by:
        setSuperclassIndex in interface ClassInfo
        Parameters:
        index - The index of the name of the superclass.
      • setInterfaceIndices

        public void setInterfaceIndices​(int[] indices)
        Set the indices into the constant pool of the names of the class's interfaces.
        Specified by:
        setInterfaceIndices in interface ClassInfo
        Parameters:
        indices - The indices of the names of the interfaces.
      • classIndex

        public int classIndex()
        Get the index into the constant pool of the name of the class.
        Specified by:
        classIndex in interface ClassInfo
        Returns:
        The index of the name of the class.
      • superclassIndex

        public int superclassIndex()
        Get the index into the constant pool of the name of the class's superclass.
        Specified by:
        superclassIndex in interface ClassInfo
        Returns:
        The index of the name of the superclass.
      • interfaceIndices

        public int[] interfaceIndices()
        Get the indices into the constant pool of the names of the class's interfaces.
        Specified by:
        interfaceIndices in interface ClassInfo
        Returns:
        The indices of the names of the interfaces.
      • setModifiers

        public void setModifiers​(int modifiers)
        Set the modifiers of the class. The values correspond to the constants in the Modifiers class.
        Specified by:
        setModifiers in interface ClassInfo
        Parameters:
        modifiers - A bit vector of modifier flags for the class.
        See Also:
        Modifiers
      • modifiers

        public int modifiers()
        Get the modifiers of the class. The values correspond to the constants in the Modifiers class.
        Specified by:
        modifiers in interface ClassInfo
        Returns:
        A bit vector of modifier flags for the class.
        See Also:
        Modifiers
      • fields

        public FieldInfo[] fields()
        Get an array of FieldInfo structures for each field in the class.
        Specified by:
        fields in interface ClassInfo
        Returns:
        An array of FieldInfo structures.
        See Also:
        FieldInfo
      • methods

        public MethodInfo[] methods()
        Returns an array of MethodInfo structures for each method in the class.
        Specified by:
        methods in interface ClassInfo
      • setMethods

        public void setMethods​(MethodInfo[] methods)
        Sets the methods in this class.
        Specified by:
        setMethods in interface ClassInfo
      • constants

        public Constant[] constants()
        Get an array of the constants in the constant pool.
        Specified by:
        constants in interface ClassInfo
        Returns:
        An array of Constants.
      • setConstants

        public void setConstants​(Constant[] constants)
        Set all the constants in the constant pool.
        Specified by:
        setConstants in interface ClassInfo
        Parameters:
        constants - The array of Constants.
        See Also:
        Constant
      • file

        public java.io.File file()
        Returns the File from which this ClassFile was created. If this ClassFile was created from scratch, null is returned.
      • outputFile

        public java.io.File outputFile()
        Creates a new File object to hold this class. It is placed in the output directory and has the name of the class represented by this ClassFile followed by the .class extension.
      • commit

        public void commit()
        Commit any changes back to a file in the output directory. The output directory is determined from the ClassFileLoader.
        Specified by:
        commit in interface ClassInfo
      • commitOnly

        public void commitOnly​(java.util.Set methods,
                               java.util.Set fields)
        Description copied from interface: ClassInfo
        Commits only certain methods and fields.
        Specified by:
        commitOnly in interface ClassInfo
        Parameters:
        methods - Methods (MethodInfos) to commit. If null, all methods are committed.
        fields - Fields (FieldInfos) to commit. If null, all fields are committed.
      • addNewField

        public FieldInfo addNewField​(int modifiers,
                                     int typeIndex,
                                     int nameIndex)
        Creates a new field in this classfile
        Specified by:
        addNewField in interface ClassInfo
      • addNewField

        public FieldInfo addNewField​(int modifiers,
                                     int typeIndex,
                                     int nameIndex,
                                     int cvNameIndex,
                                     int constantValueIndex)
        Creates a new field in this classfile
        Specified by:
        addNewField in interface ClassInfo
        cvNameIndex - The index in the class's constant pool for the UTF8 constant "ConstantValue"
        constantValueIndex - The index in the class's constant pool for the constant value
      • deleteField

        public void deleteField​(int nameIndex)
        Removes the field whose name is at the given index in the constant pool.
        Specified by:
        deleteField in interface ClassInfo
        Parameters:
        nameIndex - Index in the constant pool of the name of the field to be deleted
        Throws:
        java.lang.IllegalArgumentException - The class does not contain a field whose name is at the given index
      • deleteMethod

        public void deleteMethod​(int nameIndex,
                                 int typeIndex)
        Deletes a method from this class
        Specified by:
        deleteMethod in interface ClassInfo
        Parameters:
        nameIndex - Index in the constant pool of the name of the method to be deleted
        typeIndex - Index in the constant pool of the type of the method to be deleted
        Throws:
        java.lang.IllegalArgumentException - The class modeled by this ClassInfo does not contain a method whose name and type are not at the given indices
      • addNewMethod

        public MethodInfo addNewMethod​(int modifiers,
                                       int typeIndex,
                                       int nameIndex,
                                       int exceptionIndex,
                                       int[] exceptionTypeIndices,
                                       int codeIndex)
        Creates a new method in this class
        Specified by:
        addNewMethod in interface ClassInfo
        Parameters:
        modifiers - The modifiers for the new method
        typeIndex - The index of the type (conglomeration of the parameter types and the return type) for this method in the class's constant pool
        nameIndex - The index of the name of the method in the class's constant pool
        exceptionIndex - The index of the UTF8 string "Exceptions" in the class's constant pool
        exceptionTypeIndices - The indices in the class's constant pool of the type of the exceptions thrown by this method
        codeIndex - The index of the UTF8 string "Code" in the class's constant pool
      • print

        public void print​(java.io.PrintStream out)
        Prints a textual representation of this classfile to a PrintStream. The text includes information such as the constants in the constant pool, the name of the class's superclass, its modifier flags, its fields, and its methods.
        Specified by:
        print in interface ClassInfo
        Parameters:
        out - The stream to which to print.
      • print

        public void print​(java.io.PrintWriter out)
        Specified by:
        print in interface ClassInfo
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface ClassInfo
        Overrides:
        toString in class java.lang.Object