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, that is, 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[]annotationsprivate Attribute[]attributesprivate static BCELComparator<JavaClass>bcelComparatorprivate static java.lang.StringCLASS_NAME_OBJECTprivate java.lang.StringclassNameprivate intclassNameIndexprivate booleancomputedNestedTypeStatusprivate booleancomputedRecordprivate ConstantPoolconstantPoolprivate static booleandebugstatic JavaClass[]EMPTY_ARRAYEmpty array.static java.lang.StringEXTENSIONThe standard class file extension.private Field[]fieldsstatic byteFILESource was read from file.private java.lang.StringfileNamestatic byteHEAPSource was read from heap.private java.lang.String[]interfaceNamesprivate int[]interfacesprivate booleanisAnonymousprivate booleanisNestedprivate booleanisRecordprivate intmajorprivate Method[]methodsprivate intminorprivate java.lang.StringpackageNameprivate RepositoryrepositoryIn cases where we go ahead and create something, use the default SyntheticRepository, because we don't know any better.private bytesourceprivate java.lang.StringsourceFileNameprivate java.lang.StringsuperclassNameprivate intsuperclassNameIndexstatic byteZIPSource was read from 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 voidaccept(Visitor v)Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.intcompareTo(JavaClass obj)Return the natural ordering of two JavaClasses.private voidcomputeIsRecord()private voidcomputeNestedTypeStatus()JavaClasscopy()Creates a deep copy of this class.(package private) static voidDebug(java.lang.String str)voiddump(java.io.DataOutputStream file)Dumps Java class to output stream in binary format.voiddump(java.io.File file)Dumps class to a file.voiddump(java.io.OutputStream file)Dumps Java class to output stream in binary format.voiddump(java.lang.String fileName)Dumps class to a file named fileName.booleanequals(java.lang.Object obj)Return value as defined by given BCELComparator strategy.FieldfindField(java.lang.String fieldName, Type fieldType)Finds a visible field by name and type in this class and its super classes.private FieldfindFieldVisit(java.lang.String fieldName, Type fieldType, java.util.Set<JavaClass> visiting)JavaClass[]getAllInterfaces()Gets all interfaces implemented by this JavaClass (transitively).AnnotationEntry[]getAnnotationEntries()Gets annotations on the class.<T extends Attribute>
TgetAttribute(byte tag)Gets attribute for given tag.Attribute[]getAttributes()Gets attributes of the class.byte[]getBytes()Gets class in binary format.java.lang.StringgetClassName()Gets the class name.intgetClassNameIndex()Gets the class name index.static BCELComparator<JavaClass>getComparator()Gets the comparison strategy object.ConstantPoolgetConstantPool()Gets the constant pool.Field[]getFields()Gets the fields.java.lang.StringgetFileName()Gets the file name of class.int[]getInterfaceIndices()Gets indices in constant pool of implemented interfaces.java.lang.String[]getInterfaceNames()Gets names of implemented interfaces.JavaClass[]getInterfaces()Gets interfaces directly implemented by this JavaClass.intgetMajor()Gets the major number of class file version.MethodgetMethod(java.lang.reflect.Method m)Gets a Method corresponding to java.lang.reflect.Method if any.Method[]getMethods()Gets the methods of the class.intgetMinor()Gets the minor number of class file version.java.lang.StringgetPackageName()Gets the package name.RepositorygetRepository()Gets the ClassRepository which holds its definition.bytegetSource()Gets the source.java.lang.StringgetSourceFileName()Gets the file name where this class was read from.java.lang.StringgetSourceFilePath()Gets the source file path including the package path.JavaClassgetSuperClass()Gets the superclass for this JavaClass object, or null if this isObject.JavaClass[]getSuperClasses()Gets list of super classes of this class in ascending order.java.lang.StringgetSuperclassName()returns the super class name of this class.intgetSuperclassNameIndex()Gets the class name index.inthashCode()Return value as defined by given BCELComparator strategy.booleanimplementationOf(JavaClass inter)Checks if this class is an implementation of interface inter.private static java.lang.Stringindent(java.lang.Object obj)booleaninstanceOf(JavaClass superclass)Equivalent to runtime "instanceof" operator.booleanisAnonymous()Checks if this class is anonymous.booleanisClass()Checks if this is a class.booleanisNested()Checks if this class is nested.booleanisRecord()Tests whether this class was declared as a recordbooleanisSuper()Checks if this is a super class.voidsetAttributes(Attribute[] attributes)Sets the attributes.voidsetClassName(java.lang.String className)Sets the class name.voidsetClassNameIndex(int classNameIndex)Sets the class name index.static voidsetComparator(BCELComparator<JavaClass> comparator)Sets the comparison strategy object.voidsetConstantPool(ConstantPool constantPool)Sets the constant pool.voidsetFields(Field[] fields)Sets the fields.voidsetFileName(java.lang.String fileName)Sets File name of class, aka SourceFile attribute value.voidsetInterfaceNames(java.lang.String[] interfaceNames)Sets the interface names.voidsetInterfaces(int[] interfaces)Sets the interfaces.voidsetMajor(int major)Sets the major version.voidsetMethods(Method[] methods)Sets the methods.voidsetMinor(int minor)Sets the minor version.voidsetRepository(Repository repository)Sets the ClassRepository which loaded the JavaClass.voidsetSourceFileName(java.lang.String sourceFileName)Sets absolute path to file this class was read from.voidsetSuperclassName(java.lang.String superclassName)Sets the superclass name.voidsetSuperclassNameIndex(int superclassNameIndex)Sets the superclass name index.java.lang.StringtoString()-
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
-
CLASS_NAME_OBJECT
private static final java.lang.String CLASS_NAME_OBJECT
- See Also:
- Constant Field Values
-
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
Source was read from heap.- See Also:
- Constant Field Values
-
FILE
public static final byte FILE
Source was read from file.- See Also:
- Constant Field Values
-
ZIP
public static final byte ZIP
Source was read from ZIP.- See Also:
- Constant Field Values
-
debug
private static final boolean debug
-
bcelComparator
private static BCELComparator<JavaClass> 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
-
isRecord
private boolean isRecord
-
computedNestedTypeStatus
private boolean computedNestedTypeStatus
-
computedRecord
private boolean computedRecord
-
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 name.superclassNameIndex- Superclass name.fileName- File name.major- Major compiler version.minor- Minor compiler version.accessFlags- Access rights defined by bit flags.constantPool- Array of constants.interfaces- Implemented interfaces.fields- Class fields.methods- Class methods.attributes- 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 name.major- Major compiler version.minor- Minor compiler version.accessFlags- Access rights defined by bit flags.constantPool- Array of constants.interfaces- Implemented interfaces.fields- Class fields.methods- Class methods.attributes- Class attributes.source- Read from file or generated in memory.
-
-
Method Detail
-
Debug
static void Debug(java.lang.String str)
-
getComparator
public static BCELComparator<JavaClass> getComparator()
Gets the comparison strategy object.- Returns:
- Comparison strategy object.
-
indent
private static java.lang.String indent(java.lang.Object obj)
-
setComparator
public static void setComparator(BCELComparator<JavaClass> comparator)
Sets the comparison strategy object.- 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:
compareToin interfacejava.lang.Comparable<JavaClass>- Since:
- 6.0
-
computeIsRecord
private void computeIsRecord()
-
computeNestedTypeStatus
private void computeNestedTypeStatus()
-
copy
public JavaClass copy()
Creates a deep copy of this class.- Returns:
- deep copy of this class.
-
dump
public void dump(java.io.DataOutputStream file) throws java.io.IOExceptionDumps 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.IOExceptionDumps 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.IOExceptionDumps 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.IOExceptionDumps 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:
equalsin classjava.lang.Object- See Also:
Object.equals(Object)
-
findField
public Field findField(java.lang.String fieldName, Type fieldType) throws java.lang.ClassNotFoundException
Finds a visible field by name and type in this class and its super classes.- Parameters:
fieldName- the field name to find.fieldType- 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:
java.lang.ClassNotFoundException- if the class cannot be found.- Since:
- 6.8.0
-
findFieldVisit
private Field findFieldVisit(java.lang.String fieldName, Type fieldType, java.util.Set<JavaClass> visiting) throws java.lang.ClassNotFoundException
- Throws:
java.lang.ClassNotFoundException
-
getAllInterfaces
public JavaClass[] getAllInterfaces() throws java.lang.ClassNotFoundException
Gets all interfaces implemented by this JavaClass (transitively).- Returns:
- all interfaces.
- Throws:
java.lang.ClassNotFoundException- if any of the class's superclasses or interfaces can't be found.
-
getAnnotationEntries
public AnnotationEntry[] getAnnotationEntries()
Gets annotations on the class.- Returns:
- Annotations on the class.
- Since:
- 6.0
-
getAttribute
public final <T extends Attribute> T getAttribute(byte tag)
Gets attribute for given tag.- Type Parameters:
T- the attribute type.- Parameters:
tag- the attribute tag.- Returns:
- Attribute for given tag, null if not found.
Refer to
Const.ATTR_UNKNOWNconstants named ATTR_* for possible values. - Since:
- 6.10.0
-
getAttributes
public Attribute[] getAttributes()
Gets attributes of the class.- Returns:
- Attributes of the class.
-
getBytes
public byte[] getBytes()
Gets class in binary format.- Returns:
- class in binary format.
-
getClassName
public java.lang.String getClassName()
Gets the class name.- Returns:
- Class name.
-
getClassNameIndex
public int getClassNameIndex()
Gets the class name index.- Returns:
- Class name index.
-
getConstantPool
public ConstantPool getConstantPool()
Gets the constant pool.- Returns:
- Constant pool.
-
getFields
public Field[] getFields()
Gets the fields.- Returns:
- Fields, that is, 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()
Gets the file name of class.- Returns:
- File name of class, aka SourceFile attribute value.
-
getInterfaceIndices
public int[] getInterfaceIndices()
Gets indices in constant pool of implemented interfaces.- Returns:
- Indices in constant pool of implemented interfaces.
-
getInterfaceNames
public java.lang.String[] getInterfaceNames()
Gets names of implemented interfaces.- Returns:
- Names of implemented interfaces.
-
getInterfaces
public JavaClass[] getInterfaces() throws java.lang.ClassNotFoundException
Gets interfaces directly implemented by this JavaClass.- Returns:
- the interfaces.
- Throws:
java.lang.ClassNotFoundException- if any of the class's interfaces can't be found.
-
getMajor
public int getMajor()
Gets the major number of class file version.- Returns:
- Major number of class file version.
-
getMethod
public Method getMethod(java.lang.reflect.Method m)
Gets a Method corresponding to java.lang.reflect.Method if any.- Parameters:
m- the method to find.- Returns:
- A
Methodcorresponding to java.lang.reflect.Method if any.
-
getMethods
public Method[] getMethods()
Gets the methods of the class.- Returns:
- Methods of the class.
-
getMinor
public int getMinor()
Gets the minor number of class file version.- Returns:
- Minor number of class file version.
-
getPackageName
public java.lang.String getPackageName()
Gets the package name.- Returns:
- Package name.
-
getRepository
public Repository getRepository()
Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance().- Returns:
- the repository.
-
getSource
public final byte getSource()
Gets the source.- Returns:
- either HEAP (generated), FILE, or ZIP.
-
getSourceFileName
public java.lang.String getSourceFileName()
Gets the file name where this class was read from.- 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
Gets the superclass for this JavaClass object, or null if this isObject.- Returns:
- the superclass for this JavaClass object, or null if this is
Object. - Throws:
java.lang.ClassNotFoundException- if the superclass can't be found.
-
getSuperClasses
public JavaClass[] getSuperClasses() throws java.lang.ClassNotFoundException
Gets list of super classes of this class in ascending order.- Returns:
- list of super classes of this class in ascending order, that is,
Objectis 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 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()
Gets the class name index.- 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.- Overrides:
hashCodein classjava.lang.Object- See Also:
Object.hashCode()
-
implementationOf
public boolean implementationOf(JavaClass inter) throws java.lang.ClassNotFoundException
Checks if this class is an implementation of interface inter.- Parameters:
inter- the interface to check.- 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.- Parameters:
superclass- the superclass to check.- 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()
Checks if this class is anonymous.- Returns:
- true if anonymous.
- Since:
- 6.0
-
isClass
public final boolean isClass()
Checks if this is a class.- Returns:
- true if this is a class.
-
isNested
public final boolean isNested()
Checks if this class is nested.- Returns:
- true if nested.
- Since:
- 6.0
-
isRecord
public boolean isRecord()
Tests whether this class was declared as a record- Returns:
- true if a record attribute is present, false otherwise.
- Since:
- 6.9.0
-
isSuper
public final boolean isSuper()
Checks if this is a super class.- Returns:
- true if this is a super class.
-
setAttributes
public void setAttributes(Attribute[] attributes)
Sets the attributes.- Parameters:
attributes- the attributes.
-
setClassName
public void setClassName(java.lang.String className)
Sets the class name.- Parameters:
className- the class name.
-
setClassNameIndex
public void setClassNameIndex(int classNameIndex)
Sets the class name index.- Parameters:
classNameIndex- the class name index.
-
setConstantPool
public void setConstantPool(ConstantPool constantPool)
Sets the constant pool.- Parameters:
constantPool- the constant pool.
-
setFields
public void setFields(Field[] fields)
Sets the fields.- Parameters:
fields- the fields.
-
setFileName
public void setFileName(java.lang.String fileName)
Sets File name of class, aka SourceFile attribute value.- Parameters:
fileName- the file name.
-
setInterfaceNames
public void setInterfaceNames(java.lang.String[] interfaceNames)
Sets the interface names.- Parameters:
interfaceNames- the interface names.
-
setInterfaces
public void setInterfaces(int[] interfaces)
Sets the interfaces.- Parameters:
interfaces- the interfaces.
-
setMajor
public void setMajor(int major)
Sets the major version.- Parameters:
major- the major version.
-
setMethods
public void setMethods(Method[] methods)
Sets the methods.- Parameters:
methods- the methods.
-
setMinor
public void setMinor(int minor)
Sets the minor version.- Parameters:
minor- the minor version.
-
setRepository
public void setRepository(Repository repository)
Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.- Parameters:
repository- the repository.
-
setSourceFileName
public void setSourceFileName(java.lang.String sourceFileName)
Sets absolute path to file this class was read from.- Parameters:
sourceFileName- the source file name.
-
setSuperclassName
public void setSuperclassName(java.lang.String superclassName)
Sets the superclass name.- Parameters:
superclassName- the superclass name.
-
setSuperclassNameIndex
public void setSuperclassNameIndex(int superclassNameIndex)
Sets the superclass name index.- Parameters:
superclassNameIndex- the superclass name index.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object- Returns:
- String representing class contents.
-
-