Class JavaClass

java.lang.Object
org.apache.bcel.classfile.AccessFlags
org.apache.bcel.classfile.JavaClass
All Implemented Interfaces:
Cloneable, Comparable<JavaClass>, Node

public class JavaClass extends AccessFlags implements Cloneable, Node, 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:
  • Field Details Link icon

  • Constructor Details Link icon

    • JavaClass Link icon

      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 Link icon

      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 Link icon

    • getComparator Link icon

      Returns:
      Comparison strategy object.
    • setComparator Link icon

      public static void setComparator(BCELComparator<JavaClass> comparator)
      Parameters:
      comparator - Comparison strategy object.
    • accept Link icon

      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.
      Specified by:
      accept in interface Node
      Parameters:
      v - Visitor object
    • compareTo Link icon

      public int compareTo(JavaClass obj)
      Return the natural ordering of two JavaClasses. This ordering is based on the class name
      Specified by:
      compareTo in interface Comparable<JavaClass>
      Since:
      6.0
    • copy Link icon

      public JavaClass copy()
      Returns:
      deep copy of this class
    • dump Link icon

      public void dump(DataOutputStream file) throws IOException
      Dump Java class to output stream in binary format.
      Parameters:
      file - Output stream
      Throws:
      IOException - if an I/O error occurs.
    • dump Link icon

      public void dump(File file) throws IOException
      Dump class to a file.
      Parameters:
      file - Output file
      Throws:
      IOException - if an I/O error occurs.
    • dump Link icon

      public void dump(OutputStream file) throws IOException
      Dump Java class to output stream in binary format.
      Parameters:
      file - Output stream
      Throws:
      IOException - if an I/O error occurs.
    • dump Link icon

      public void dump(String fileName) throws IOException
      Dump class to a file named fileName.
      Parameters:
      fileName - Output file name
      Throws:
      IOException - if an I/O error occurs.
    • equals Link icon

      public boolean equals(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 class Object
      See Also:
    • findField Link icon

      public Field findField(String fieldName, Type fieldType) throws 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:
      ClassNotFoundException
      Since:
      6.8.0
    • getAllInterfaces Link icon

      Gets all interfaces implemented by this JavaClass (transitively).
      Throws:
      ClassNotFoundException - if any of the class's superclasses or interfaces can't be found.
    • getAnnotationEntries Link icon

      Returns:
      Annotations on the class
      Since:
      6.0
    • getAttribute Link icon

      public final <T extends Attribute> T getAttribute(byte tag)
      Gets attribute for given tag.
      Returns:
      Attribute for given tag, null if not found. Refer to Const.ATTR_UNKNOWN constants named ATTR_* for possible values.
      Since:
      6.10.0
    • getAttributes Link icon

      Returns:
      Attributes of the class.
    • getBytes Link icon

      public byte[] getBytes()
      Returns:
      class in binary format
    • getClassName Link icon

      public String getClassName()
      Returns:
      Class name.
    • getClassNameIndex Link icon

      public int getClassNameIndex()
      Returns:
      Class name index.
    • getConstantPool Link icon

      Returns:
      Constant pool.
    • getFields Link icon

      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 Link icon

      public String getFileName()
      Returns:
      File name of class, aka SourceFile attribute value
    • getInterfaceIndices Link icon

      public int[] getInterfaceIndices()
      Returns:
      Indices in constant pool of implemented interfaces.
    • getInterfaceNames Link icon

      Returns:
      Names of implemented interfaces.
    • getInterfaces Link icon

      Gets interfaces directly implemented by this JavaClass.
      Throws:
      ClassNotFoundException - if any of the class's interfaces can't be found.
    • getMajor Link icon

      public int getMajor()
      Returns:
      Major number of class file version.
    • getMethod Link icon

      public Method getMethod(Method m)
      Returns:
      A Method corresponding to java.lang.reflect.Method if any
    • getMethods Link icon

      public Method[] getMethods()
      Returns:
      Methods of the class.
    • getMinor Link icon

      public int getMinor()
      Returns:
      Minor number of class file version.
    • getPackageName Link icon

      Returns:
      Package name.
    • getRepository Link icon

      Gets the ClassRepository which holds its definition. By default this is the same as SyntheticRepository.getInstance();
    • getSource Link icon

      public final byte getSource()
      Returns:
      returns either HEAP (generated), FILE, or ZIP
    • getSourceFileName Link icon

      Returns:
      file name where this class was read from
    • getSourceFilePath Link icon

      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 Link icon

      Returns:
      the superclass for this JavaClass object, or null if this is Object
      Throws:
      ClassNotFoundException - if the superclass can't be found
    • getSuperClasses Link icon

      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 Link icon

      returns the super class name of this class. In the case that this class is Object, 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 Link icon

      public int getSuperclassNameIndex()
      Returns:
      Class name index.
    • hashCode Link icon

      public int hashCode()
      Return value as defined by given BCELComparator strategy. By default return the hash code of the class name.
      Overrides:
      hashCode in class Object
      See Also:
    • implementationOf Link icon

      public boolean implementationOf(JavaClass inter) throws ClassNotFoundException
      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 Link icon

      public final boolean instanceOf(JavaClass superclass) throws ClassNotFoundException
      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 Link icon

      public final boolean isAnonymous()
      Since:
      6.0
    • isClass Link icon

      public final boolean isClass()
    • isNested Link icon

      public final boolean isNested()
      Since:
      6.0
    • isRecord Link icon

      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 Link icon

      public final boolean isSuper()
    • setAttributes Link icon

      public void setAttributes(Attribute[] attributes)
      Parameters:
      attributes - .
    • setClassName Link icon

      public void setClassName(String className)
      Parameters:
      className - .
    • setClassNameIndex Link icon

      public void setClassNameIndex(int classNameIndex)
      Parameters:
      classNameIndex - .
    • setConstantPool Link icon

      public void setConstantPool(ConstantPool constantPool)
      Parameters:
      constantPool - .
    • setFields Link icon

      public void setFields(Field[] fields)
      Parameters:
      fields - .
    • setFileName Link icon

      public void setFileName(String fileName)
      Sets File name of class, aka SourceFile attribute value
    • setInterfaceNames Link icon

      public void setInterfaceNames(String[] interfaceNames)
      Parameters:
      interfaceNames - .
    • setInterfaces Link icon

      public void setInterfaces(int[] interfaces)
      Parameters:
      interfaces - .
    • setMajor Link icon

      public void setMajor(int major)
      Parameters:
      major - .
    • setMethods Link icon

      public void setMethods(Method[] methods)
      Parameters:
      methods - .
    • setMinor Link icon

      public void setMinor(int minor)
      Parameters:
      minor - .
    • setRepository Link icon

      public void setRepository(Repository repository)
      Sets the ClassRepository which loaded the JavaClass. Should be called immediately after parsing is done.
    • setSourceFileName Link icon

      public void setSourceFileName(String sourceFileName)
      Sets absolute path to file this class was read from.
    • setSuperclassName Link icon

      public void setSuperclassName(String superclassName)
      Parameters:
      superclassName - .
    • setSuperclassNameIndex Link icon

      public void setSuperclassNameIndex(int superclassNameIndex)
      Parameters:
      superclassNameIndex - .
    • toString Link icon

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      String representing class contents.