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, 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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final JavaClass[]Empty array.static final StringThe standard class file extension.static final byteSource was read from file.static final byteSource was read from heap.static final byteSource was read from ZIP.Fields inherited from class AccessFlags
access_flagsModifier and TypeFieldDescriptionprotected intDeprecated.(since 6.0) will be made private; do not access directly, use getter/setter. -
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 TypeMethodDescriptionvoidCalled by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.intReturn the natural ordering of two JavaClasses.copy()Creates a deep copy of this class.voiddump(DataOutputStream file) Dumps Java class to output stream in binary format.voidDumps class to a file.voiddump(OutputStream file) Dumps Java class to output stream in binary format.voidDumps class to a file named fileName.booleanReturn value as defined by given BCELComparator strategy.Finds a visible field by name and type in this class and its superclasses.Gets all interfaces implemented by this JavaClass (transitively).Gets annotations on the class.final <T extends Attribute>
TgetAttribute(byte tag) Gets attribute for given tag.Gets attributes of the class.byte[]getBytes()Gets class in binary format.Gets the class name.intGets the class name index.static BCELComparator<JavaClass> Gets the comparison strategy object.Gets the constant pool.Field[]Gets the fields.Gets the file name of class.int[]Gets indices in constant pool of implemented interfaces.String[]Gets names of implemented interfaces.Gets interfaces directly implemented by this JavaClass.intgetMajor()Gets the major number of class file version.Gets a Method corresponding to java.lang.reflect.Method if any.Method[]Gets the methods of the class.intgetMinor()Gets the minor number of class file version.Gets the package name.Gets the ClassRepository which holds its definition.final byteGets the source.Gets the file name where this class was read from.Gets the source file path including the package path.Gets the superclass for this JavaClass object, or null if this isObject.Gets the list of superclasses of this class in ascending order.Gets the superclass name of this class.intGets 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.final booleaninstanceOf(JavaClass superclass) Equivalent to runtime "instanceof" operator.final booleanTests whether this class is anonymous.final booleanisClass()Tests whether this is a class.final booleanisNested()Tests whether this class is nested.booleanisRecord()Tests whether this class was declared as a recordfinal booleanisSuper()Tests whether the ACC_SUPER flag is set.voidsetAttributes(Attribute[] attributes) Sets the attributes.voidsetClassName(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.voidSets the fields.voidsetFileName(String fileName) Sets the source file name of the class, that is, the SourceFile attribute value.voidsetInterfaceNames(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(String sourceFileName) Sets absolute path to file this class was read from.voidsetSuperclassName(String superclassName) Sets the superclass name.voidsetSuperclassNameIndex(int superclassNameIndex) Sets the superclass name index.toString()Methods inherited from class 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, setModifiersModifier and TypeMethodDescriptionfinal intGets access flags.final intGets access flags.final booleanTests whether the abstract bit is on.final voidisAbstract(boolean flag) Sets the abstract bit.final booleanTests whether the annotation bit is on.final voidisAnnotation(boolean flag) Sets the annotation bit.final booleanisEnum()Tests whether the enum bit is on.final voidisEnum(boolean flag) Sets the enum bit.final booleanisFinal()Tests whether the final bit is on.final voidisFinal(boolean flag) Sets the final bit.final booleanTests whether the interface bit is on.final voidisInterface(boolean flag) Sets the interface bit.final booleanisNative()Tests whether the native bit is on.final voidisNative(boolean flag) Sets the native bit.final booleanTests whether the private bit is on.final voidisPrivate(boolean flag) Sets the private bit.final booleanTests whether the protected bit is on.final voidisProtected(boolean flag) Sets the protected bit.final booleanisPublic()Tests whether the public bit is on.final voidisPublic(boolean flag) Sets the public bit.final booleanisStatic()Tests whether the static bit is on.final voidisStatic(boolean flag) Sets the static bit.final booleanTests whether the strict bit is on.final voidisStrictfp(boolean flag) Sets the strict bit.final booleanTests whether the synchronized bit is on.final voidisSynchronized(boolean flag) Sets the synchronized bit.final booleanTests whether the synthetic bit is on.final voidisSynthetic(boolean flag) Sets the synthetic bit.final booleanTests whether the transient bit is on.final voidisTransient(boolean flag) Sets the transient bit.final booleanTests whether the varargs bit is on.final voidisVarArgs(boolean flag) Sets the varargs bit.final booleanTests whether the volatile bit is on.final voidisVolatile(boolean flag) Sets the volatile bit.final voidsetAccessFlags(int accessFlags) Sets access flags also known as modifiers.final voidsetModifiers(int accessFlags) Sets access flags aka "modifiers".
-
Field Details
-
EXTENSION
-
EMPTY_ARRAY
-
HEAP
-
FILE
-
ZIP
-
-
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 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, 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 Details
-
getComparator
Gets the comparison strategy object.- Returns:
- Comparison strategy object.
-
setComparator
Sets the comparison strategy object.- 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. That is, the hierarchy of methods, fields, attributes, and other elements forms a tree of objects. -
compareTo
Return the natural ordering of two JavaClasses. This ordering is based on the class name- Specified by:
compareToin interfaceComparable<JavaClass>- Since:
- 6.0
-
copy
-
dump
Dumps Java class to output stream in binary format.For versions 45.0 through 45.2, all method Code attributes use compact four-byte headers if any method's Code attribute returns
truefromCode.isCompactCodeHeader(). Otherwise, all method Code attributes use standard eight-byte headers. Versions 45.3 and later always use standard headers, including after a version change. The header size refers only tomax_stack,max_locals, andcode_length. These encoding adjustments do not modify the methods or their attributes.- Parameters:
file- Output stream.- Throws:
IOException- Thrown if an I/O error occurs.ClassFormatException- Thrown if a method's values do not fit the selected Code header layout.
-
dump
Dumps class to a file.- Parameters:
file- Output file.- Throws:
IOException- Thrown if an I/O error occurs.
-
dump
Dumps Java class to output stream in binary format.- Parameters:
file- Output stream.- Throws:
IOException- Thrown if an I/O error occurs.
-
dump
Dumps class to a file named fileName.- Parameters:
fileName- Output file name.- Throws:
IOException- Thrown if an I/O error occurs.
-
equals
-
findField
Finds a visible field by name and type in this class and its superclasses.- 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:
ClassNotFoundException- Thrown if the class cannot be found.- Since:
- 6.8.0
-
getAllInterfaces
Gets all interfaces implemented by this JavaClass (transitively).- Returns:
- all interfaces.
- Throws:
ClassNotFoundException- Thrown if any of the class's superclasses or interfaces can't be found.
-
getAnnotationEntries
Gets annotations on the class.- Returns:
- Annotations on the class.
- Since:
- 6.0
-
getAttribute
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
-
getBytes
-
getClassName
-
getClassNameIndex
-
getConstantPool
-
getFields
-
getFileName
Gets the file name of class.- Returns:
- File name of class, aka SourceFile attribute value.
-
getInterfaceIndices
Gets indices in constant pool of implemented interfaces.- Returns:
- Indices in constant pool of implemented interfaces.
-
getInterfaceNames
Gets names of implemented interfaces.- Returns:
- Names of implemented interfaces.
-
getInterfaces
Gets interfaces directly implemented by this JavaClass.- Returns:
- The interfaces.
- Throws:
ClassNotFoundException- Thrown if any of the class's interfaces can't be found.
-
getMajor
Gets the major number of class file version.- Returns:
- Major number of class file version.
-
getMethod
-
getMethods
-
getMinor
Gets the minor number of class file version.- Returns:
- Minor number of class file version.
-
getPackageName
-
getRepository
Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance().- Returns:
- The repository.
-
getSource
-
getSourceFileName
Gets the file name where this class was read from.- 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
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:
ClassNotFoundException- Thrown if the superclass can't be found.
-
getSuperClasses
Gets the list of superclasses of this class in ascending order.- Returns:
- list of superclasses of this class in ascending order, that is,
Objectis always the last element. - Throws:
ClassNotFoundException- Thrown if any of the superclasses can't be found.
-
getSuperclassName
-
getSuperclassNameIndex
-
hashCode
-
implementationOf
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:
ClassNotFoundException- Thrown if superclasses or superinterfaces of this class can't be found.
-
instanceOf
Equivalent to runtime "instanceof" operator.- Parameters:
superclass- The superclass to check.- Returns:
- true if this JavaClass is derived from the superclass.
- Throws:
ClassNotFoundException- Thrown if superclasses or superinterfaces of this object can't be found.
-
isAnonymous
Tests whether this class is anonymous.- Returns:
- true if anonymous.
- Since:
- 6.0
-
isClass
-
isNested
Tests whether this class is nested.- Returns:
- true if nested.
- Since:
- 6.0
-
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
Tests whether the ACC_SUPER flag is set.- Returns:
- true if the ACC_SUPER flag is set.
-
setAttributes
Sets the attributes.- Parameters:
attributes- The attributes.
-
setClassName
Sets the class name.- Parameters:
className- The class name.
-
setClassNameIndex
Sets the class name index.- Parameters:
classNameIndex- The class name index.
-
setConstantPool
Sets the constant pool.- Parameters:
constantPool- The constant pool.
-
setFields
-
setFileName
Sets the source file name of the class, that is, the SourceFile attribute value.- Parameters:
fileName- The file name.
-
setInterfaceNames
Sets the interface names.- Parameters:
interfaceNames- The interface names.
-
setInterfaces
Sets the interfaces.- Parameters:
interfaces- The interfaces.
-
setMajor
-
setMethods
-
setMinor
-
setRepository
Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.- Parameters:
repository- The repository.
-
setSourceFileName
Sets absolute path to file this class was read from.- Parameters:
sourceFileName- The source file name.
-
setSuperclassName
Sets the superclass name.- Parameters:
superclassName- The superclass name.
-
setSuperclassNameIndex
Sets the superclass name index.- Parameters:
superclassNameIndex- The superclass name index.
-
toString
-