org.apache.bcel.classfile

Class JavaClass

public class JavaClass extends AccessFlags implements Cloneable, Node, Comparable

Represents a Java class, i.e., the data structures, constant pool, fields, methods and commands contained in a Java .class file. See JVM specification for details. The intent of this class is to represent a parsed or otherwise existing class file. Those interested in programatically generating classes should see the ClassGen class.

See Also: ClassGen

Field Summary
static byteFILE
static byteHEAP
static byteZIP
Constructor Summary
JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes, byte source)
Constructor gets all contents as arguments.
JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes)
Constructor gets all contents as arguments.
Method Summary
voidaccept(Visitor v)
Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
intcompareTo(Object obj)
Return the natural ordering of two JavaClasses.
JavaClasscopy()
voiddump(File file)
Dump class to a file.
voiddump(String _file_name)
Dump class to a file named file_name.
voiddump(OutputStream file)
Dump Java class to output stream in binary format.
voiddump(DataOutputStream file)
Dump Java class to output stream in binary format.
booleanequals(Object obj)
Return value as defined by given BCELComparator strategy.
JavaClass[]getAllInterfaces()
Get all interfaces implemented by this JavaClass (transitively).
Attribute[]getAttributes()
byte[]getBytes()
StringgetClassName()
intgetClassNameIndex()
static BCELComparatorgetComparator()
ConstantPoolgetConstantPool()
Field[]getFields()
StringgetFileName()
int[]getInterfaceIndices()
String[]getInterfaceNames()
JavaClass[]getInterfaces()
Get interfaces directly implemented by this JavaClass.
intgetMajor()
MethodgetMethod(Method m)
Method[]getMethods()
intgetMinor()
StringgetPackageName()
RepositorygetRepository()
Gets the ClassRepository which holds its definition.
bytegetSource()
StringgetSourceFileName()
StringgetSuperclassName()
intgetSuperclassNameIndex()
JavaClassgetSuperClass()
JavaClass[]getSuperClasses()
inthashCode()
Return value as defined by given BCELComparator strategy.
booleanimplementationOf(JavaClass inter)
booleaninstanceOf(JavaClass super_class)
Equivalent to runtime "instanceof" operator.
booleanisClass()
booleanisSuper()
voidsetAttributes(Attribute[] attributes)
voidsetClassName(String class_name)
voidsetClassNameIndex(int class_name_index)
static voidsetComparator(BCELComparator comparator)
voidsetConstantPool(ConstantPool constant_pool)
voidsetFields(Field[] fields)
voidsetFileName(String file_name)
Set File name of class, aka SourceFile attribute value
voidsetInterfaceNames(String[] interface_names)
voidsetInterfaces(int[] interfaces)
voidsetMajor(int major)
voidsetMethods(Method[] methods)
voidsetMinor(int minor)
voidsetRepository(Repository repository)
Sets the ClassRepository which loaded the JavaClass.
voidsetSourceFileName(String source_file_name)
Set absolute path to file this class was read from.
voidsetSuperclassName(String superclass_name)
voidsetSuperclassNameIndex(int superclass_name_index)
StringtoString()

Field Detail

FILE

public static final byte FILE

HEAP

public static final byte HEAP

ZIP

public static final byte ZIP

Constructor Detail

JavaClass

public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes, byte source)
Constructor gets all contents as arguments.

Parameters: class_name_index Index into constant pool referencing a ConstantClass that represents this class. superclass_name_index Index into constant pool referencing a ConstantClass that represents this class's superclass. file_name File name major Major compiler version minor Minor compiler version access_flags Access rights defined by bit flags constant_pool Array of constants interfaces Implemented interfaces fields Class fields methods Class methods attributes Class attributes source Read from file or generated in memory?

JavaClass

public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes)
Constructor gets all contents as arguments.

Parameters: class_name_index Class name superclass_name_index Superclass name file_name File name major Major compiler version minor Minor compiler version access_flags Access rights defined by bit flags constant_pool Array of constants interfaces Implemented interfaces fields Class fields methods Class methods attributes Class attributes

Method Detail

accept

public void accept(Visitor v)
Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.

Parameters: v Visitor object

compareTo

public int compareTo(Object obj)
Return the natural ordering of two JavaClasses. This ordering is based on the class name

copy

public JavaClass copy()

Returns: deep copy of this class

dump

public void dump(File file)
Dump class to a file.

Parameters: file Output file

Throws: IOException

dump

public void dump(String _file_name)
Dump class to a file named file_name.

Parameters: _file_name Output file name

Throws: IOException

dump

public void dump(OutputStream file)
Dump Java class to output stream in binary format.

Parameters: file Output stream

Throws: IOException

dump

public void dump(DataOutputStream file)
Dump Java class to output stream in binary format.

Parameters: file Output stream

Throws: IOException

equals

public boolean equals(Object obj)
Return value as defined by given BCELComparator strategy. By default two JavaClass objects are said to be equal when their class names are equal.

See Also: java.lang.Object#equals(java.lang.Object)

getAllInterfaces

public JavaClass[] getAllInterfaces()
Get all interfaces implemented by this JavaClass (transitively).

getAttributes

public Attribute[] getAttributes()

Returns: Attributes of the class.

getBytes

public byte[] getBytes()

Returns: class in binary format

getClassName

public String getClassName()

Returns: Class name.

getClassNameIndex

public int getClassNameIndex()

Returns: Class name index.

getComparator

public static BCELComparator getComparator()

Returns: Comparison strategy object

getConstantPool

public ConstantPool getConstantPool()

Returns: Constant pool.

getFields

public Field[] getFields()

Returns: Fields, i.e., variables of the class. Like the JVM spec mandates for the classfile format, these fields are those specific to this class, and not those of the superclass or superinterfaces.

getFileName

public String getFileName()

Returns: File name of class, aka SourceFile attribute value

getInterfaceIndices

public int[] getInterfaceIndices()

Returns: Indices in constant pool of implemented interfaces.

getInterfaceNames

public String[] getInterfaceNames()

Returns: Names of implemented interfaces.

getInterfaces

public JavaClass[] getInterfaces()
Get interfaces directly implemented by this JavaClass.

getMajor

public int getMajor()

Returns: Major number of class file version.

getMethod

public Method getMethod(Method m)

Returns: A org.apache.bcel.classfile.Method corresponding to java.lang.reflect.Method if any

getMethods

public Method[] getMethods()

Returns: Methods of the class.

getMinor

public int getMinor()

Returns: Minor number of class file version.

getPackageName

public String getPackageName()

Returns: Package name.

getRepository

public Repository getRepository()
Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance();

getSource

public final byte getSource()

Returns: returns either HEAP (generated), FILE, or ZIP

getSourceFileName

public String getSourceFileName()

Returns: sbsolute path to file where this class was read from

getSuperclassName

public String getSuperclassName()

Returns: Superclass name.

getSuperclassNameIndex

public int getSuperclassNameIndex()

Returns: Class name index.

getSuperClass

public JavaClass getSuperClass()

Returns: the superclass for this JavaClass object, or null if this is java.lang.Object

Throws: ClassNotFoundException if the superclass can't be found

getSuperClasses

public JavaClass[] getSuperClasses()

Returns: list of super classes of this class in ascending order, i.e., java.lang.Object is always the last element

Throws: ClassNotFoundException if any of the superclasses can't be found

hashCode

public int hashCode()
Return value as defined by given BCELComparator strategy. By default return the hashcode of the class name.

See Also: java.lang.Object#hashCode()

implementationOf

public boolean implementationOf(JavaClass inter)

Returns: true, if this class is an implementation of interface inter

Throws: ClassNotFoundException if superclasses or superinterfaces of this class can't be found

instanceOf

public final boolean instanceOf(JavaClass super_class)
Equivalent to runtime "instanceof" operator.

Returns: true if this JavaClass is derived from the super class

Throws: ClassNotFoundException if superclasses or superinterfaces of this object can't be found

isClass

public final boolean isClass()

isSuper

public final boolean isSuper()

setAttributes

public void setAttributes(Attribute[] attributes)

Parameters: attributes .

setClassName

public void setClassName(String class_name)

Parameters: class_name .

setClassNameIndex

public void setClassNameIndex(int class_name_index)

Parameters: class_name_index .

setComparator

public static void setComparator(BCELComparator comparator)

Parameters: comparator Comparison strategy object

setConstantPool

public void setConstantPool(ConstantPool constant_pool)

Parameters: constant_pool .

setFields

public void setFields(Field[] fields)

Parameters: fields .

setFileName

public void setFileName(String file_name)
Set File name of class, aka SourceFile attribute value

setInterfaceNames

public void setInterfaceNames(String[] interface_names)

Parameters: interface_names .

setInterfaces

public void setInterfaces(int[] interfaces)

Parameters: interfaces .

setMajor

public void setMajor(int major)

Parameters: major .

setMethods

public void setMethods(Method[] methods)

Parameters: methods .

setMinor

public void setMinor(int minor)

Parameters: minor .

setRepository

public void setRepository(Repository repository)
Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.

setSourceFileName

public void setSourceFileName(String source_file_name)
Set absolute path to file this class was read from.

setSuperclassName

public void setSuperclassName(String superclass_name)

Parameters: superclass_name .

setSuperclassNameIndex

public void setSuperclassNameIndex(int superclass_name_index)

Parameters: superclass_name_index .

toString

public String toString()

Returns: String representing class contents.