edu.wisc.cs.java
Class ClassFile

java.lang.Object
  |
  +--edu.wisc.cs.java.Accessible
        |
        +--edu.wisc.cs.java.ClassFile

public class ClassFile
extends Accessible

This object maintains a Class_file table, as specified by the Java Virtual Machine specification for class files.


Field Summary
protected  AttributeInfo[] attributes
          An array of AttributeInfo objects which represent the general attributes of thie class.
protected  ConstantPool constants
          The constant pool for this class.
protected  java.util.Vector fields
          An array of FieldInfo objects which represent the fields of thie class.
protected  java.lang.String filename
          The name of the file from which this class was read.
protected  char[] interfaces
          An array of indeces into the constant pool which are the interfaces directly implemented by this class.
static int MAGIC
          All valid classfiles begin with this four byte number.
static int MAJOR_FILE_VERSION
          The major file version number this class was designed to read and write.
protected  int major_version
          The major version number for this class file.
protected  java.util.Vector methods
          An array of MethodInfo objects which represent the methods of thie class.
static int MINOR_FILE_VERSION
          The minor file version number this class was designed to read and write.
protected  int minor_version
          The minor version number for this class file.
protected  char super_class
          An index into the constant pool containing the symbolic reference to the super class.
protected  char this_class
          An index into the constant pool containing the symbolic reference to this class.
 
Fields inherited from class edu.wisc.cs.java.Accessible
ACC_ABSTRACT, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_SUPER, ACC_SYNCHRONIZED, ACC_TRANSIENT, ACC_VOLATILE, access_flags
 
Constructor Summary
ClassFile(java.io.File file)
          Constructs a new ClassFile object by reading data from the specified file.
 
Method Summary
 FieldInfo addField(int access, java.lang.String name, java.lang.String desc)
          Adds a new field to the class with the given access flags, name, and type.
 MethodInfo addMethod(int access, java.lang.String name, java.lang.String desc)
          Adds a new method to the class with the given access flags, name, and type.
 java.lang.String getClassName()
          Returns the name of the class.
 ConstantPool getConstantPool()
          Returns the constant pool for this class.
 java.util.Enumeration getFields()
          Returns an enumeration containing all of the fields for this class.
 java.lang.String getFileName()
          Returns the name of the file from which this class was read.
 int getMajorVersion()
          Returns the major version number of this class file.
 java.util.Enumeration getMethods()
          Returns an enumeration containing all of the methods for this class.
 int getMinorVersion()
          Returns the minor version number of this class file.
 java.lang.String getSuperClass()
          Returns the name of the superclass.
 void printData(java.io.PrintStream out)
          Writes textual information about the class represented by this ClassFile object to the specified destination.
 void writeClassFile(java.io.File f)
          Writes a class file according to the Java Virtual Machine specification.
 
Methods inherited from class edu.wisc.cs.java.Accessible
getFlag, setFlag, unSetFlag
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAGIC

public static final int MAGIC
All valid classfiles begin with this four byte number.

MAJOR_FILE_VERSION

public static final int MAJOR_FILE_VERSION
The major file version number this class was designed to read and write.

MINOR_FILE_VERSION

public static final int MINOR_FILE_VERSION
The minor file version number this class was designed to read and write.

filename

protected java.lang.String filename
The name of the file from which this class was read.

major_version

protected int major_version
The major version number for this class file.

minor_version

protected int minor_version
The minor version number for this class file.

this_class

protected char this_class
An index into the constant pool containing the symbolic reference to this class.

super_class

protected char super_class
An index into the constant pool containing the symbolic reference to the super class.

constants

protected ConstantPool constants
The constant pool for this class.

interfaces

protected char[] interfaces
An array of indeces into the constant pool which are the interfaces directly implemented by this class.

fields

protected java.util.Vector fields
An array of FieldInfo objects which represent the fields of thie class.

methods

protected java.util.Vector methods
An array of MethodInfo objects which represent the methods of thie class.

attributes

protected AttributeInfo[] attributes
An array of AttributeInfo objects which represent the general attributes of thie class.
Constructor Detail

ClassFile

public ClassFile(java.io.File file)
          throws java.io.EOFException
Constructs a new ClassFile object by reading data from the specified file. Note that this file is expected to have been verified as readable by this process by the caller.
Parameters:
filename - String containing the name a path of a class file.
Throws:
java.io.EOFException - if the end of file is encountered unexpectedly.
Method Detail

getFileName

public java.lang.String getFileName()
Returns the name of the file from which this class was read.
Returns:
the name of the file from which this class was read.

getMajorVersion

public int getMajorVersion()
Returns the major version number of this class file.
Returns:
the major version number of this class file.

getMinorVersion

public int getMinorVersion()
Returns the minor version number of this class file.
Returns:
the minor version number of this class file.

getConstantPool

public ConstantPool getConstantPool()
Returns the constant pool for this class.
Returns:
the constant pool for this class.

addField

public FieldInfo addField(int access,
                          java.lang.String name,
                          java.lang.String desc)
Adds a new field to the class with the given access flags, name, and type.
Parameters:
access - a bit field with the desired acess flags set.
name - the name of the new field.
desc - the descriptor for the new field.
Returns:
the new FieldInfo object.

addMethod

public MethodInfo addMethod(int access,
                            java.lang.String name,
                            java.lang.String desc)
Adds a new method to the class with the given access flags, name, and type.
Parameters:
access - a bit field with the desired acess flags set.
name - the name of the new field.
desc - the descriptor for the new field.
Returns:
the new MethodInfo object.

getFields

public java.util.Enumeration getFields()
Returns an enumeration containing all of the fields for this class.

getMethods

public java.util.Enumeration getMethods()
Returns an enumeration containing all of the methods for this class.

getSuperClass

public java.lang.String getSuperClass()
Returns the name of the superclass.
Returns:
a string containing the name of the superclass.

getClassName

public java.lang.String getClassName()
Returns the name of the class.
Returns:
a string containing the name of the class.

writeClassFile

public void writeClassFile(java.io.File f)
                    throws java.io.IOException
Writes a class file according to the Java Virtual Machine specification. Note that it is assumed that the file argument has been ok'ed for writing by the caller.
Parameters:
f - a file object representing the file on disk to write.
Throws:
java.io.IOException - if there is a problem writing the file.

printData

public void printData(java.io.PrintStream out)
Writes textual information about the class represented by this ClassFile object to the specified destination. The destination is usually System.out or System.err. This method is primarily for debugging purposes. Eventually, I would like to turn this into a robust Java disassembler, outputing the data in a format similar to java source code, but substituting the bytecode in the body of the methods.
Parameters:
out - a PrintStream connected to the desired destination.