net.sf.rej.java
Class ClassFile

java.lang.Object
  extended by net.sf.rej.java.ClassFile

public class ClassFile
extends java.lang.Object

ClassFile objects represent .class files. They're constructed either by parsing a byte array or a stream with net.sf.rej.java.Disassembler , using the Constructor or using the net.sf.rej.java.ClassFactory class.

Author:
Sami Koivu

Nested Class Summary
static class ClassFile.OffsetTag
           
 
Constructor Summary
ClassFile()
          Initializes this ClassFile object.
 
Method Summary
 void add(Field field)
          Adds a field to this class.
 void add(Method method)
          Adds a method to this class.
 boolean equals(java.lang.Object obj)
          Checks for equality.
 int getAccessFlags()
          Returns the class-level access flags of this class.
 java.lang.String getAccessString()
          Returns the class-level access modifiers of the class modeled by this object as a String.
 Attributes getAttributes()
          Returns an Attributes object, containing the class-level attributes of this class - if it has any.
 byte[] getData()
          Serializes this class.
 java.util.List<Field> getFields()
          Returns a read-only list of the fields in this class.
 java.lang.String getFullClassName()
          Returns the full class name, including the package definition part.
 java.util.List<Interface> getInterfaces()
          Returns a read-only list of the interfaces implemented by this class.
 java.lang.String getJavaVersionCompabilityString()
          Returns a String describing the Java version compability of this class.
 byte[] getMagic()
          Returns the default magic for Java class files.
 int getMajorVersion()
          Returns the major version of this class.
 java.util.List<Method> getMethods()
          Returns a read-only list of the methods in this class.
 int getMinorVersion()
          Returns the minor version of this class.
 java.util.Map<java.lang.Object,Range> getOffsetMap()
          Returns a map of offsets of each significant element of the class file.
 java.lang.String getPackageName()
          Returns the name of the package defined for the class presented by this object.
 ConstantPool getPool()
          Returns the ConstantPool object which models the constant pool associated with this ClassFile object.
 java.lang.String getShortClassName()
          Returns the short name of this class, in other words, the name without the package definition part.
 int getSuperClass()
          Returns the index to the constant pool ClassRef item which describes the parent class of the class represented by this ClassFile object.
 java.lang.String getSuperClassName()
          Returns the full class name of the parent class of the class modeled by this object.
 int getThisClass()
          Returns the index to the constant pool ClassRef item which describes the class represented by this ClassFile object.
 ClassVersion getVersion()
          Returns the major and minor version of this class in a ClassVersion object.
 int hashCode()
           
 void remove(Field field)
          Removes the given field from this class.
 void remove(Method method)
          Removes the given method from this class.
 void setAccessFlags(int accessFlags)
          Sets the class-level access flags of this class.
 void setAttributes(Attributes attributes)
          Sets the attributes object for this class, discarding the old one.
 void setFields(java.util.List<Field> fields)
          Sets all the fields for this class.
 void setInterfaces(java.util.List<Interface> interfaces)
          Sets all the interfaces for this class.
 void setMajorVersion(int majorVersion)
          Sets the major version of this class.
 void setMethods(java.util.List<Method> methods)
          Sets all the methods for this class.
 void setMinorVersion(int minorVersion)
          Sets the minor version of this class.
 void setPool(ConstantPool pool)
          Sets the constant pool for this class.
 void setSuperClass(int superClass)
          Sets the index to the constant pool ClassRef item which defines the parent class of the class modeled by this ClassFile object.
 void setThisClass(int thisClass)
          Sets the index to the constant pool ClassRef item which defines the class modeled by this ClassFile object.
 void setVersion(ClassVersion version)
          Sets the major and minor versions of this class.
 void validateMagic(byte[] magic)
          Validates the magic given as a parameter.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassFile

public ClassFile()
Initializes this ClassFile object. Note that this object is not yet valid for deserialization until several attributes of this class have been set.

Method Detail

getAccessFlags

public int getAccessFlags()
Returns the class-level access flags of this class. The int value is the result of a bit-wise AND operation of all the modifiers of this class. For a more thorough description and programmatically handling this value see net.sf.rej.java.AccessFlags.

Returns:
class-level access-flags.

getMajorVersion

public int getMajorVersion()
Returns the major version of this class. For a more thorough description of how the version maps into different Java compability settings, see the net.sf.rej.java.ClassVersion class.

Returns:
the major version of this class.

getMinorVersion

public int getMinorVersion()
Returns the minor version of this class. For a more thorough description of how the version maps into different Java compability settings, see the net.sf.rej.java.ClassVersion class.

Returns:
the minor version of this class.

setMajorVersion

public void setMajorVersion(int majorVersion)
Sets the major version of this class. For a more thorough description of how the version maps into different Java compability settings, see the net.sf.rej.java.ClassVersion class.

Parameters:
majorVersion - the new major version for this class.

setMinorVersion

public void setMinorVersion(int minorVersion)
Sets the minor version of this class. For a more thorough description of how the version maps into different Java compability settings, see the net.sf.rej.java.ClassVersion class.

Parameters:
minorVersion - the new minor version for this class.

getVersion

public ClassVersion getVersion()
Returns the major and minor version of this class in a ClassVersion object. For a more thorough description of how the version maps into different Java compability settings, see the net.sf.rej.java.ClassVersion class.

Returns:
the major and minor versions of this class.

setVersion

public void setVersion(ClassVersion version)
Sets the major and minor versions of this class.

Parameters:
version -

getMethods

public java.util.List<Method> getMethods()
Returns a read-only list of the methods in this class. The methods are returned as self-contained net.sf.rej.java.Method objects. Modifying this list does not affect this class. Modifying the methods contained in the list does affect this class.

Returns:
a list of Method objects describing the methods of this class.

add

public void add(Method method)
Adds a method to this class. For creating net.sf.rej.java.Method objects, see net.sf.rej.java.MethodFactory class.

Parameters:
method - a method (or static code block, or constructor) to add to this class.

add

public void add(Field field)
Adds a field to this class. For creating net.sf.rej.java.Field objects, see net.sf.rej.java.FieldFactory class.

Parameters:
field - a field to add to this class.

getInterfaces

public java.util.List<Interface> getInterfaces()
Returns a read-only list of the interfaces implemented by this class. The interfaces are returned as self-contained net.sf.rej.java.Interface objects. Modifying this list does not affect this class. Modifying the Interface objects contained in the list does affect this class.

Returns:
a list of Interface objects describing the interfaces implemented by this class.

getThisClass

public int getThisClass()
Returns the index to the constant pool ClassRef item which describes the class represented by this ClassFile object.

Returns:
an int value index to the constant pool.

getAttributes

public Attributes getAttributes()
Returns an Attributes object, containing the class-level attributes of this class - if it has any. The object returned is not a copy and any modifications to it or the attributes contained in it will affect this class.

Returns:
an Attributes object with the class-level attributes of this class.

getSuperClass

public int getSuperClass()
Returns the index to the constant pool ClassRef item which describes the parent class of the class represented by this ClassFile object.

Returns:
an int value index to the constant pool.

getFields

public java.util.List<Field> getFields()
Returns a read-only list of the fields in this class. The fields are returned as self-contained net.sf.rej.java.Field objects. Modifying the list returned will not affect this class. Modifying the fields contained in the list will affect this class.

Returns:
a list of Field objects describing the fields of this class.

getPool

public ConstantPool getPool()
Returns the ConstantPool object which models the constant pool associated with this ClassFile object. The object returned is not a copy and any modification to it will modify this class file as well.

Returns:
the constant pool.

getMagic

public byte[] getMagic()
Returns the default magic for Java class files. In other words, 0xcafebabe

Returns:
a byte array with the default class file magic.

setAccessFlags

public void setAccessFlags(int accessFlags)
Sets the class-level access flags of this class. The int value is the result of a bit-wise AND operation of all the modifiers of this class. For a more thorough description and programmatically handling this value see net.sf.rej.java.AccessFlags.

Parameters:
accessFlags - the class-level access-flags.

setMethods

public void setMethods(java.util.List<Method> methods)
Sets all the methods for this class. Any methods existing in this class prior to calling this method are discarded (provided, naturally, that they are not in the list and thus added again).

Parameters:
methods - the list of methods (or static code block, or constructor) for this class.

setInterfaces

public void setInterfaces(java.util.List<Interface> interfaces)
Sets all the interfaces for this class. Any interfaces existing in this class prior to calling this method are discarded (provided, naturally, that they are not in the list and thus added again).

Parameters:
interfaces - the list of interfaces for this class.

setThisClass

public void setThisClass(int thisClass)
Sets the index to the constant pool ClassRef item which defines the class modeled by this ClassFile object.

Parameters:
thisClass - index to the constant pool.

setAttributes

public void setAttributes(Attributes attributes)
Sets the attributes object for this class, discarding the old one.

Parameters:
attributes - an attributes object containing 0-n attributes.

setSuperClass

public void setSuperClass(int superClass)
Sets the index to the constant pool ClassRef item which defines the parent class of the class modeled by this ClassFile object.

Parameters:
superClass - index to the constant pool.

setFields

public void setFields(java.util.List<Field> fields)
Sets all the fields for this class. Any fields existing in this class prior to calling this method are discarded (provided, naturally, that they are not in the list and thus added again).

Parameters:
fields - the list of fields for this class.

setPool

public void setPool(ConstantPool pool)
Sets the constant pool for this class. The old constant pool is discarded.

Parameters:
pool - new constant pool.

validateMagic

public void validateMagic(byte[] magic)
                   throws java.lang.RuntimeException
Validates the magic given as a parameter. If the given magic is not the standard 0xcafebabe a RuntimeException is thrown.

Parameters:
magic - the magic to validate.
Throws:
java.lang.RuntimeException - the validation failed.

getData

public byte[] getData()
Serializes this class. The produced byte array is in the class file format. It may be written to a .class file and it may be loaded by a ClassLoader.

Returns:
the class data.

getOffsetMap

public java.util.Map<java.lang.Object,Range> getOffsetMap()
Returns a map of offsets of each significant element of the class file. The offsets returned by this method are only valid until this ClassFile object is modified. The keys in the map are of type OffsetTag, Interface, Field and Method.

Returns:
a map of element offsets in class file data.

getShortClassName

public java.lang.String getShortClassName()
Returns the short name of this class, in other words, the name without the package definition part. For example, for the class java.lang.String this method returns "String".

Returns:
the short name of the class.

getFullClassName

public java.lang.String getFullClassName()
Returns the full class name, including the package definition part. For example "net.sf.rej.java.ClassFile".

Returns:
the full name of this class.

getSuperClassName

public java.lang.String getSuperClassName()
Returns the full class name of the parent class of the class modeled by this object. For example, for a ClassFile object modeling the class java.lang.String this method will return "java.lang.Object".

Returns:
the full name of the super class of this class.

getAccessString

public java.lang.String getAccessString()
Returns the class-level access modifiers of the class modeled by this object as a String. For example "public final".

Returns:
the class-level access modifers.

getPackageName

public java.lang.String getPackageName()
Returns the name of the package defined for the class presented by this object. In the case where the class is in the default package, an empty String is returned.

Returns:
package.

remove

public void remove(Method method)
Removes the given method from this class. Since no equals method has been defined for the Method class the removal only works if the Method object given as parameter is one of the Method objects stored in this class's list of methods.

Parameters:
method - the method to remove.

remove

public void remove(Field field)
Removes the given field from this class. Since no equals method has been defined for the Field class the removal only works if the Field object given as parameter is one of the Field objects stored in this class's list of fields.

Parameters:
field - the field to remove.

getJavaVersionCompabilityString

public java.lang.String getJavaVersionCompabilityString()
Returns a String describing the Java version compability of this class. The value is determined from the major/minor version of the class file using the following specification:
The Java virtual machine implementation of Sun’s JDK release 1.0.2 supports class file format versions 45.0 through 45.3 inclusive. Sun’s JDK releases 1.1.X can support class file formats of versions in the range 45.0 through 45.65535 inclusive. For implementations of version 1.k of the Java 2 platform can support class file formats of versions in the range 45.0 through 44+k.0 inclusive.

Returns:
java version compability String such as "1.5".

equals

public boolean equals(java.lang.Object obj)
Checks for equality. At this moment the equality is checked by comparing the fully qualified names of the two ClassFile objects.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to compare to this object.
Returns:
true if the class files refer to the same FQN.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object