Class ClassParser


  • public final class ClassParser
    extends java.lang.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.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassParser​(java.io.InputStream inputStream, java.lang.String fileName)
      Parses class from the given stream.
      ClassParser​(java.lang.String fileName)
      Parses class from given .class file.
      ClassParser​(java.lang.String zipFile, java.lang.String fileName)
      Parses class from given .class file in a ZIP-archive
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      JavaClass parse()
      Parses the given Java class file and return an object that represents the contained data, i.e., constants, methods, fields and commands.
      private void readAttributes()
      Reads information about the attributes of the class.
      private void readClassInfo()
      Reads information about the class and its super class.
      private void readConstantPool()
      Reads constant pool entries.
      private void readFields()
      Reads information about the fields of the class, i.e., its variables.
      private void readID()
      Checks whether the header of the file is ok.
      private void readInterfaces()
      Reads information about the interfaces implemented by this class.
      private void readMethods()
      Reads information about the methods of the class.
      private void readVersion()
      Reads major and minor version of compiler which created the file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • dataInputStream

        private java.io.DataInputStream dataInputStream
      • fileOwned

        private final boolean fileOwned
      • fileName

        private final java.lang.String fileName
      • zipFile

        private java.lang.String zipFile
      • classNameIndex

        private int classNameIndex
      • superclassNameIndex

        private int superclassNameIndex
      • major

        private int major
      • minor

        private int minor
      • accessFlags

        private int accessFlags
      • interfaces

        private int[] interfaces
      • fields

        private Field[] fields
      • methods

        private Method[] methods
      • isZip

        private final boolean isZip
    • Constructor Detail

      • ClassParser

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

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

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

      • parse

        public JavaClass parse()
                        throws java.io.IOException,
                               ClassFormatException
        Parses the given Java class file and return an object that represents the contained data, i.e., 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).
        Returns:
        Class object representing the parsed class file
        Throws:
        java.io.IOException - if an I/O error occurs.
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • readAttributes

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

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

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

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

        private void readID()
                     throws java.io.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:
        java.io.IOException - if an I/O error occurs.
        ClassFormatException - if a class is malformed or cannot be interpreted as a class file
      • readInterfaces

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

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

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