Top Description Fields Constructors Methods
org.apache.bcel.classfile

public Class JavaClass

extends AccessFlags
implements Cloneable, Node, Comparable<JavaClass>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable, org.apache.bcel.classfile.Node, java.lang.Cloneable
Imports
java.io.ByteArrayOutputStream, .DataOutputStream, .File, .FileOutputStream, .IOException, .OutputStream, java.util.ArrayList, .Arrays, .List, .Objects, .Set, .StringTokenizer, .TreeSet, org.apache.bcel.Const, org.apache.bcel.generic.Type, org.apache.bcel.util.BCELComparator, .ClassQueue, .SyntheticRepository, org.apache.commons.lang3.ArrayUtils

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 programmatically generating classes should see the ClassGen class.
See Also
org.apache.bcel.generic.ClassGen

Field Summary

Modifier and TypeField and Description
private AnnotationEntry[]
private Attribute[]
private static BCELComparator<JavaClass>
private String
private int
private boolean
private ConstantPool
private static final boolean
public static final JavaClass[]
EMPTY_ARRAY

Empty array.

public static final String
EXTENSION

The standard class file extension.

private Field[]
public static final byte
private String
public static final byte
private String[]
private int[]
private boolean
private boolean
private int
private Method[]
private int
private final String
private transient Repository
repository

In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.

private byte
private String
private String
private int
public static final byte
Inherited from org.apache.bcel.classfile.AccessFlags:
access_flags

Constructor Summary

AccessConstructor and Description
public
JavaClass(final int
Class name
classNameIndex
,
final int
Superclass name
superclassNameIndex
,
final String
File name
fileName
,
final int
Major compiler version
major
,
final int
Minor compiler version
minor
,
final int
Access rights defined by bit flags
accessFlags
,
final ConstantPool
Array of constants
constantPool
,
final int[]
Implemented interfaces
interfaces
,
final Field[]
Class fields
fields
,
final Method[]
Class methods
methods
,
final Attribute[]
Class attributes
attributes
)

Constructor gets all contents as arguments.

public
JavaClass(final int
Index into constant pool referencing a ConstantClass that represents this class.
classNameIndex
,
final int
Index into constant pool referencing a ConstantClass that represents this class's superclass.
superclassNameIndex
,
final String
File name
fileName
,
final int
Major compiler version
major
,
final int
Minor compiler version
minor
,
final int
Access rights defined by bit flags
accessFlags
,
final ConstantPool
Array of constants
constantPool
,
int[]
Implemented interfaces
interfaces
,
Field[]
Class fields
fields
,
Method[]
Class methods
methods
,
Attribute[]
Class attributes
attributes
,
final byte
Read from file or generated in memory?
source
)

Constructor gets all contents as arguments.

Method Summary

Modifier and TypeMethod and Description
public void
accept(final Visitor
Visitor object
v
)

Implements org.apache.bcel.classfile.Node.accept.

Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
public int
compareTo(final JavaClass
the object to be compared.
obj
)

Implements java.lang.Comparable.compareTo.

Return the natural ordering of two JavaClasses.
private void
public JavaClass

Returns:

deep copy of this class
copy
()

pack-priv static void
Debug(final String str)

public void
dump(final DataOutputStream
Output stream
file
)

Dump Java class to output stream in binary format.

public void
dump(final File
Output file
file
)

Dump class to a file.

public void
dump(final OutputStream
Output stream
file
)

Dump Java class to output stream in binary format.

public void
dump(final String
Output file name
fileName
)

Dump class to a file named fileName.

public boolean
equals(final Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Return value as defined by given BCELComparator strategy.
public Field

Returns:

field matching given name and type, null if field is not found or not accessible from this class.
findField
(final String
the field name to find
fieldName
,
final Type
the field type to find
fieldType
)

Finds a visible field by name and type in this class and its super classes.

public JavaClass[]
getAllInterfaces()

Gets all interfaces implemented by this JavaClass (transitively).

public AnnotationEntry[]

Returns:

Annotations on the class
getAnnotationEntries
()

public Attribute[]

Returns:

Attributes of the class.
getAttributes
()

public byte[]

Returns:

class in binary format
getBytes
()

public String

Returns:

Class name.
getClassName
()

public int

Returns:

Class name index.
getClassNameIndex
()

public static BCELComparator<JavaClass>

Returns:

Comparison strategy object.
getComparator
()

public ConstantPool

Returns:

Constant pool.
getConstantPool
()

public Field[]

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.
getFields
()

public String

Returns:

File name of class, aka SourceFile attribute value
getFileName
()

public int[]

Returns:

Indices in constant pool of implemented interfaces.
getInterfaceIndices
()

public String[]

Returns:

Names of implemented interfaces.
getInterfaceNames
()

public JavaClass[]
getInterfaces()

Gets interfaces directly implemented by this JavaClass.

public int

Returns:

Major number of class file version.
getMajor
()

public Method

Returns:

A Method corresponding to java.lang.reflect.Method if any
getMethod
(final Method m)

public Method[]

Returns:

Methods of the class.
getMethods
()

public int

Returns:

Minor number of class file version.
getMinor
()

public String

Returns:

Package name.
getPackageName
()

public Repository
getRepository()

Gets the ClassRepository which holds its definition.

public final byte

Returns:

returns either HEAP (generated), FILE, or ZIP
getSource
()

public String

Returns:

file name where this class was read from
getSourceFileName
()

public String

Returns:

path to original source file of parsed class, relative to original source directory.
getSourceFilePath
()

Gets the source file path including the package path.

public JavaClass

Returns:

the superclass for this JavaClass object, or null if this is Object
getSuperClass
()

public JavaClass[]

Returns:

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

public String

Returns:

Superclass name.
getSuperclassName
()

returns the super class name of this class.

public int

Returns:

Class name index.
getSuperclassNameIndex
()

public int
hashCode()

Overrides java.lang.Object.hashCode.

Return value as defined by given BCELComparator strategy.
public boolean

Returns:

true, if this class is an implementation of interface inter
implementationOf
(final JavaClass inter)

private static String
indent(final Object obj)

public final boolean

Returns:

true if this JavaClass is derived from the super class
instanceOf
(final JavaClass superclass)

Equivalent to runtime "instanceof" operator.

public final boolean
public final boolean
public final boolean
public final boolean
public void
setAttributes(final Attribute[]
.
attributes
)

public void
setClassName(final String
.
className
)

public void
setClassNameIndex(final int
.
classNameIndex
)

public static void
setComparator(final BCELComparator<JavaClass>
Comparison strategy object.
comparator
)

public void
setConstantPool(final ConstantPool
.
constantPool
)

public void
setFields(final Field[]
.
fields
)

public void
setFileName(final String fileName)

Sets File name of class, aka SourceFile attribute value

public void
setInterfaceNames(final String[]
.
interfaceNames
)

public void
setInterfaces(final int[]
.
interfaces
)

public void
setMajor(final int
.
major
)

public void
setMethods(final Method[]
.
methods
)

public void
setMinor(final int
.
minor
)

public void
setRepository(final Repository repository)

Sets the ClassRepository which loaded the JavaClass.

public void
setSourceFileName(final String sourceFileName)

Sets absolute path to file this class was read from.

public void
setSuperclassName(final String
.
superclassName
)

public void
setSuperclassNameIndex(final int
.
superclassNameIndex
)

public String

Returns:

String representing class contents.
toString
()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from org.apache.bcel.classfile.AccessFlags:
getAccessFlagsgetModifiersisAbstractisAbstractisAnnotationisAnnotationisEnumisEnumisFinalisFinalisInterfaceisInterfaceisNativeisNativeisPrivateisPrivateisProtectedisProtectedisPublicisPublicisStaticisStaticisStrictfpisStrictfpisSynchronizedisSynchronizedisSyntheticisSyntheticisTransientisTransientisVarArgsisVarArgsisVolatileisVolatilesetAccessFlagssetModifiers

Field Detail

annotationsback to summary
private AnnotationEntry[] annotations
attributesback to summary
private Attribute[] attributes
bcelComparatorback to summary
private static BCELComparator<JavaClass> bcelComparator
classNameback to summary
private String className
classNameIndexback to summary
private int classNameIndex
computedNestedTypeStatusback to summary
private boolean computedNestedTypeStatus
constantPoolback to summary
private ConstantPool constantPool
debugback to summary
private static final boolean debug
EMPTY_ARRAYback to summary
public static final JavaClass[] EMPTY_ARRAY

Empty array.

Since
6.6.0
EXTENSIONback to summary
public static final String EXTENSION

The standard class file extension.

Since
6.7.0
fieldsback to summary
private Field[] fields
FILEback to summary
public static final byte FILE
fileNameback to summary
private String fileName
HEAPback to summary
public static final byte HEAP
interfaceNamesback to summary
private String[] interfaceNames
interfacesback to summary
private int[] interfaces
isAnonymousback to summary
private boolean isAnonymous
isNestedback to summary
private boolean isNested
majorback to summary
private int major
methodsback to summary
private Method[] methods
minorback to summary
private int minor
packageNameback to summary
private final String packageName
repositoryback to summary
private transient Repository repository

In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.

sourceback to summary
private byte source
sourceFileNameback to summary
private String sourceFileName
superclassNameback to summary
private String superclassName
superclassNameIndexback to summary
private int superclassNameIndex
ZIPback to summary
public static final byte ZIP

Constructor Detail

JavaClassback to summary
public JavaClass(final int classNameIndex, final int superclassNameIndex, final String fileName, final int major, final int minor, final int accessFlags, final ConstantPool constantPool, final int[] interfaces, final Field[] fields, final Method[] methods, final Attribute[] attributes)

Constructor gets all contents as arguments.

Parameters
classNameIndex:int

Class name

superclassNameIndex:int

Superclass name

fileName:String

File name

major:int

Major compiler version

minor:int

Minor compiler version

accessFlags:int

Access rights defined by bit flags

constantPool:ConstantPool

Array of constants

interfaces:int[]

Implemented interfaces

fields:Field[]

Class fields

methods:Method[]

Class methods

attributes:Attribute[]

Class attributes

JavaClassback to summary
public JavaClass(final int classNameIndex, final int superclassNameIndex, final String fileName, final int major, final int minor, final int accessFlags, final ConstantPool constantPool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes, final byte source)

Constructor gets all contents as arguments.

Parameters
classNameIndex:int

Index into constant pool referencing a ConstantClass that represents this class.

superclassNameIndex:int

Index into constant pool referencing a ConstantClass that represents this class's superclass.

fileName:String

File name

major:int

Major compiler version

minor:int

Minor compiler version

accessFlags:int

Access rights defined by bit flags

constantPool:ConstantPool

Array of constants

interfaces:int[]

Implemented interfaces

fields:Field[]

Class fields

methods:Method[]

Class methods

attributes:Attribute[]

Class attributes

source:byte

Read from file or generated in memory?

Method Detail

acceptback to summary
public void accept(final Visitor v)

Implements org.apache.bcel.classfile.Node.accept.

Called by objects that are traversing the nodes of the tree implicitly 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

Visitor object

Annotations
@Override
compareToback to summary
public int compareTo(final JavaClass obj)

Implements java.lang.Comparable.compareTo.

Return the natural ordering of two JavaClasses. This ordering is based on the class name

Parameters
obj:JavaClass

Doc from java.lang.Comparable.compareTo.

the object to be compared.

Returns:int

Doc from java.lang.Comparable.compareTo.

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
Since
6.0
computeNestedTypeStatusback to summary
private void computeNestedTypeStatus()
copyback to summary
public JavaClass copy()
Returns:JavaClass

deep copy of this class

Debugback to summary
pack-priv static void Debug(final String str)
dumpback to summary
public void dump(final DataOutputStream file) throws IOException

Dump Java class to output stream in binary format.

Parameters
file:DataOutputStream

Output stream

Exceptions
IOException:
if an I/O error occurs.
dumpback to summary
public void dump(final File file) throws IOException

Dump class to a file.

Parameters
file:File

Output file

Exceptions
IOException:
if an I/O error occurs.
dumpback to summary
public void dump(final OutputStream file) throws IOException

Dump Java class to output stream in binary format.

Parameters
file:OutputStream

Output stream

Exceptions
IOException:
if an I/O error occurs.
dumpback to summary
public void dump(final String fileName) throws IOException

Dump class to a file named fileName.

Parameters
fileName:String

Output file name

Exceptions
IOException:
if an I/O error occurs.
equalsback to summary
public boolean equals(final Object obj)

Overrides java.lang.Object.equals.

Return value as defined by given BCELComparator strategy. By default two JavaClass objects are said to be equal when their class names are equal.

Parameters
obj:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
See Also
Object#equals(Object)
findFieldback to summary
public Field findField(final String fieldName, final Type fieldType) throws ClassNotFoundException

Finds a visible field by name and type in this class and its super classes.

Parameters
fieldName:String

the field name to find

fieldType:Type

the field type to find

Returns:Field

field matching given name and type, null if field is not found or not accessible from this class.

Since
6.8.0
getAllInterfacesback to summary
public JavaClass[] getAllInterfaces() throws ClassNotFoundException

Gets all interfaces implemented by this JavaClass (transitively).

Exceptions
ClassNotFoundException:
if any of the class's superclasses or interfaces can't be found.
getAnnotationEntriesback to summary
public AnnotationEntry[] getAnnotationEntries()
Returns:AnnotationEntry[]

Annotations on the class

Since
6.0
getAttributesback to summary
public Attribute[] getAttributes()
Returns:Attribute[]

Attributes of the class.

getBytesback to summary
public byte[] getBytes()
Returns:byte[]

class in binary format

getClassNameback to summary
public String getClassName()
Returns:String

Class name.

getClassNameIndexback to summary
public int getClassNameIndex()
Returns:int

Class name index.

getComparatorback to summary
public static BCELComparator<JavaClass> getComparator()
Returns:BCELComparator<JavaClass>

Comparison strategy object.

getConstantPoolback to summary
public ConstantPool getConstantPool()
Returns:ConstantPool

Constant pool.

getFieldsback to summary
public Field[] getFields()
Returns:Field[]

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.

getFileNameback to summary
public String getFileName()
Returns:String

File name of class, aka SourceFile attribute value

getInterfaceIndicesback to summary
public int[] getInterfaceIndices()
Returns:int[]

Indices in constant pool of implemented interfaces.

getInterfaceNamesback to summary
public String[] getInterfaceNames()
Returns:String[]

Names of implemented interfaces.

getInterfacesback to summary
public JavaClass[] getInterfaces() throws ClassNotFoundException

Gets interfaces directly implemented by this JavaClass.

Exceptions
ClassNotFoundException:
if any of the class's interfaces can't be found.
getMajorback to summary
public int getMajor()
Returns:int

Major number of class file version.

getMethodback to summary
public Method getMethod(final Method m)
Returns:Method

A Method corresponding to java.lang.reflect.Method if any

getMethodsback to summary
public Method[] getMethods()
Returns:Method[]

Methods of the class.

getMinorback to summary
public int getMinor()
Returns:int

Minor number of class file version.

getPackageNameback to summary
public String getPackageName()
Returns:String

Package name.

getRepositoryback to summary
public Repository getRepository()

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

getSourceback to summary
public final byte getSource()
Returns:byte

returns either HEAP (generated), FILE, or ZIP

getSourceFileNameback to summary
public String getSourceFileName()
Returns:String

file name where this class was read from

getSourceFilePathback to summary
public String getSourceFilePath()

Gets the source file path including the package path.

Returns:String

path to original source file of parsed class, relative to original source directory.

Since
6.7.0
getSuperClassback to summary
public JavaClass getSuperClass() throws ClassNotFoundException
Returns:JavaClass

the superclass for this JavaClass object, or null if this is Object

Exceptions
ClassNotFoundException:
if the superclass can't be found
getSuperClassesback to summary
public JavaClass[] getSuperClasses() throws ClassNotFoundException
Returns:JavaClass[]

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

Exceptions
ClassNotFoundException:
if any of the superclasses can't be found
getSuperclassNameback to summary
public String getSuperclassName()

returns the super class name of this class. In the case that this class is Object, it will return itself (Object). This is probably incorrect but isn't fixed at this time to not break existing clients.

Returns:String

Superclass name.

getSuperclassNameIndexback to summary
public int getSuperclassNameIndex()
Returns:int

Class name index.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Return value as defined by given BCELComparator strategy. By default return the hash code of the class name.

Returns:int

Doc from java.lang.Object.hashCode.

a hash code value for this object.

Annotations
@Override
See Also
Object#hashCode()
implementationOfback to summary
public boolean implementationOf(final JavaClass inter) throws ClassNotFoundException
Returns:boolean

true, if this class is an implementation of interface inter

Exceptions
ClassNotFoundException:
if superclasses or superinterfaces of this class can't be found
indentback to summary
private static String indent(final Object obj)
instanceOfback to summary
public final boolean instanceOf(final JavaClass superclass) throws ClassNotFoundException

Equivalent to runtime "instanceof" operator.

Returns:boolean

true if this JavaClass is derived from the super class

Exceptions
ClassNotFoundException:
if superclasses or superinterfaces of this object can't be found
isAnonymousback to summary
public final boolean isAnonymous()
Since
6.0
isClassback to summary
public final boolean isClass()
isNestedback to summary
public final boolean isNested()
Since
6.0
isSuperback to summary
public final boolean isSuper()
setAttributesback to summary
public void setAttributes(final Attribute[] attributes)
Parameters
attributes:Attribute[]

.

setClassNameback to summary
public void setClassName(final String className)
Parameters
className:String

.

setClassNameIndexback to summary
public void setClassNameIndex(final int classNameIndex)
Parameters
classNameIndex:int

.

setComparatorback to summary
public static void setComparator(final BCELComparator<JavaClass> comparator)
Parameters
comparator:BCELComparator<JavaClass>

Comparison strategy object.

setConstantPoolback to summary
public void setConstantPool(final ConstantPool constantPool)
Parameters
constantPool:ConstantPool

.

setFieldsback to summary
public void setFields(final Field[] fields)
Parameters
fields:Field[]

.

setFileNameback to summary
public void setFileName(final String fileName)

Sets File name of class, aka SourceFile attribute value

setInterfaceNamesback to summary
public void setInterfaceNames(final String[] interfaceNames)
Parameters
interfaceNames:String[]

.

setInterfacesback to summary
public void setInterfaces(final int[] interfaces)
Parameters
interfaces:int[]

.

setMajorback to summary
public void setMajor(final int major)
Parameters
major:int

.

setMethodsback to summary
public void setMethods(final Method[] methods)
Parameters
methods:Method[]

.

setMinorback to summary
public void setMinor(final int minor)
Parameters
minor:int

.

setRepositoryback to summary
public void setRepository(final Repository repository)

Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.

setSourceFileNameback to summary
public void setSourceFileName(final String sourceFileName)

Sets absolute path to file this class was read from.

setSuperclassNameback to summary
public void setSuperclassName(final String superclassName)
Parameters
superclassName:String

.

setSuperclassNameIndexback to summary
public void setSuperclassNameIndex(final int superclassNameIndex)
Parameters
superclassNameIndex:int

.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

String representing class contents.

Annotations
@Override