Class ClassFile
java.lang.Object
EDU.purdue.cs.bloat.file.ClassFile
- All Implemented Interfaces:
ClassInfo
-
Constructor Summary
ConstructorsConstructorDescriptionClassFile(int modifiers, int classIndex, int superClassIndex, int[] interfaceIndexes, List constants, ClassInfoLoader loader) Creates a newClassFilefrom scratch.ClassFile(File file, ClassInfoLoader loader, DataInputStream in) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionaddNewField(int modifiers, int typeIndex, int nameIndex) Creates a new field in this classfileaddNewField(int modifiers, int typeIndex, int nameIndex, int cvNameIndex, int constantValueIndex) Creates a new field in this classfileaddNewMethod(int modifiers, int typeIndex, int nameIndex, int exceptionIndex, int[] exceptionTypeIndices, int codeIndex) Creates a new method in this classintGet the index into the constant pool of the name of the class.voidcommit()Commit any changes back to a file in the output directory.voidcommitOnly(Set methods, Set fields) Commits only certain methods and fields.Constant[]Get an array of the constants in the constant pool.voiddeleteField(int nameIndex) Removes the field whose name is at the given index in the constant pool.voiddeleteMethod(int nameIndex, int typeIndex) Deletes a method from this classfields()Get an array of FieldInfo structures for each field in the class.file()Returns the File from which thisClassFilewas created.int[]Get the indices into the constant pool of the names of the class's interfaces.loader()Get the class info loader for the class.methods()Returns an array of MethodInfo structures for each method in the class.intGet the modifiers of the class.name()Get the name of the class, including the package name.Creates a new File object to hold this class.voidprint(PrintStream out) Prints a textual representation of this classfile to a PrintStream.voidprint(PrintWriter out) voidsetClassIndex(int index) Set the index into the constant pool of the name of the class.voidsetConstants(Constant[] constants) Set all the constants in the constant pool.voidsetInterfaceIndices(int[] indices) Set the indices into the constant pool of the names of the class's interfaces.voidsetMethods(MethodInfo[] methods) Sets the methods in this class.voidsetModifiers(int modifiers) Set the modifiers of the class.voidsetSuperclassIndex(int index) Set the index into the constant pool of the name of the class's superclass.intGet the index into the constant pool of the name of the class's superclass.toString()
-
Constructor Details
-
ClassFile
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 newClassFilefrom scratch. It has no fields or methods.- Parameters:
modifiers- The modifiers describing the newly-created classclassIndex- The index of the type of the newly-created class in its constant poolsuperClassIndex- The index of the type of the newly-created class's superclass in its constant poolinterfaceIndexes- The indexes of the types of the interfaces that the newly-created class implementsconstants- The constant pool for the newly created class (a list ofConstants).
-
-
Method Details
-
loader
Get the class info loader for the class. -
name
-
setClassIndex
public void setClassIndex(int index) Set the index into the constant pool of the name of the class.- Specified by:
setClassIndexin interfaceClassInfo- 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:
setSuperclassIndexin interfaceClassInfo- 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:
setInterfaceIndicesin interfaceClassInfo- 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:
classIndexin interfaceClassInfo- 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:
superclassIndexin interfaceClassInfo- 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:
interfaceIndicesin interfaceClassInfo- 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:
setModifiersin interfaceClassInfo- Parameters:
modifiers- A bit vector of modifier flags for the class.- See Also:
-
modifiers
-
fields
-
methods
Returns an array of MethodInfo structures for each method in the class. -
setMethods
Sets the methods in this class.- Specified by:
setMethodsin interfaceClassInfo
-
constants
-
setConstants
Set all the constants in the constant pool.- Specified by:
setConstantsin interfaceClassInfo- Parameters:
constants- The array of Constants.- See Also:
-
file
Returns the File from which thisClassFilewas created. If thisClassFilewas created from scratch,nullis returned. -
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
-
commitOnly
Description copied from interface:ClassInfoCommits only certain methods and fields.- Specified by:
commitOnlyin interfaceClassInfo- Parameters:
methods- Methods (MethodInfos) to commit. If null, all methods are committed.fields- Fields (FieldInfos) to commit. If null, all fields are committed.
-
addNewField
Creates a new field in this classfile- Specified by:
addNewFieldin interfaceClassInfo
-
addNewField
public FieldInfo addNewField(int modifiers, int typeIndex, int nameIndex, int cvNameIndex, int constantValueIndex) Creates a new field in this classfile- Specified by:
addNewFieldin interfaceClassInfo- 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:
deleteFieldin interfaceClassInfo- 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:
deleteMethodin interfaceClassInfo- Parameters:
nameIndex- Index in the constant pool of the name of the method to be deletedtypeIndex- Index in the constant pool of the type of the method to be deleted- Throws:
IllegalArgumentException- The class modeled by thisClassInfodoes 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:
addNewMethodin interfaceClassInfo- Parameters:
modifiers- Themodifiersfor the new methodtypeIndex- The index of the type (conglomeration of the parameter types and the return type) for this method in the class's constant poolnameIndex- The index of the name of the method in the class's constant poolexceptionIndex- The index of the UTF8 string "Exceptions" in the class's constant poolexceptionTypeIndices- The indices in the class's constant pool of the type of the exceptions thrown by this methodcodeIndex- The index of the UTF8 string "Code" in the class's constant pool
-
print
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. -
print
-
toString
-