Package org.apache.bcel.classfile
Class JavaClass
- java.lang.Object
-
- org.apache.bcel.classfile.AccessFlags
-
- org.apache.bcel.classfile.JavaClass
-
public class JavaClass extends AccessFlags implements java.lang.Cloneable, Node, java.lang.Comparable<JavaClass>
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:
ClassGen
-
-
Field Summary
Fields Modifier and Type Field Description private AnnotationEntry[]
annotations
private Attribute[]
attributes
private static BCELComparator
bcelComparator
private java.lang.String
className
private int
classNameIndex
private boolean
computedNestedTypeStatus
private ConstantPool
constantPool
private static boolean
debug
static JavaClass[]
EMPTY_ARRAY
Empty array.static java.lang.String
EXTENSION
The standard class file extension.private Field[]
fields
static byte
FILE
private java.lang.String
fileName
static byte
HEAP
private java.lang.String[]
interfaceNames
private int[]
interfaces
private boolean
isAnonymous
private boolean
isNested
private int
major
private Method[]
methods
private int
minor
private java.lang.String
packageName
private Repository
repository
In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.private byte
source
private java.lang.String
sourceFileName
private java.lang.String
superclassName
private int
superclassNameIndex
static byte
ZIP
-
Fields inherited from class org.apache.bcel.classfile.AccessFlags
access_flags
-
-
Constructor Summary
Constructors Constructor Description JavaClass(int classNameIndex, int superclassNameIndex, java.lang.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, java.lang.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept(Visitor v)
Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.int
compareTo(JavaClass obj)
Return the natural ordering of two JavaClasses.private void
computeNestedTypeStatus()
JavaClass
copy()
(package private) static void
Debug(java.lang.String str)
void
dump(java.io.DataOutputStream file)
Dump Java class to output stream in binary format.void
dump(java.io.File file)
Dump class to a file.void
dump(java.io.OutputStream file)
Dump Java class to output stream in binary format.void
dump(java.lang.String fileName)
Dump class to a file named fileName.boolean
equals(java.lang.Object obj)
Return value as defined by given BCELComparator strategy.JavaClass[]
getAllInterfaces()
Get all interfaces implemented by this JavaClass (transitively).AnnotationEntry[]
getAnnotationEntries()
Attribute[]
getAttributes()
byte[]
getBytes()
java.lang.String
getClassName()
int
getClassNameIndex()
static BCELComparator
getComparator()
ConstantPool
getConstantPool()
Field[]
getFields()
java.lang.String
getFileName()
int[]
getInterfaceIndices()
java.lang.String[]
getInterfaceNames()
JavaClass[]
getInterfaces()
Get interfaces directly implemented by this JavaClass.int
getMajor()
Method
getMethod(java.lang.reflect.Method m)
Method[]
getMethods()
int
getMinor()
java.lang.String
getPackageName()
Repository
getRepository()
Gets the ClassRepository which holds its definition.byte
getSource()
java.lang.String
getSourceFileName()
java.lang.String
getSourceFilePath()
Gets the source file path including the package path.JavaClass
getSuperClass()
JavaClass[]
getSuperClasses()
java.lang.String
getSuperclassName()
returns the super class name of this class.int
getSuperclassNameIndex()
int
hashCode()
Return value as defined by given BCELComparator strategy.boolean
implementationOf(JavaClass inter)
private static java.lang.String
indent(java.lang.Object obj)
boolean
instanceOf(JavaClass superclass)
Equivalent to runtime "instanceof" operator.boolean
isAnonymous()
boolean
isClass()
boolean
isNested()
boolean
isSuper()
void
setAttributes(Attribute[] attributes)
void
setClassName(java.lang.String className)
void
setClassNameIndex(int classNameIndex)
static void
setComparator(BCELComparator comparator)
void
setConstantPool(ConstantPool constantPool)
void
setFields(Field[] fields)
void
setFileName(java.lang.String fileName)
Set File name of class, aka SourceFile attribute valuevoid
setInterfaceNames(java.lang.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(java.lang.String sourceFileName)
Set absolute path to file this class was read from.void
setSuperclassName(java.lang.String superclassName)
void
setSuperclassNameIndex(int superclassNameIndex)
java.lang.String
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 Detail
-
EXTENSION
public static final java.lang.String EXTENSION
The standard class file extension.- Since:
- 6.7.0
- See Also:
- Constant Field Values
-
EMPTY_ARRAY
public static final JavaClass[] EMPTY_ARRAY
Empty array.- Since:
- 6.6.0
-
HEAP
public static final byte HEAP
- See Also:
- Constant Field Values
-
FILE
public static final byte FILE
- See Also:
- Constant Field Values
-
ZIP
public static final byte ZIP
- See Also:
- Constant Field Values
-
debug
private static final boolean debug
-
bcelComparator
private static BCELComparator bcelComparator
-
fileName
private java.lang.String fileName
-
packageName
private final java.lang.String packageName
-
sourceFileName
private java.lang.String sourceFileName
-
classNameIndex
private int classNameIndex
-
superclassNameIndex
private int superclassNameIndex
-
className
private java.lang.String className
-
superclassName
private java.lang.String superclassName
-
major
private int major
-
minor
private int minor
-
constantPool
private ConstantPool constantPool
-
interfaces
private int[] interfaces
-
interfaceNames
private java.lang.String[] interfaceNames
-
fields
private Field[] fields
-
methods
private Method[] methods
-
attributes
private Attribute[] attributes
-
annotations
private AnnotationEntry[] annotations
-
source
private byte source
-
isAnonymous
private boolean isAnonymous
-
isNested
private boolean isNested
-
computedNestedTypeStatus
private boolean computedNestedTypeStatus
-
repository
private transient Repository repository
In cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.
-
-
Constructor Detail
-
JavaClass
public JavaClass(int classNameIndex, int superclassNameIndex, java.lang.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, java.lang.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 Detail
-
Debug
static void Debug(java.lang.String str)
-
getComparator
public static BCELComparator getComparator()
- Returns:
- Comparison strategy object
-
indent
private static java.lang.String indent(java.lang.Object obj)
-
setComparator
public static void setComparator(BCELComparator comparator)
- Parameters:
comparator
- Comparison strategy object
-
accept
public void accept(Visitor v)
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
public int compareTo(JavaClass obj)
Return the natural ordering of two JavaClasses. This ordering is based on the class name- Specified by:
compareTo
in interfacejava.lang.Comparable<JavaClass>
- Since:
- 6.0
-
computeNestedTypeStatus
private void computeNestedTypeStatus()
-
copy
public JavaClass copy()
- Returns:
- deep copy of this class
-
dump
public void dump(java.io.DataOutputStream file) throws java.io.IOException
Dump Java class to output stream in binary format.- Parameters:
file
- Output stream- Throws:
java.io.IOException
- if an I/O error occurs.
-
dump
public void dump(java.io.File file) throws java.io.IOException
Dump class to a file.- Parameters:
file
- Output file- Throws:
java.io.IOException
- if an I/O error occurs.
-
dump
public void dump(java.io.OutputStream file) throws java.io.IOException
Dump Java class to output stream in binary format.- Parameters:
file
- Output stream- Throws:
java.io.IOException
- if an I/O error occurs.
-
dump
public void dump(java.lang.String fileName) throws java.io.IOException
Dump class to a file named fileName.- Parameters:
fileName
- Output file name- Throws:
java.io.IOException
- if an I/O error occurs.
-
equals
public boolean equals(java.lang.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.- Overrides:
equals
in classjava.lang.Object
- See Also:
Object.equals(Object)
-
getAllInterfaces
public JavaClass[] getAllInterfaces() throws java.lang.ClassNotFoundException
Get all interfaces implemented by this JavaClass (transitively).- Throws:
java.lang.ClassNotFoundException
- if any of the class's superclasses or interfaces can't be found.
-
getAnnotationEntries
public AnnotationEntry[] getAnnotationEntries()
- Returns:
- Annotations on the class
- Since:
- 6.0
-
getAttributes
public Attribute[] getAttributes()
- Returns:
- Attributes of the class.
-
getBytes
public byte[] getBytes()
- Returns:
- class in binary format
-
getClassName
public java.lang.String getClassName()
- Returns:
- Class name.
-
getClassNameIndex
public int getClassNameIndex()
- Returns:
- Class name index.
-
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 java.lang.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 java.lang.String[] getInterfaceNames()
- Returns:
- Names of implemented interfaces.
-
getInterfaces
public JavaClass[] getInterfaces() throws java.lang.ClassNotFoundException
Get interfaces directly implemented by this JavaClass.- Throws:
java.lang.ClassNotFoundException
- if any of the class's interfaces can't be found.
-
getMajor
public int getMajor()
- Returns:
- Major number of class file version.
-
getMethod
public Method getMethod(java.lang.reflect.Method m)
- Returns:
- A
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 java.lang.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 java.lang.String getSourceFileName()
- Returns:
- file name where this class was read from
-
getSourceFilePath
public java.lang.String 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
public JavaClass getSuperClass() throws java.lang.ClassNotFoundException
- Returns:
- the superclass for this JavaClass object, or null if this is java.lang.Object
- Throws:
java.lang.ClassNotFoundException
- if the superclass can't be found
-
getSuperClasses
public JavaClass[] getSuperClasses() throws java.lang.ClassNotFoundException
- Returns:
- list of super classes of this class in ascending order, i.e., java.lang.Object is always the last element
- Throws:
java.lang.ClassNotFoundException
- if any of the superclasses can't be found
-
getSuperclassName
public java.lang.String getSuperclassName()
returns the super class name of this class. In the case that this class is java.lang.Object, it will return itself (java.lang.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 hashcode of the class name.- Overrides:
hashCode
in classjava.lang.Object
- See Also:
Object.hashCode()
-
implementationOf
public boolean implementationOf(JavaClass inter) throws java.lang.ClassNotFoundException
- Returns:
- true, if this class is an implementation of interface inter
- Throws:
java.lang.ClassNotFoundException
- if superclasses or superinterfaces of this class can't be found
-
instanceOf
public final boolean instanceOf(JavaClass superclass) throws java.lang.ClassNotFoundException
Equivalent to runtime "instanceof" operator.- Returns:
- true if this JavaClass is derived from the super class
- Throws:
java.lang.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
public void setAttributes(Attribute[] attributes)
- Parameters:
attributes
- .
-
setClassName
public void setClassName(java.lang.String className)
- Parameters:
className
- .
-
setClassNameIndex
public void setClassNameIndex(int classNameIndex)
- Parameters:
classNameIndex
- .
-
setConstantPool
public void setConstantPool(ConstantPool constantPool)
- Parameters:
constantPool
- .
-
setFields
public void setFields(Field[] fields)
- Parameters:
fields
- .
-
setFileName
public void setFileName(java.lang.String fileName)
Set File name of class, aka SourceFile attribute value
-
setInterfaceNames
public void setInterfaceNames(java.lang.String[] interfaceNames)
- Parameters:
interfaceNames
- .
-
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(java.lang.String sourceFileName)
Set absolute path to file this class was read from.
-
setSuperclassName
public void setSuperclassName(java.lang.String superclassName)
- Parameters:
superclassName
- .
-
setSuperclassNameIndex
public void setSuperclassNameIndex(int superclassNameIndex)
- Parameters:
superclassNameIndex
- .
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Returns:
- String representing class contents.
-
-