Class ClassParser

java.lang.Object
org.apache.bcel.classfile.ClassParser

public final class ClassParser extends Object
Wrapper class that parses a given Java .class file. The method parse returns a JavaClass object on success. When an I/O error or an inconsistency occurs an appropriate exception is propagated back to the caller. The structure and the names comply, except for a few conveniences, exactly with the JVM specification 1.0. See this paper for further details about the structure of a bytecode file.
  • Field Details

    • BUFSIZE

      private static final int BUFSIZE
      See Also:
    • dataInputStream

      private DataInputStream dataInputStream
    • fileOwned

      private final boolean fileOwned
    • fileName

      private final String fileName
    • zipFile

      private String zipFile
    • classNameIndex

      private int classNameIndex
    • superclassNameIndex

      private int superclassNameIndex
    • major

      private int major
    • minor

      private int minor
    • compactCodeHeader

      private boolean compactCodeHeader
    • compactCodeHeaderEnabled

      private boolean compactCodeHeaderEnabled
    • accessFlags

      private int accessFlags
    • interfaces

      private int[] interfaces
    • constantPool

      private ConstantPool constantPool
    • fields

      private Field[] fields
    • methods

      private Method[] methods
    • attributes

      private Attribute[] attributes
    • isZip

      private final boolean isZip
  • Constructor Details

    • ClassParser

      public ClassParser(InputStream inputStream, String fileName)
      Parses class from the given stream.
      Parameters:
      inputStream - Input stream.
      fileName - File name.
    • ClassParser

      public ClassParser(String fileName)
      Parses class from given .class file.
      Parameters:
      fileName - file name.
    • ClassParser

      public ClassParser(String zipFile, String fileName)
      Parses class from given .class file in a ZIP-archive
      Parameters:
      zipFile - ZIP file name.
      fileName - file name.
  • Method Details

    • readU2U2Table

      static int[] readU2U2Table(DataInput dataInput) throws IOException
      Throws:
      IOException
    • isCompactCodeHeaderEnabled

      public boolean isCompactCodeHeaderEnabled()
      Gets whether compact Code header parsing is enabled for class file versions 45.0 through 45.2.
      Returns:
      Whether compact Code header parsing is enabled.
      Since:
      6.14.0
      See Also:
    • parse

      public JavaClass parse() throws IOException, ClassFormatException
      Parses the given Java class file and return an object that represents the contained data, that is, constants, methods, fields and commands. A ClassFormatException is raised, if the file is not a valid .class file. (This does not include verification of the byte code as it is performed by the Java interpreter).

      Code attributes use the standard eight-byte header by default, including in class files with version 45.0 through 45.2. Use setCompactCodeHeaderEnabled(boolean) to read the compact four-byte header used by historical files with those versions.

      Returns:
      Class object representing the parsed class file.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readAttributes

      private void readAttributes() throws IOException, ClassFormatException
      Reads information about the attributes of the class.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readClassInfo

      private void readClassInfo() throws IOException, ClassFormatException
      Reads information about the class and its superclass.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readConstantPool

      private void readConstantPool() throws IOException, ClassFormatException
      Reads constant pool entries.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readFields

      private void readFields() throws IOException, ClassFormatException
      Reads information about the fields of the class, that is, its variables.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readID

      private void readID() throws IOException, ClassFormatException
      Checks whether the header of the file is ok. Of course, this has to be the first action on successive file reads.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readInterfaces

      private void readInterfaces() throws IOException, ClassFormatException
      Reads information about the interfaces implemented by this class.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readMethods

      private void readMethods() throws IOException
      Reads information about the methods of the class.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • readVersion

      private void readVersion() throws IOException, ClassFormatException
      Reads major and minor version of compiler which created the file.
      Throws:
      IOException - Thrown if an I/O error occurs.
      ClassFormatException - Thrown if a class is malformed or cannot be interpreted as a class file.
    • setCompactCodeHeaderEnabled

      public void setCompactCodeHeaderEnabled(boolean enabled)
      Sets whether to read compact Code headers in class file versions 45.0 through 45.2.

      "Compact" refers to the widths of max_stack, max_locals, and code_length: unsigned one-, one-, and two-byte fields, respectively, totaling 4 bytes. Standard headers use unsigned two-, two-, and four-byte fields, totaling 8 bytes. These sizes exclude the common six-byte attribute prefix. The layout of that prefix and the remaining Code fields is unchanged.

      This option is disabled by default because modern JVMs also accept versions 45.0 through 45.2 with standard headers. The version therefore cannot determine the layout by itself; enabling this option selects the historical compact layout. Other class file versions always use standard headers, regardless of this option. Set this option before calling parse().

      Parameters:
      enabled - Whether to enable compact Code header parsing.
      Since:
      6.14.0
      See Also: