Package org.mvel2.asm

Class ClassReader

java.lang.Object
org.mvel2.asm.ClassReader

public class ClassReader extends Object
A parser to make a ClassVisitor 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.
Author:
Eric Bruneton, Eugene Kuleshov
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final byte[]
    A byte array containing the JVMS ClassFile structure to be parsed.
    static final int
    A flag to expand the stack map frames.
    final int
    The offset in bytes, in b, of the ClassFile's access_flags field.
    static final int
    A flag to skip the Code attributes.
    static final int
    A flag to skip the SourceFile, SourceDebugExtension, LocalVariableTable, LocalVariableTypeTable and LineNumberTable attributes.
    static final int
    A flag to skip the StackMap and StackMapTable attributes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ClassReader(byte[] classFile)
    Constructs a new ClassReader object.
    ClassReader(byte[] classFileBuffer, int classFileOffset, int classFileLength)
    Constructs a new ClassReader object.
    ClassReader(InputStream inputStream)
    Constructs a new ClassReader object.
    ClassReader(String className)
    Constructs a new ClassReader object.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    accept(ClassVisitor classVisitor, int parsingOptions)
    Makes the given visitor visit the JVMS ClassFile structure passed to the constructor of this ClassReader.
    void
    accept(ClassVisitor classVisitor, Attribute[] attributePrototypes, int parsingOptions)
    Makes the given visitor visit the JVMS ClassFile structure passed to the constructor of this ClassReader.
    int
    Returns the class's access flags (see Opcodes).
    Returns the internal name of the class (see Type.getInternalName()).
    Returns the internal names of the implemented interfaces (see Type.getInternalName()).
    int
    getItem(int constantPoolEntryIndex)
    Returns the start offset in b of a JVMS 'cp_info' structure (i.e.
    int
    Returns the number of entries in the class's constant pool table.
    int
    Returns a conservative estimate of the maximum length of the strings contained in the class's constant pool table.
    Returns the internal of name of the super class (see Type.getInternalName()).
    int
    readByte(int offset)
    Reads a byte value in b.
    readClass(int offset, char[] charBuffer)
    Reads a CONSTANT_Class constant pool entry in b.
    readConst(int constantPoolEntryIndex, char[] charBuffer)
    Reads a numeric or string constant pool entry in b.
    int
    readInt(int offset)
    Reads a signed int value in b.
    protected Label
    readLabel(int bytecodeOffset, Label[] labels)
    Returns the label corresponding to the given bytecode offset.
    long
    readLong(int offset)
    Reads a signed long value in b.
    readModule(int offset, char[] charBuffer)
    Reads a CONSTANT_Module constant pool entry in b.
    readPackage(int offset, char[] charBuffer)
    Reads a CONSTANT_Package constant pool entry in b.
    short
    readShort(int offset)
    Reads a signed short value in b.
    int
    readUnsignedShort(int offset)
    Reads an unsigned short value in b.
    readUTF8(int offset, char[] charBuffer)
    Reads a CONSTANT_Utf8 constant pool entry in b.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • ClassReader

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

      public ClassReader(byte[] classFileBuffer, int classFileOffset, int classFileLength)
      Constructs a new ClassReader object.
      Parameters:
      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

      public ClassReader(InputStream inputStream) throws IOException
      Constructs a new ClassReader object.
      Parameters:
      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.
      Throws:
      IOException - if a problem occurs during reading.
    • ClassReader

      public ClassReader(String className) throws IOException
      Constructs a new ClassReader object.
      Parameters:
      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).
      Throws:
      IOException - if an exception occurs during reading.
  • Method Details

    • getAccess

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

      public String getClassName()
      Returns the internal name of the class (see Type.getInternalName()).
      Returns:
      the internal class name.
      See Also:
    • getSuperName

      public String getSuperName()
      Returns the internal of name of the super class (see Type.getInternalName()). For interfaces, the super class is Object.
      Returns:
      the internal name of the super class, or null for Object class.
      See Also:
    • getInterfaces

      public String[] getInterfaces()
      Returns the internal names of the implemented interfaces (see Type.getInternalName()).
      Returns:
      the internal names of the directly implemented interfaces. Inherited implemented interfaces are not returned.
      See Also:
    • accept

      public void accept(ClassVisitor classVisitor, int parsingOptions)
      Makes the given visitor visit the JVMS ClassFile structure passed to the constructor of this ClassReader.
      Parameters:
      classVisitor - the visitor that must visit this class.
      parsingOptions - the options to use to parse this class. One or more of SKIP_CODE, SKIP_DEBUG, SKIP_FRAMES or EXPAND_FRAMES.
    • accept

      public void accept(ClassVisitor classVisitor, Attribute[] attributePrototypes, int parsingOptions)
      Makes the given visitor visit the JVMS ClassFile structure passed to the constructor of this ClassReader.
      Parameters:
      classVisitor - the visitor that must visit this class.
      attributePrototypes - prototypes of the attributes that must be parsed during the visit of the class. Any attribute whose type is not equal to the type of one the prototypes will not be parsed: its byte array value will be passed unchanged to the ClassWriter. This may corrupt it if this value contains references to the constant pool, or has syntactic or semantic links with a class element that has been transformed by a class adapter between the reader and the writer.
      parsingOptions - the options to use to parse this class. One or more of SKIP_CODE, SKIP_DEBUG, SKIP_FRAMES or EXPAND_FRAMES.
    • readLabel

      protected Label readLabel(int bytecodeOffset, Label[] labels)
      Returns the label corresponding to the given bytecode offset. The default implementation of this method creates a label for the given offset if it has not been already created.
      Parameters:
      bytecodeOffset - a bytecode offset in a method.
      labels - the already created labels, indexed by their offset. If a label already exists for bytecodeOffset this method must not create a new one. Otherwise it must store the new label in this array.
      Returns:
      a non null Label, which must be equal to labels[bytecodeOffset].
    • getItemCount

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

      public int getItem(int constantPoolEntryIndex)
      Returns the start offset in b 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.
      Parameters:
      constantPoolEntryIndex - the index a constant pool entry in the class's constant pool table.
      Returns:
      the start offset in b 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.
      Returns:
      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 b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      offset - the start offset of the value to be read in b.
      Returns:
      the read value.
    • readUnsignedShort

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

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

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

      public long readLong(int offset)
      Reads a signed long value in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      offset - the start offset of the value to be read in b.
      Returns:
      the read value.
    • readUTF8

      public String readUTF8(int offset, char[] charBuffer)
      Reads a CONSTANT_Utf8 constant pool entry in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      offset - the start offset of an unsigned short value in b, 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.
      Returns:
      the String corresponding to the specified CONSTANT_Utf8 entry.
    • readClass

      public String readClass(int offset, char[] charBuffer)
      Reads a CONSTANT_Class constant pool entry in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      offset - the start offset of an unsigned short value in b, 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.
      Returns:
      the String corresponding to the specified CONSTANT_Class entry.
    • readModule

      public String readModule(int offset, char[] charBuffer)
      Reads a CONSTANT_Module constant pool entry in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      offset - the start offset of an unsigned short value in b, 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.
      Returns:
      the String corresponding to the specified CONSTANT_Module entry.
    • readPackage

      public String readPackage(int offset, char[] charBuffer)
      Reads a CONSTANT_Package constant pool entry in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      offset - the start offset of an unsigned short value in b, 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.
      Returns:
      the String corresponding to the specified CONSTANT_Package entry.
    • readConst

      public Object readConst(int constantPoolEntryIndex, char[] charBuffer)
      Reads a numeric or string constant pool entry in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.
      Parameters:
      constantPoolEntryIndex - the index of a CONSTANT_Integer, CONSTANT_Float, CONSTANT_Long, CONSTANT_Double, CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_MethodHandle or CONSTANT_Dynamic entry in the class's constant pool.
      charBuffer - the buffer to be used to read strings. This buffer must be sufficiently large. It is not automatically resized.
      Returns:
      the Integer, Float, Long, Double, String, Type, Handle or ConstantDynamic corresponding to the specified constant pool entry.