Class ClassFile

java.lang.Object
EDU.purdue.cs.bloat.file.ClassFile
All Implemented Interfaces:
ClassInfo

public class ClassFile extends 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:
  • Constructor Summary

    Constructors
    Constructor
    Description
    ClassFile(int modifiers, int classIndex, int superClassIndex, int[] interfaceIndexes, List constants, ClassInfoLoader loader)
    Creates a new ClassFile from scratch.
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    addNewField(int modifiers, int typeIndex, int nameIndex)
    Creates a new field in this classfile
    addNewField(int modifiers, int typeIndex, int nameIndex, int cvNameIndex, int constantValueIndex)
    Creates a new field in this classfile
    addNewMethod(int modifiers, int typeIndex, int nameIndex, int exceptionIndex, int[] exceptionTypeIndices, int codeIndex)
    Creates a new method in this class
    int
    Get the index into the constant pool of the name of the class.
    void
    Commit any changes back to a file in the output directory.
    void
    commitOnly(Set methods, Set fields)
    Commits only certain methods and fields.
    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
    Get an array of FieldInfo structures for each field in the class.
    Returns the File from which this ClassFile was created.
    int[]
    Get the indices into the constant pool of the names of the class's interfaces.
    Get the class info loader for the class.
    Returns an array of MethodInfo structures for each method in the class.
    int
    Get the modifiers of the class.
    Get the name of the class, including the package name.
    Creates a new File object to hold this class.
    void
    Prints a textual representation of this classfile to a PrintStream.
    void
     
    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
    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
    Get the index into the constant pool of the name of the class's superclass.
     

    Methods inherited from class Object

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

    • ClassFile

      public ClassFile(File file, ClassInfoLoader loader, 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:
      ClassFormatError - When the class could not be parsed.
    • ClassFile

      public ClassFile(int modifiers, int classIndex, int superClassIndex, int[] interfaceIndexes, 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 Details

    • 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 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

      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:
    • 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:
    • 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:
    • file

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

      public 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(Set methods, 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
      Parameters:
      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:
      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:
      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(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(PrintWriter out)
      Specified by:
      print in interface ClassInfo
    • toString

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