Package EDU.purdue.cs.bloat.file
Class ClassFile
- java.lang.Object
-
- EDU.purdue.cs.bloat.file.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.
-
-
Constructor Summary
Constructors Constructor Description ClassFile(int modifiers, int classIndex, int superClassIndex, int[] interfaceIndexes, java.util.List constants, ClassInfoLoader loader)Creates a newClassFilefrom 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 FieldInfoaddNewField(int modifiers, int typeIndex, int nameIndex)Creates a new field in this classfileFieldInfoaddNewField(int modifiers, int typeIndex, int nameIndex, int cvNameIndex, int constantValueIndex)Creates a new field in this classfileMethodInfoaddNewMethod(int modifiers, int typeIndex, int nameIndex, int exceptionIndex, int[] exceptionTypeIndices, int codeIndex)Creates a new method in this classintclassIndex()Get 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(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.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 classFieldInfo[]fields()Get an array of FieldInfo structures for each field in the class.java.io.Filefile()Returns the File from which thisClassFilewas created.int[]interfaceIndices()Get the indices into the constant pool of the names of the class's interfaces.ClassInfoLoaderloader()Get the class info loader for the class.MethodInfo[]methods()Returns an array of MethodInfo structures for each method in the class.intmodifiers()Get the modifiers of the class.java.lang.Stringname()Get the name of the class, including the package name.java.io.FileoutputFile()Creates a new File object to hold this class.voidprint(java.io.PrintStream out)Prints a textual representation of this classfile to a PrintStream.voidprint(java.io.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.intsuperclassIndex()Get the index into the constant pool of the name of the class's superclass.java.lang.StringtoString()
-
-
-
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 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 Detail
-
loader
public ClassInfoLoader loader()
Get the class info loader for the class.
-
name
public java.lang.String name()
Get the name of the class, including the package 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
-
modifiers
public int modifiers()
Get the modifiers of the class. The values correspond to the constants in the Modifiers class.
-
fields
public FieldInfo[] fields()
Get an array of FieldInfo structures for each field in the class.
-
methods
public MethodInfo[] methods()
Returns an array of MethodInfo structures for each method in the class.
-
setMethods
public void setMethods(MethodInfo[] methods)
Sets the methods in this class.- Specified by:
setMethodsin interfaceClassInfo
-
constants
public Constant[] constants()
Get an array of the constants in the constant pool.
-
setConstants
public void setConstants(Constant[] constants)
Set all the constants in the constant pool.- Specified by:
setConstantsin interfaceClassInfo- Parameters:
constants- The array of Constants.- See Also:
Constant
-
file
public java.io.File file()
Returns the File from which thisClassFilewas created. If thisClassFilewas created from scratch,nullis 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.
-
commitOnly
public void commitOnly(java.util.Set methods, java.util.Set fields)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
public FieldInfo addNewField(int modifiers, int typeIndex, int nameIndex)
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 interfaceClassInfocvNameIndex- 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:
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:
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:
java.lang.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
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.
-
-