Package org.apache.bcel.classfile
Class JavaClass
java.lang.Object
org.apache.bcel.classfile.AccessFlags
org.apache.bcel.classfile.JavaClass
- All Implemented Interfaces:
Cloneable
,Comparable<JavaClass>
,Node
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AnnotationEntry[]
private Attribute[]
private static BCELComparator
<JavaClass> private String
private int
private boolean
private ConstantPool
private static final boolean
static final JavaClass[]
Empty array.static final String
The standard class file extension.private Field[]
static final byte
private String
static final byte
private String[]
private int[]
private boolean
private boolean
private int
private Method[]
private int
private final String
private 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
static final byte
Fields inherited from class org.apache.bcel.classfile.AccessFlags
access_flags
-
Constructor Summary
ConstructorsConstructorDescriptionJavaClass
(int classNameIndex, int superclassNameIndex, String fileName, int major, int minor, int accessFlags, ConstantPool constantPool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes) Constructor gets all contents as arguments.JavaClass
(int classNameIndex, int superclassNameIndex, String fileName, int major, int minor, int accessFlags, ConstantPool constantPool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes, byte source) Constructor gets all contents as arguments. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.int
Return the natural ordering of two JavaClasses.private void
copy()
(package private) static void
void
dump
(DataOutputStream file) Dump Java class to output stream in binary format.void
Dump class to a file.void
dump
(OutputStream file) Dump Java class to output stream in binary format.void
Dump class to a file named fileName.boolean
Return value as defined by given BCELComparator strategy.Finds a visible field by name and type in this class and its super classes.Gets all interfaces implemented by this JavaClass (transitively).byte[]
getBytes()
int
static BCELComparator
<JavaClass> Field[]
int[]
String[]
Gets interfaces directly implemented by this JavaClass.int
getMajor()
Method[]
int
getMinor()
Gets the ClassRepository which holds its definition.final byte
Gets the source file path including the package path.returns the super class name of this class.int
int
hashCode()
Return value as defined by given BCELComparator strategy.boolean
implementationOf
(JavaClass inter) private static String
final boolean
instanceOf
(JavaClass superclass) Equivalent to runtime "instanceof" operator.final boolean
final boolean
isClass()
final boolean
isNested()
final boolean
isSuper()
void
setAttributes
(Attribute[] attributes) void
setClassName
(String className) void
setClassNameIndex
(int classNameIndex) static void
setComparator
(BCELComparator<JavaClass> comparator) void
setConstantPool
(ConstantPool constantPool) void
void
setFileName
(String fileName) Sets File name of class, aka SourceFile attribute valuevoid
setInterfaceNames
(String[] interfaceNames) void
setInterfaces
(int[] interfaces) void
setMajor
(int major) void
setMethods
(Method[] methods) void
setMinor
(int minor) void
setRepository
(Repository repository) Sets the ClassRepository which loaded the JavaClass.void
setSourceFileName
(String sourceFileName) Sets absolute path to file this class was read from.void
setSuperclassName
(String superclassName) void
setSuperclassNameIndex
(int superclassNameIndex) toString()
Methods inherited from class org.apache.bcel.classfile.AccessFlags
getAccessFlags, getModifiers, isAbstract, isAbstract, isAnnotation, isAnnotation, isEnum, isEnum, isFinal, isFinal, isInterface, isInterface, isNative, isNative, isPrivate, isPrivate, isProtected, isProtected, isPublic, isPublic, isStatic, isStatic, isStrictfp, isStrictfp, isSynchronized, isSynchronized, isSynthetic, isSynthetic, isTransient, isTransient, isVarArgs, isVarArgs, isVolatile, isVolatile, setAccessFlags, setModifiers
-
Field Details
-
EXTENSION
The standard class file extension.- Since:
- 6.7.0
- See Also:
-
EMPTY_ARRAY
Empty array.- Since:
- 6.6.0
-
HEAP
public static final byte HEAP- See Also:
-
FILE
public static final byte FILE- See Also:
-
ZIP
public static final byte ZIP- See Also:
-
debug
private static final boolean debug -
bcelComparator
-
fileName
-
packageName
-
sourceFileName
-
classNameIndex
private int classNameIndex -
superclassNameIndex
private int superclassNameIndex -
className
-
superclassName
-
major
private int major -
minor
private int minor -
constantPool
-
interfaces
private int[] interfaces -
interfaceNames
-
fields
-
methods
-
attributes
-
annotations
-
source
private byte source -
isAnonymous
private boolean isAnonymous -
isNested
private boolean isNested -
computedNestedTypeStatus
private boolean computedNestedTypeStatus -
repository
In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.
-
-
Constructor Details
-
JavaClass
public JavaClass(int classNameIndex, int superclassNameIndex, String fileName, int major, int minor, int accessFlags, ConstantPool constantPool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes) Constructor gets all contents as arguments.- Parameters:
classNameIndex
- Class namesuperclassNameIndex
- Superclass namefileName
- File namemajor
- Major compiler versionminor
- Minor compiler versionaccessFlags
- Access rights defined by bit flagsconstantPool
- Array of constantsinterfaces
- Implemented interfacesfields
- Class fieldsmethods
- Class methodsattributes
- Class attributes
-
JavaClass
public JavaClass(int classNameIndex, int superclassNameIndex, String fileName, int major, int minor, int accessFlags, ConstantPool constantPool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes, byte source) Constructor gets all contents as arguments.- Parameters:
classNameIndex
- Index into constant pool referencing a ConstantClass that represents this class.superclassNameIndex
- Index into constant pool referencing a ConstantClass that represents this class's superclass.fileName
- File namemajor
- Major compiler versionminor
- Minor compiler versionaccessFlags
- Access rights defined by bit flagsconstantPool
- Array of constantsinterfaces
- Implemented interfacesfields
- Class fieldsmethods
- Class methodsattributes
- Class attributessource
- Read from file or generated in memory?
-
-
Method Details
-
Debug
-
getComparator
- Returns:
- Comparison strategy object.
-
indent
-
setComparator
- Parameters:
comparator
- Comparison strategy object.
-
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. -
compareTo
Return the natural ordering of two JavaClasses. This ordering is based on the class name- Specified by:
compareTo
in interfaceComparable<JavaClass>
- Since:
- 6.0
-
computeNestedTypeStatus
private void computeNestedTypeStatus() -
copy
- Returns:
- deep copy of this class
-
dump
Dump Java class to output stream in binary format.- Parameters:
file
- Output stream- Throws:
IOException
- if an I/O error occurs.
-
dump
Dump class to a file.- Parameters:
file
- Output file- Throws:
IOException
- if an I/O error occurs.
-
dump
Dump Java class to output stream in binary format.- Parameters:
file
- Output stream- Throws:
IOException
- if an I/O error occurs.
-
dump
Dump class to a file named fileName.- Parameters:
fileName
- Output file name- Throws:
IOException
- if an I/O error occurs.
-
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. -
findField
Finds a visible field by name and type in this class and its super classes.- Parameters:
fieldName
- the field name to findfieldType
- the field type to find- Returns:
- field matching given name and type, null if field is not found or not accessible from this class.
- Throws:
ClassNotFoundException
- Since:
- 6.8.0
-
getAllInterfaces
Gets all interfaces implemented by this JavaClass (transitively).- Throws:
ClassNotFoundException
- if any of the class's superclasses or interfaces can't be found.
-
getAnnotationEntries
- Returns:
- Annotations on the class
- Since:
- 6.0
-
getAttributes
- Returns:
- Attributes of the class.
-
getBytes
public byte[] getBytes()- Returns:
- class in binary format
-
getClassName
- Returns:
- Class name.
-
getClassNameIndex
public int getClassNameIndex()- Returns:
- Class name index.
-
getConstantPool
- Returns:
- Constant pool.
-
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
- Returns:
- File name of class, aka SourceFile attribute value
-
getInterfaceIndices
public int[] getInterfaceIndices()- Returns:
- Indices in constant pool of implemented interfaces.
-
getInterfaceNames
- Returns:
- Names of implemented interfaces.
-
getInterfaces
Gets interfaces directly implemented by this JavaClass.- Throws:
ClassNotFoundException
- if any of the class's interfaces can't be found.
-
getMajor
public int getMajor()- Returns:
- Major number of class file version.
-
getMethod
- Returns:
- A
Method
corresponding to java.lang.reflect.Method if any
-
getMethods
- Returns:
- Methods of the class.
-
getMinor
public int getMinor()- Returns:
- Minor number of class file version.
-
getPackageName
- Returns:
- Package name.
-
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
- Returns:
- file name where this class was read from
-
getSourceFilePath
Gets the source file path including the package path.- Returns:
- path to original source file of parsed class, relative to original source directory.
- Since:
- 6.7.0
-
getSuperClass
- Returns:
- the superclass for this JavaClass object, or null if this is
Object
- Throws:
ClassNotFoundException
- if the superclass can't be found
-
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
-
getSuperclassName
returns the super class name of this class. In the case that this class isObject
, it will return itself (Object
). This is probably incorrect but isn't fixed at this time to not break existing clients.- Returns:
- Superclass name.
-
getSuperclassNameIndex
public int getSuperclassNameIndex()- Returns:
- Class name index.
-
hashCode
public int hashCode()Return value as defined by given BCELComparator strategy. By default return the hash code of the class name. -
implementationOf
- 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
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
-
isAnonymous
public final boolean isAnonymous()- Since:
- 6.0
-
isClass
public final boolean isClass() -
isNested
public final boolean isNested()- Since:
- 6.0
-
isSuper
public final boolean isSuper() -
setAttributes
- Parameters:
attributes
- .
-
setClassName
- Parameters:
className
- .
-
setClassNameIndex
public void setClassNameIndex(int classNameIndex) - Parameters:
classNameIndex
- .
-
setConstantPool
- Parameters:
constantPool
- .
-
setFields
- Parameters:
fields
- .
-
setFileName
Sets File name of class, aka SourceFile attribute value -
setInterfaceNames
- Parameters:
interfaceNames
- .
-
setInterfaces
public void setInterfaces(int[] interfaces) - Parameters:
interfaces
- .
-
setMajor
public void setMajor(int major) - Parameters:
major
- .
-
setMethods
- Parameters:
methods
- .
-
setMinor
public void setMinor(int minor) - Parameters:
minor
- .
-
setRepository
Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done. -
setSourceFileName
Sets absolute path to file this class was read from. -
setSuperclassName
- Parameters:
superclassName
- .
-
setSuperclassNameIndex
public void setSuperclassNameIndex(int superclassNameIndex) - Parameters:
superclassNameIndex
- .
-
toString
-