sysModel.classFile
Class ClassFile

java.lang.Object
  extended by sysModel.classFile.ClassFile

public class ClassFile
extends java.lang.Object

Represents a Java class file.

Author:
Mathias Ricken

Field Summary
private  java.util.ArrayList<AAttributeInfo> _attributes
          Attributes of the class.
private  short _classAccessFlags
          Class access flags.
private  ConstantPool _constantPool
          Constant pool.
private  java.util.ArrayList<FieldInfo> _fields
          Fields in the class.
private  java.util.ArrayList<ClassPoolInfo> _interfaces
          Class information about implemented interfaces.
private  short _majorVersion
          Major version.
private  java.util.ArrayList<MethodInfo> _methods
          Methods in the class.
private  short _minorVersion
          Minor version.
private  ClassPoolInfo _superClass
          Class information about the superclass.
private  ClassPoolInfo _thisClass
          Class information about this class.
static int ACC_ABSTRACT
           
static int ACC_ANNOTATION
           
static int ACC_BRIDGE
           
static int ACC_ENUM
           
static int ACC_FINAL
           
static int ACC_INTERFACE
           
static int ACC_NATIVE
           
static int ACC_PRIVATE
           
static int ACC_PROTECTED
           
static int ACC_PUBLIC
           
static int ACC_STATIC
           
static int ACC_STRICT
           
static int ACC_SYNCHRONIZED
           
static int ACC_SYNTHETIC
           
static int ACC_TRANSIENT
           
static int ACC_VARARGS
           
static int ACC_VOLATILE
           
private static int JAVA_FILE_MAGIC
          Java file magic, 0xCAFEBABE.
 
Constructor Summary
ClassFile(byte[] b)
          Constructor.
ClassFile(java.io.InputStream in)
          Constructor.
ClassFile(short majorVersion, short minorVersion, short classAccessFlags, java.lang.String thisClassName, java.lang.String superClassName)
          Create a new ClassFile instance.
 
Method Summary
 void addAttribute(AAttributeInfo newAttribute)
          Add an attribute to the class.
 short[] addConstantPoolItems(APoolInfo[] items)
          Add new items to the constant pool.
 short addField(java.lang.String className, java.lang.String fieldName, java.lang.String fieldDescriptor, boolean addToFields, short accessFlags)
          Add the constant pool items for a field "fieldName:fieldDescriptor" in class "className".
 short addMethodToConstantPool(java.lang.String className, java.lang.String methodName, java.lang.String methodDescriptor)
          Add the constant pool items for a method "methodName:methodDescriptor" in class "className".
 short findMethodInConstantPool(java.lang.String className, java.lang.String methodName, java.lang.String methodDescriptor)
          Find the method info in the constant pool for a method "methodName:methodDescriptor" in class "className".
 AAttributeInfo getAttribute(java.lang.String name)
          Return the attribute with the specified name.
 java.util.ArrayList<AAttributeInfo> getAttributes()
          Accessor for the attributes list.
 short getClassAccessFlags()
          Accessor for the class access flags.
 ConstantPool getConstantPool()
          Accessor for the constant pool.
 APoolInfo getConstantPoolItem(short index)
          Return a constant pool item from this class.
 java.util.ArrayList<FieldInfo> getFields()
          Accessor for the fields list.
 java.util.ArrayList<ClassPoolInfo> getInterfaces()
          Accessor for the interface list.
 short getMajorVersion()
          Accessor for the major version.
 java.util.ArrayList<MethodInfo> getMethods()
          Accessor for the methods list.
 short getMinorVersion()
          Accessor for the minor version.
 ClassPoolInfo getSuperClass()
          Accessor for the superclass.
 java.lang.String getSuperClassName()
          Return the name of the super class or the empty string, if this class is java.lang.Object.
 ClassPoolInfo getThisClass()
          Accessor for this class.
 java.lang.String getThisClassName()
          Return the name of this class.
protected  void readAttributes(java.io.DataInputStream di)
          Read class file attributes.
protected  void readClassInfo(java.io.DataInputStream di)
          Read class information.
protected  void readConstantPool(java.io.DataInputStream di)
          Read constant pool.
protected  void readFields(java.io.DataInputStream di)
          Read class file fields.
protected  void readHeader(java.io.DataInputStream di)
          Read a class file header.
protected  void readInterfaces(java.io.DataInputStream di)
          Read class file interfaces.
protected  void readMethods(java.io.DataInputStream di)
          Read class file methods.
 void setClassAccessFlags(short classAccessFlags)
          Mutator for the class access flags.
 void setMajorVersion(short majorVersion)
          Mutator for the major version.
 void setMinorVersion(short minorVersion)
          Mutator for the minor version.
 void setSuperClass(ClassPoolInfo cpi)
          Mutator for the superclass.
 void setThisClass(ClassPoolInfo cpi)
          Mutator for the this class.
 java.lang.String toString()
          Return a human-readable version of this class.
 java.lang.String toStringVerbose()
          Return a verbose human-readable version of this class.
 java.lang.String toStringVerbose(boolean lineNumbers, boolean PCs)
          Return a verbose human-readable version of this class.
 void write(java.io.OutputStream out)
          Write class file into stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_majorVersion

private short _majorVersion
Major version.


_minorVersion

private short _minorVersion
Minor version.


_constantPool

private ConstantPool _constantPool
Constant pool.


_classAccessFlags

private short _classAccessFlags
Class access flags.


_thisClass

private ClassPoolInfo _thisClass
Class information about this class.


_superClass

private ClassPoolInfo _superClass
Class information about the superclass.


_interfaces

private java.util.ArrayList<ClassPoolInfo> _interfaces
Class information about implemented interfaces.


_fields

private java.util.ArrayList<FieldInfo> _fields
Fields in the class.


_methods

private java.util.ArrayList<MethodInfo> _methods
Methods in the class.


_attributes

private java.util.ArrayList<AAttributeInfo> _attributes
Attributes of the class.


ACC_PUBLIC

public static final int ACC_PUBLIC
See Also:
Constant Field Values

ACC_PRIVATE

public static final int ACC_PRIVATE
See Also:
Constant Field Values

ACC_PROTECTED

public static final int ACC_PROTECTED
See Also:
Constant Field Values

ACC_STATIC

public static final int ACC_STATIC
See Also:
Constant Field Values

ACC_FINAL

public static final int ACC_FINAL
See Also:
Constant Field Values

ACC_SYNCHRONIZED

public static final int ACC_SYNCHRONIZED
See Also:
Constant Field Values

ACC_VOLATILE

public static final int ACC_VOLATILE
See Also:
Constant Field Values

ACC_BRIDGE

public static final int ACC_BRIDGE
See Also:
Constant Field Values

ACC_TRANSIENT

public static final int ACC_TRANSIENT
See Also:
Constant Field Values

ACC_VARARGS

public static final int ACC_VARARGS
See Also:
Constant Field Values

ACC_NATIVE

public static final int ACC_NATIVE
See Also:
Constant Field Values

ACC_INTERFACE

public static final int ACC_INTERFACE
See Also:
Constant Field Values

ACC_ABSTRACT

public static final int ACC_ABSTRACT
See Also:
Constant Field Values

ACC_STRICT

public static final int ACC_STRICT
See Also:
Constant Field Values

ACC_SYNTHETIC

public static final int ACC_SYNTHETIC
See Also:
Constant Field Values

ACC_ANNOTATION

public static final int ACC_ANNOTATION
See Also:
Constant Field Values

ACC_ENUM

public static final int ACC_ENUM
See Also:
Constant Field Values

JAVA_FILE_MAGIC

private static final int JAVA_FILE_MAGIC
Java file magic, 0xCAFEBABE.

See Also:
Constant Field Values
Constructor Detail

ClassFile

public ClassFile(short majorVersion,
                 short minorVersion,
                 short classAccessFlags,
                 java.lang.String thisClassName,
                 java.lang.String superClassName)
Create a new ClassFile instance.

Parameters:
majorVersion - major class file version
minorVersion - minor class file version
classAccessFlags - class access flags
thisClassName - class name
superClassName - superclass name

ClassFile

public ClassFile(java.io.InputStream in)
          throws java.io.IOException,
                 java.lang.ClassFormatError
Constructor.

Parameters:
in - input stream with class file
Throws:
java.io.IOException
java.lang.ClassFormatError

ClassFile

public ClassFile(byte[] b)
          throws java.io.IOException,
                 java.lang.ClassFormatError
Constructor.

Parameters:
b - byte array
Throws:
java.io.IOException
java.lang.ClassFormatError
Method Detail

readHeader

protected void readHeader(java.io.DataInputStream di)
                   throws java.io.IOException,
                          java.lang.ClassFormatError
Read a class file header.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

readConstantPool

protected void readConstantPool(java.io.DataInputStream di)
                         throws java.io.IOException,
                                java.lang.ClassFormatError
Read constant pool.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

readClassInfo

protected void readClassInfo(java.io.DataInputStream di)
                      throws java.io.IOException,
                             java.lang.ClassFormatError
Read class information.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

readInterfaces

protected void readInterfaces(java.io.DataInputStream di)
                       throws java.io.IOException,
                              java.lang.ClassFormatError
Read class file interfaces.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

readFields

protected void readFields(java.io.DataInputStream di)
                   throws java.io.IOException,
                          java.lang.ClassFormatError
Read class file fields.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

readMethods

protected void readMethods(java.io.DataInputStream di)
                    throws java.io.IOException,
                           java.lang.ClassFormatError
Read class file methods.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

readAttributes

protected void readAttributes(java.io.DataInputStream di)
                       throws java.io.IOException,
                              java.lang.ClassFormatError
Read class file attributes.

Parameters:
di - stream
Throws:
java.io.IOException
java.lang.ClassFormatError

write

public void write(java.io.OutputStream out)
           throws java.io.IOException
Write class file into stream.

Parameters:
out - stream
Throws:
java.io.IOException

getThisClassName

public java.lang.String getThisClassName()
Return the name of this class.

Returns:
name of this class

getSuperClassName

public java.lang.String getSuperClassName()
Return the name of the super class or the empty string, if this class is java.lang.Object.

Returns:
name of super class

toString

public java.lang.String toString()
Return a human-readable version of this class.

Overrides:
toString in class java.lang.Object
Returns:
string

toStringVerbose

public java.lang.String toStringVerbose()
Return a verbose human-readable version of this class.

Returns:
verbose string

toStringVerbose

public java.lang.String toStringVerbose(boolean lineNumbers,
                                        boolean PCs)
Return a verbose human-readable version of this class.

Parameters:
lineNumbers - print line numbers
PCs - print PC values
Returns:
verbose string

getConstantPoolItem

public APoolInfo getConstantPoolItem(short index)
Return a constant pool item from this class. This reindexes the constant pool item to make sure the indices are still correct.

Parameters:
index - index of the item
Returns:
pool item, or null if out of range

addConstantPoolItems

public short[] addConstantPoolItems(APoolInfo[] items)
Add new items to the constant pool. The items must have been resolved already. The list is pruned to avoid adding items to the pool that are already there.

The function first goes through the list and identifies all those value items (int, long, float, double, ASCII, and Unicode) that are already in the pool. If an item is already in the pool, it is not added again. Furthermore, if an item is an ASCII or Unicode UTF item, then all the references to that item present in the list of new items are changed to point to the item in pool. This is only done for ASCII and Unicode UTF items since they are the only ones referenced from other items.

This process is repeated for all reference items (class, field, method, interface method, name-and-type, and string) in the list. Here, only name-and-type and class are referenced from other items, so references to them are changed to point to the pool if they are already in the pool.

Finally, items not yet in the pool are added.

Parameters:
items - array of new items
Returns:
array with indices of added items

addAttribute

public void addAttribute(AAttributeInfo newAttribute)
Add an attribute to the class.

Parameters:
newAttribute - new attribute

getAttribute

public AAttributeInfo getAttribute(java.lang.String name)
Return the attribute with the specified name.

Parameters:
name - attribute name
Returns:
attribute or null if not found

getMinorVersion

public short getMinorVersion()
Accessor for the minor version.

Returns:
minor version

setMinorVersion

public void setMinorVersion(short minorVersion)
Mutator for the minor version.

Parameters:
minorVersion - new minor version

getMajorVersion

public short getMajorVersion()
Accessor for the major version.

Returns:
major version

setMajorVersion

public void setMajorVersion(short majorVersion)
Mutator for the major version.

Parameters:
majorVersion - new major version

getClassAccessFlags

public short getClassAccessFlags()
Accessor for the class access flags.

Returns:
access flags

setClassAccessFlags

public void setClassAccessFlags(short classAccessFlags)
Mutator for the class access flags.

Parameters:
classAccessFlags - new flags

getSuperClass

public ClassPoolInfo getSuperClass()
Accessor for the superclass.

Returns:
superclass information

setSuperClass

public void setSuperClass(ClassPoolInfo cpi)
Mutator for the superclass.

Parameters:
cpi - superclass information

getThisClass

public ClassPoolInfo getThisClass()
Accessor for this class.

Returns:
this class

setThisClass

public void setThisClass(ClassPoolInfo cpi)
Mutator for the this class.

Parameters:
cpi - this class information

getInterfaces

public java.util.ArrayList<ClassPoolInfo> getInterfaces()
Accessor for the interface list.

Returns:
interface list

getFields

public java.util.ArrayList<FieldInfo> getFields()
Accessor for the fields list.

Returns:
fields list

getMethods

public java.util.ArrayList<MethodInfo> getMethods()
Accessor for the methods list.

Returns:
methods list

getAttributes

public java.util.ArrayList<AAttributeInfo> getAttributes()
Accessor for the attributes list.

Returns:
methods list

getConstantPool

public ConstantPool getConstantPool()
Accessor for the constant pool.

Returns:
methods list

addMethodToConstantPool

public short addMethodToConstantPool(java.lang.String className,
                                     java.lang.String methodName,
                                     java.lang.String methodDescriptor)
Add the constant pool items for a method "methodName:methodDescriptor" in class "className".

Parameters:
className - class name
methodName - method name
methodDescriptor - method descriptor
Returns:
constant pool index of method info

addField

public short addField(java.lang.String className,
                      java.lang.String fieldName,
                      java.lang.String fieldDescriptor,
                      boolean addToFields,
                      short accessFlags)
Add the constant pool items for a field "fieldName:fieldDescriptor" in class "className".

Parameters:
className - class name
fieldName - field name
fieldDescriptor - field descriptor
addToFields - true if a new field should be added to the fields list as well
accessFlags - access flags (only if addToFields is true)
Returns:
constant pool index of field info

findMethodInConstantPool

public short findMethodInConstantPool(java.lang.String className,
                                      java.lang.String methodName,
                                      java.lang.String methodDescriptor)
Find the method info in the constant pool for a method "methodName:methodDescriptor" in class "className".

Parameters:
className - class name
methodName - method name
methodDescriptor - method descriptor
Returns:
constant pool index of the method, or 0 if not found