类 ClassReader


  • public class ClassReader
    extends java.lang.Object
    Copy from ASM, with less modifications A parser to make a visit a ClassFile structure, as defined in the Java Virtual Machine Specification (JVMS). This class parses the ClassFile content and calls the appropriate visit methods of a given ClassVisitor for each field, method and bytecode instruction encountered.
    作者:
    Eric Bruneton, Eugene Kuleshov
    另请参阅:
    JVMS 4
    • 字段概要

      字段 
      修饰符和类型 字段 说明
      byte[] b
      已过时。
      Use readByte(int) and the other read methods instead.
      (专用程序包) byte[] classFileBuffer
      A byte array containing the JVMS ClassFile structure to be parsed.
      private java.lang.String[] constantUtf8Values
      The String objects corresponding to the CONSTANT_Utf8 constant pool items.
      private int[] cpInfoOffsets
      The offset in bytes, in classFileBuffer, of each cp_info entry of the ClassFile's constant_pool array, plus one.
      (专用程序包) static int EXPAND_ASM_INSNS
      A flag to expand the ASM specific instructions into an equivalent sequence of standard bytecode instructions.
      static int EXPAND_FRAMES
      A flag to expand the stack map frames.
      int header
      The offset in bytes of the ClassFile's access_flags field.
      private static int INPUT_STREAM_DATA_CHUNK_SIZE
      The size of the temporary byte array used to read class input streams chunk by chunk.
      private static int MAX_BUFFER_SIZE
      The maximum size of array to allocate.
      private int maxStringLength
      A conservative estimate of the maximum length of the strings contained in the constant pool of the class.
      static int SKIP_CODE
      A flag to skip the Code attributes.
      static int SKIP_DEBUG
      A flag to skip the SourceFile.
      static int SKIP_FRAMES
      A flag to skip the StackMap and StackMapTable attributes.
      static int V19  
    • 方法概要

      所有方法 静态方法 实例方法 具体方法 
      修饰符和类型 方法 说明
      private static int calculateBufferSize​(java.io.InputStream inputStream)  
      int getAccess()
      Returns the class's access flags .
      java.lang.String getClassName()
      the internal class name.
      (专用程序包) int getFirstAttributeOffset()
      Returns the offset in classFileBuffer of the first ClassFile's 'attributes' array field entry.
      java.lang.String[] getInterfaces()
      the internal names of the directly implemented interfaces.
      int getItem​(int constantPoolEntryIndex)
      Returns the start offset in this ClassReader of a JVMS 'cp_info' structure (i.e. a constant pool entry), plus one.
      int getItemCount()
      Returns the number of entries in the class's constant pool table.
      int getMaxStringLength()
      Returns a conservative estimate of the maximum length of the strings contained in the class's constant pool table.
      java.lang.String getSuperName()  
      int readByte​(int offset)
      Reads a byte value in this ClassReader.
      java.lang.String readClass​(int offset, char[] charBuffer)
      Reads a CONSTANT_Class constant pool entry in this ClassReader.
      int readInt​(int offset)
      Reads a signed int value in this ClassReader.
      long readLong​(int offset)
      Reads a signed long value in this ClassReader.
      java.lang.String readModule​(int offset, char[] charBuffer)
      Reads a CONSTANT_Module constant pool entry in this ClassReader.
      java.lang.String readPackage​(int offset, char[] charBuffer)
      Reads a CONSTANT_Package constant pool entry in this ClassReader.
      short readShort​(int offset)
      Reads a signed short value in this ClassReader.
      private static byte[] readStream​(java.io.InputStream inputStream, boolean close)
      Reads the given input stream and returns its content as a byte array.
      private java.lang.String readStringish​(int offset, char[] charBuffer)
      Reads a CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_Module or CONSTANT_Package constant pool entry in classFileBuffer.
      int readUnsignedShort​(int offset)
      Reads an unsigned short value in this ClassReader.
      (专用程序包) java.lang.String readUtf​(int constantPoolEntryIndex, char[] charBuffer)
      Reads a CONSTANT_Utf8 constant pool entry in classFileBuffer.
      private java.lang.String readUtf​(int utfOffset, int utfLength, char[] charBuffer)
      Reads an UTF8 string in classFileBuffer.
      java.lang.String readUtf8​(int offset, char[] charBuffer)
      Reads a CONSTANT_Utf8 constant pool entry in this ClassReader.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 字段详细资料

      • SKIP_CODE

        public static final int SKIP_CODE
        A flag to skip the Code attributes. If this flag is set the Code attributes are neither parsed nor visited.
        另请参阅:
        常量字段值
      • SKIP_DEBUG

        public static final int SKIP_DEBUG
        A flag to skip the SourceFile.
        另请参阅:
        常量字段值
      • SKIP_FRAMES

        public static final int SKIP_FRAMES
        A flag to skip the StackMap and StackMapTable attributes. If this flag is set these attributes are neither parsed nor visited is not called). This flag is useful when the option is used: it avoids visiting frames that will be ignored and recomputed from scratch.
        另请参阅:
        常量字段值
      • EXPAND_FRAMES

        public static final int EXPAND_FRAMES
        A flag to expand the stack map frames. By default stack map frames are visited in their original format (i.e. "expanded" for classes whose version is less than V1_6, and "compressed" for the other classes). If this flag is set, stack map frames are always visited in expanded format (this option adds a decompression/compression step in ClassReader and ClassWriter which degrades performance quite a lot).
        另请参阅:
        常量字段值
      • EXPAND_ASM_INSNS

        static final int EXPAND_ASM_INSNS
        A flag to expand the ASM specific instructions into an equivalent sequence of standard bytecode instructions. When resolving a forward jump it may happen that the signed 2 bytes offset reserved for it is not sufficient to store the bytecode offset. This internal flag is used to re-read classes containing such instructions, in order to replace them with standard instructions. In addition, when this flag is used, goto_w and jsr_w are not converted into goto and jsr, to make sure that infinite loops where a goto_w is replaced with a goto in ClassReader and converted back to a goto_w in ClassWriter cannot occur.
        另请参阅:
        常量字段值
      • MAX_BUFFER_SIZE

        private static final int MAX_BUFFER_SIZE
        The maximum size of array to allocate.
        另请参阅:
        常量字段值
      • INPUT_STREAM_DATA_CHUNK_SIZE

        private static final int INPUT_STREAM_DATA_CHUNK_SIZE
        The size of the temporary byte array used to read class input streams chunk by chunk.
        另请参阅:
        常量字段值
      • b

        @Deprecated
        public final byte[] b
        已过时。
        Use readByte(int) and the other read methods instead. This field will eventually be deleted.
        A byte array containing the JVMS ClassFile structure to be parsed.
      • header

        public final int header
        The offset in bytes of the ClassFile's access_flags field.
      • classFileBuffer

        final byte[] classFileBuffer
        A byte array containing the JVMS ClassFile structure to be parsed. The content of this array must not be modified. This field is intended for Attribute sub classes, and is normally not needed by class visitors.

        NOTE: the ClassFile structure can start at any offset within this array, i.e. it does not necessarily start at offset 0. Use getItem(int) and header to get correct ClassFile element offsets within this byte array.

      • cpInfoOffsets

        private final int[] cpInfoOffsets
        The offset in bytes, in classFileBuffer, of each cp_info entry of the ClassFile's constant_pool array, plus one. In other words, the offset of constant pool entry i is given by cpInfoOffsets[i] - 1, i.e. its cp_info's tag field is given by b[cpInfoOffsets[i] - 1].
      • constantUtf8Values

        private final java.lang.String[] constantUtf8Values
        The String objects corresponding to the CONSTANT_Utf8 constant pool items. This cache avoids multiple parsing of a given CONSTANT_Utf8 constant pool item.
      • maxStringLength

        private final int maxStringLength
        A conservative estimate of the maximum length of the strings contained in the constant pool of the class.
    • 构造器详细资料

      • ClassReader

        public ClassReader​(byte[] classFile)
        Constructs a new ClassReader object.
        参数:
        classFile - the JVMS ClassFile structure to be read.
      • ClassReader

        public ClassReader​(byte[] classFileBuffer,
                           int classFileOffset,
                           int classFileLength)
        Constructs a new ClassReader object.
        参数:
        classFileBuffer - a byte array containing the JVMS ClassFile structure to be read.
        classFileOffset - the offset in byteBuffer of the first byte of the ClassFile to be read.
        classFileLength - the length in bytes of the ClassFile to be read.
      • ClassReader

        ClassReader​(byte[] classFileBuffer,
                    int classFileOffset,
                    boolean checkClassVersion)
        Constructs a new ClassReader object. This internal constructor must not be exposed as a public API.
        参数:
        classFileBuffer - a byte array containing the JVMS ClassFile structure to be read.
        classFileOffset - the offset in byteBuffer of the first byte of the ClassFile to be read.
        checkClassVersion - whether to check the class version or not.
      • ClassReader

        public ClassReader​(java.io.InputStream inputStream)
                    throws java.io.IOException
        Constructs a new ClassReader object.
        参数:
        inputStream - an input stream of the JVMS ClassFile structure to be read. This input stream must contain nothing more than the ClassFile structure itself. It is read from its current position to its end.
        抛出:
        java.io.IOException - if a problem occurs during reading.
      • ClassReader

        public ClassReader​(java.lang.String className)
                    throws java.io.IOException
        Constructs a new ClassReader object.
        参数:
        className - the fully qualified name of the class to be read. The ClassFile structure is retrieved with the current class loader's ClassLoader.getSystemResourceAsStream(java.lang.String).
        抛出:
        java.io.IOException - if an exception occurs during reading.
    • 方法详细资料

      • readStream

        private static byte[] readStream​(java.io.InputStream inputStream,
                                         boolean close)
                                  throws java.io.IOException
        Reads the given input stream and returns its content as a byte array.
        参数:
        inputStream - an input stream.
        close - true to close the input stream after reading.
        返回:
        the content of the given input stream.
        抛出:
        java.io.IOException - if a problem occurs during reading.
      • calculateBufferSize

        private static int calculateBufferSize​(java.io.InputStream inputStream)
                                        throws java.io.IOException
        抛出:
        java.io.IOException
      • getAccess

        public int getAccess()
        Returns the class's access flags . This value may not reflect Deprecated and Synthetic flags when bytecode is before 1.5 and those flags are represented by attributes.
        返回:
        the class access flags.
      • getClassName

        public java.lang.String getClassName()
        the internal class name.
      • getSuperName

        public java.lang.String getSuperName()
      • getInterfaces

        public java.lang.String[] getInterfaces()
        the internal names of the directly implemented interfaces. Inherited implemented interfaces are not returned.
      • getFirstAttributeOffset

        final int getFirstAttributeOffset()
        Returns the offset in classFileBuffer of the first ClassFile's 'attributes' array field entry.
        返回:
        the offset in classFileBuffer of the first ClassFile's 'attributes' array field entry.
      • getItemCount

        public int getItemCount()
        Returns the number of entries in the class's constant pool table.
        返回:
        the number of entries in the class's constant pool table.
      • getItem

        public int getItem​(int constantPoolEntryIndex)
        Returns the start offset in this ClassReader of a JVMS 'cp_info' structure (i.e. a constant pool entry), plus one. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        constantPoolEntryIndex - the index a constant pool entry in the class's constant pool table.
        返回:
        the start offset in this ClassReader of the corresponding JVMS 'cp_info' structure, plus one.
      • getMaxStringLength

        public int getMaxStringLength()
        Returns a conservative estimate of the maximum length of the strings contained in the class's constant pool table.
        返回:
        a conservative estimate of the maximum length of the strings contained in the class's constant pool table.
      • readByte

        public int readByte​(int offset)
        Reads a byte value in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of the value to be read in this ClassReader.
        返回:
        the read value.
      • readUnsignedShort

        public int readUnsignedShort​(int offset)
        Reads an unsigned short value in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start index of the value to be read in this ClassReader.
        返回:
        the read value.
      • readShort

        public short readShort​(int offset)
        Reads a signed short value in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of the value to be read in this ClassReader.
        返回:
        the read value.
      • readInt

        public int readInt​(int offset)
        Reads a signed int value in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of the value to be read in this ClassReader.
        返回:
        the read value.
      • readLong

        public long readLong​(int offset)
        Reads a signed long value in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of the value to be read in this ClassReader.
        返回:
        the read value.
      • readUtf8

        public java.lang.String readUtf8​(int offset,
                                         char[] charBuffer)
        Reads a CONSTANT_Utf8 constant pool entry in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Utf8 entry in the class's constant pool table.
        charBuffer - the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified CONSTANT_Utf8 entry.
      • readUtf

        final java.lang.String readUtf​(int constantPoolEntryIndex,
                                       char[] charBuffer)
        Reads a CONSTANT_Utf8 constant pool entry in classFileBuffer.
        参数:
        constantPoolEntryIndex - the index of a CONSTANT_Utf8 entry in the class's constant pool table.
        charBuffer - the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified CONSTANT_Utf8 entry.
      • readUtf

        private java.lang.String readUtf​(int utfOffset,
                                         int utfLength,
                                         char[] charBuffer)
        Reads an UTF8 string in classFileBuffer.
        参数:
        utfOffset - the start offset of the UTF8 string to be read.
        utfLength - the length of the UTF8 string to be read.
        charBuffer - the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified UTF8 string.
      • readStringish

        private java.lang.String readStringish​(int offset,
                                               char[] charBuffer)
        Reads a CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_Module or CONSTANT_Package constant pool entry in classFileBuffer. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of an unsigned short value in classFileBuffer, whose value is the index of a CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_Module or CONSTANT_Package entry in class's constant pool table.
        charBuffer - the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified constant pool entry.
      • readClass

        public java.lang.String readClass​(int offset,
                                          char[] charBuffer)
        Reads a CONSTANT_Class constant pool entry in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Class entry in class's constant pool table.
        charBuffer - the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified CONSTANT_Class entry.
      • readModule

        public java.lang.String readModule​(int offset,
                                           char[] charBuffer)
        Reads a CONSTANT_Module constant pool entry in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Module entry in class's constant pool table.
        charBuffer - the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified CONSTANT_Module entry.
      • readPackage

        public java.lang.String readPackage​(int offset,
                                            char[] charBuffer)
        Reads a CONSTANT_Package constant pool entry in this ClassReader. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
        参数:
        offset - the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Package entry in class's constant pool table.
        charBuffer - the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
        返回:
        the String corresponding to the specified CONSTANT_Package entry.