Class JavaClass
java.lang.Object
org.apache.bcel.classfile.AccessFlags
org.apache.bcel.classfile.JavaClass
- All Implemented Interfaces:
Serializable, 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 programatically generating classes
should see the ClassGen class.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final bytestatic final bytestatic final byteFields inherited from class AccessFlags
access_flags -
Constructor Summary
ConstructorsConstructorDescriptionJavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes) Constructor gets all contents as arguments.JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, 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 implicitely defined by the contents of a Java class.intReturn the natural ordering of two JavaClasses.copy()voiddump(DataOutputStream file) Dump Java class to output stream in binary format.voidDump class to a file.voiddump(OutputStream file) Dump Java class to output stream in binary format.voidDump class to a file named file_name.booleanReturn value as defined by given BCELComparator strategy.Get all interfaces implemented by this JavaClass (transitively).byte[]getBytes()intstatic BCELComparatorField[]int[]String[]Get interfaces directly implemented by this JavaClass.intgetMajor()Method[]intgetMinor()Gets the ClassRepository which holds its definition.final bytereturns the super class name of this class.intinthashCode()Return value as defined by given BCELComparator strategy.booleanimplementationOf(JavaClass inter) final booleaninstanceOf(JavaClass super_class) Equivalent to runtime "instanceof" operator.final booleanfinal booleanisClass()final booleanisNested()final booleanisSuper()voidsetAttributes(Attribute[] attributes) voidsetClassName(String class_name) voidsetClassNameIndex(int class_name_index) static voidsetComparator(BCELComparator comparator) voidsetConstantPool(ConstantPool constant_pool) voidvoidsetFileName(String file_name) Set File name of class, aka SourceFile attribute valuevoidsetInterfaceNames(String[] interface_names) voidsetInterfaces(int[] interfaces) voidsetMajor(int major) voidsetMethods(Method[] methods) voidsetMinor(int minor) voidsetRepository(Repository repository) Sets the ClassRepository which loaded the JavaClass.voidsetSourceFileName(String source_file_name) Set absolute path to file this class was read from.voidsetSuperclassName(String superclass_name) voidsetSuperclassNameIndex(int 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, isVolatile, isVolatile, setAccessFlags, setModifiers
-
Field Details
-
HEAP
public static final byte HEAP- See Also:
-
FILE
public static final byte FILE- See Also:
-
ZIP
public static final byte ZIP- See Also:
-
-
Constructor Details
-
JavaClass
public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes, byte source) Constructor gets all contents as arguments.- Parameters:
class_name_index- Index into constant pool referencing a ConstantClass that represents this class.superclass_name_index- Index into constant pool referencing a ConstantClass that represents this class's superclass.file_name- File namemajor- Major compiler versionminor- Minor compiler versionaccess_flags- Access rights defined by bit flagsconstant_pool- Array of constantsinterfaces- Implemented interfacesfields- Class fieldsmethods- Class methodsattributes- Class attributessource- Read from file or generated in memory?
-
JavaClass
public JavaClass(int class_name_index, int superclass_name_index, String file_name, int major, int minor, int access_flags, ConstantPool constant_pool, int[] interfaces, Field[] fields, Method[] methods, Attribute[] attributes) Constructor gets all contents as arguments.- Parameters:
class_name_index- Class namesuperclass_name_index- Superclass namefile_name- File namemajor- Major compiler versionminor- Minor compiler versionaccess_flags- Access rights defined by bit flagsconstant_pool- Array of constantsinterfaces- Implemented interfacesfields- Class fieldsmethods- Class methodsattributes- Class attributes
-
-
Method Details
-
accept
-
dump
Dump class to a file.- Parameters:
file- Output file- Throws:
IOException
-
dump
Dump class to a file named file_name.- Parameters:
_file_name- Output file name- Throws:
IOException
-
getBytes
public byte[] getBytes()- Returns:
- class in binary format
-
dump
Dump Java class to output stream in binary format.- Parameters:
file- Output stream- Throws:
IOException
-
dump
Dump Java class to output stream in binary format.- Parameters:
file- Output stream- Throws:
IOException
-
getAttributes
- Returns:
- Attributes of the class.
-
getAnnotationEntries
-
getClassName
- Returns:
- Class name.
-
getPackageName
- Returns:
- Package 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
-
getInterfaceNames
- Returns:
- Names of implemented interfaces.
-
getInterfaceIndices
public int[] getInterfaceIndices()- Returns:
- Indices in constant pool of implemented interfaces.
-
getMajor
public int getMajor()- Returns:
- Major number of class file version.
-
getMethods
- Returns:
- Methods of the class.
-
getMethod
-
getMinor
public int getMinor()- Returns:
- Minor number of class file version.
-
getSourceFileName
- Returns:
- sbsolute path to file where this class was read from
-
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.
-
setAttributes
- Parameters:
attributes- .
-
setClassName
- Parameters:
class_name- .
-
setClassNameIndex
public void setClassNameIndex(int class_name_index) - Parameters:
class_name_index- .
-
setConstantPool
- Parameters:
constant_pool- .
-
setFields
- Parameters:
fields- .
-
setFileName
Set File name of class, aka SourceFile attribute value -
setInterfaceNames
- Parameters:
interface_names- .
-
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- .
-
setSourceFileName
Set absolute path to file this class was read from. -
setSuperclassName
- Parameters:
superclass_name- .
-
setSuperclassNameIndex
public void setSuperclassNameIndex(int superclass_name_index) - Parameters:
superclass_name_index- .
-
toString
-
copy
- Returns:
- deep copy of this class
-
isSuper
public final boolean isSuper() -
isClass
public final boolean isClass() -
isAnonymous
public final boolean isAnonymous() -
isNested
public final boolean isNested() -
getSource
public final byte getSource()- Returns:
- returns either HEAP (generated), FILE, or ZIP
-
getRepository
Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance(); -
setRepository
Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done. -
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
-
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
-
getSuperClass
- Returns:
- the superclass for this JavaClass object, or null if this is java.lang.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
-
getInterfaces
Get interfaces directly implemented by this JavaClass.- Throws:
ClassNotFoundException
-
getAllInterfaces
Get all interfaces implemented by this JavaClass (transitively).- Throws:
ClassNotFoundException
-
getComparator
- Returns:
- Comparison strategy object
-
setComparator
- Parameters:
comparator- Comparison strategy object
-
equals
-
compareTo
Return the natural ordering of two JavaClasses. This ordering is based on the class name- Specified by:
compareToin interfaceComparable<JavaClass>
-
hashCode
-