net.sf.retrotranslator.runtime.asm
Class ClassReader

java.lang.Object
  extended by net.sf.retrotranslator.runtime.asm.ClassReader

public class ClassReader
extends java.lang.Object

A Java class parser to make a ClassVisitor visit an existing class. This class parses a byte array conforming to the Java class file format and calls the appropriate visit methods of a given class visitor for each field, method and bytecode instruction encountered.

Author:
Eric Bruneton, Eugene Kuleshov

Field Summary
 byte[] b
          The class to be parsed.
 int header
          Start index of the class header information (access, name...) in b.
private  int[] items
          The start index of each constant pool item in b, plus one.
private  int maxStringLength
          Maximum length of the strings contained in the constant pool of the class.
private  java.lang.String[] strings
          The String objects corresponding to the CONSTANT_Utf8 items.
 
Constructor Summary
ClassReader(byte[] b)
          Constructs a new ClassReader object.
ClassReader(byte[] b, int off, int len)
          Constructs a new ClassReader object.
ClassReader(java.io.InputStream is)
          Constructs a new ClassReader object.
ClassReader(java.lang.String name)
          Constructs a new ClassReader object.
 
Method Summary
 void accept(ClassVisitor classVisitor, Attribute[] attrs, boolean skipDebug)
          Makes the given visitor visit the Java class of this ClassReader.
 void accept(ClassVisitor classVisitor, boolean skipDebug)
          Makes the given visitor visit the Java class of this ClassReader.
(package private)  void copyPool(ClassWriter classWriter)
          Copies the constant pool data into the given ClassWriter.
 int getItem(int item)
          Returns the start index of the constant pool item in b, plus one.
private  int readAnnotationValue(int v, char[] buf, java.lang.String name, AnnotationVisitor av)
          Reads a value of an annotation and makes the given visitor visit it.
private  int readAnnotationValues(int v, char[] buf, AnnotationVisitor av)
          Reads the values of an annotation and makes the given visitor visit them.
private  Attribute readAttribute(Attribute[] attrs, java.lang.String type, int off, int len, char[] buf, int codeOff, Label[] labels)
          Reads an attribute in b.
 int readByte(int index)
          Reads a byte value in b.
private static byte[] readClass(java.io.InputStream is)
          Reads the bytecode of a class.
 java.lang.String readClass(int index, char[] buf)
          Reads a class constant pool item in b.
 java.lang.Object readConst(int item, char[] buf)
          Reads a numeric or string constant pool item in b.
 int readInt(int index)
          Reads a signed int value in b.
 long readLong(int index)
          Reads a signed long value in b.
private  void readParameterAnnotations(int v, char[] buf, boolean visible, MethodVisitor mv)
          Reads parameter annotations and makes the given visitor visit them.
 short readShort(int index)
          Reads a signed short value in b.
 int readUnsignedShort(int index)
          Reads an unsigned short value in b.
private  java.lang.String readUTF(int index, int utfLen, char[] buf)
          Reads UTF8 string in b.
 java.lang.String readUTF8(int index, char[] buf)
          Reads an UTF8 string constant pool item in b.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

b

public final byte[] b
The class 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 generators or adapters.


items

private int[] items
The start index of each constant pool item in b, plus one. The one byte offset skips the constant pool item tag that indicates its type.


strings

private java.lang.String[] strings
The String objects corresponding to the CONSTANT_Utf8 items. This cache avoids multiple parsing of a given CONSTANT_Utf8 constant pool item, which GREATLY improves performances (by a factor 2 to 3). This caching strategy could be extended to all constant pool items, but its benefit would not be so great for these items (because they are much less expensive to parse than CONSTANT_Utf8 items).


maxStringLength

private int maxStringLength
Maximum length of the strings contained in the constant pool of the class.


header

public final int header
Start index of the class header information (access, name...) in b.

Constructor Detail

ClassReader

public ClassReader(byte[] b)
Constructs a new ClassReader object.

Parameters:
b - the bytecode of the class to be read.

ClassReader

public ClassReader(byte[] b,
                   int off,
                   int len)
Constructs a new ClassReader object.

Parameters:
b - the bytecode of the class to be read.
off - the start offset of the class data.
len - the length of the class data.

ClassReader

public ClassReader(java.io.InputStream is)
            throws java.io.IOException
Constructs a new ClassReader object.

Parameters:
is - an input stream from which to read the class.
Throws:
java.io.IOException - if a problem occurs during reading.

ClassReader

public ClassReader(java.lang.String name)
            throws java.io.IOException
Constructs a new ClassReader object.

Parameters:
name - the fully qualified name of the class to be read.
Throws:
java.io.IOException - if an exception occurs during reading.
Method Detail

copyPool

void copyPool(ClassWriter classWriter)
Copies the constant pool data into the given ClassWriter. Should be called before the accept(ClassVisitor,boolean) method.

Parameters:
classWriter - the ClassWriter to copy constant pool into.

readClass

private static byte[] readClass(java.io.InputStream is)
                         throws java.io.IOException
Reads the bytecode of a class.

Parameters:
is - an input stream from which to read the class.
Returns:
the bytecode read from the given input stream.
Throws:
java.io.IOException - if a problem occurs during reading.

accept

public void accept(ClassVisitor classVisitor,
                   boolean skipDebug)
Makes the given visitor visit the Java class of this ClassReader. This class is the one specified in the constructor (see ClassReader).

Parameters:
classVisitor - the visitor that must visit this class.
skipDebug - true if the debug information of the class must not be visited. In this case the visitLocalVariable and visitLineNumber methods will not be called.

accept

public void accept(ClassVisitor classVisitor,
                   Attribute[] attrs,
                   boolean skipDebug)
Makes the given visitor visit the Java class of this ClassReader. This class is the one specified in the constructor (see ClassReader).

Parameters:
classVisitor - the visitor that must visit this class.
attrs - 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 be ignored.
skipDebug - true if the debug information of the class must not be visited. In this case the visitLocalVariable and visitLineNumber methods will not be called.

readParameterAnnotations

private void readParameterAnnotations(int v,
                                      char[] buf,
                                      boolean visible,
                                      MethodVisitor mv)
Reads parameter annotations and makes the given visitor visit them.

Parameters:
v - start offset in b of the annotations to be read.
buf - buffer to be used to call readUTF8, readClass or readConst.
visible - true if the annotations to be read are visible at runtime.
mv - the visitor that must visit the annotations.

readAnnotationValues

private int readAnnotationValues(int v,
                                 char[] buf,
                                 AnnotationVisitor av)
Reads the values of an annotation and makes the given visitor visit them.

Parameters:
v - the start offset in b of the values to be read (including the unsigned short that gives the number of values).
buf - buffer to be used to call readUTF8, readClass or readConst.
av - the visitor that must visit the values.
Returns:
the end offset of the annotations values.

readAnnotationValue

private int readAnnotationValue(int v,
                                char[] buf,
                                java.lang.String name,
                                AnnotationVisitor av)
Reads a value of an annotation and makes the given visitor visit it.

Parameters:
v - the start offset in b of the value to be read (not including the value name constant pool index).
buf - buffer to be used to call readUTF8, readClass or readConst.
name - the name of the value to be read.
av - the visitor that must visit the value.
Returns:
the end offset of the annotation value.

readAttribute

private Attribute readAttribute(Attribute[] attrs,
                                java.lang.String type,
                                int off,
                                int len,
                                char[] buf,
                                int codeOff,
                                Label[] labels)
Reads an attribute in b.

Parameters:
attrs - 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 is ignored (i.e. an empty Attribute instance is returned).
type - the type of the attribute.
off - index of the first byte of the attribute's content in b. The 6 attribute header bytes, containing the type and the length of the attribute, are not taken into account here (they have already been read).
len - the length of the attribute's content.
buf - buffer to be used to call readUTF8, readClass or readConst.
codeOff - index of the first byte of code's attribute content in b, or -1 if the attribute to be read is not a code attribute. The 6 attribute header bytes, containing the type and the length of the attribute, are not taken into account here.
labels - the labels of the method's code, or null if the attribute to be read is not a code attribute.
Returns:
the attribute that has been read, or null to skip this attribute.

getItem

public int getItem(int item)
Returns the start index of the constant pool item in b, plus one. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

Parameters:
item - the index a constant pool item.
Returns:
the start index of the constant pool item in b, plus one.

readByte

public int readByte(int index)
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:
index - the start index of the value to be read in b.
Returns:
the read value.

readUnsignedShort

public int readUnsignedShort(int index)
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:
index - the start index of the value to be read in b.
Returns:
the read value.

readShort

public short readShort(int index)
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:
index - the start index of the value to be read in b.
Returns:
the read value.

readInt

public int readInt(int index)
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:
index - the start index of the value to be read in b.
Returns:
the read value.

readLong

public long readLong(int index)
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:
index - the start index of the value to be read in b.
Returns:
the read value.

readUTF8

public java.lang.String readUTF8(int index,
                                 char[] buf)
Reads an UTF8 string constant pool item in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

Parameters:
index - the start index of an unsigned short value in b, whose value is the index of an UTF8 constant pool item.
buf - 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 UTF8 item.

readUTF

private java.lang.String readUTF(int index,
                                 int utfLen,
                                 char[] buf)
Reads UTF8 string in b.

Parameters:
index - start offset of the UTF8 string to be read.
utfLen - length of the UTF8 string to be read.
buf - 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 UTF8 string.

readClass

public java.lang.String readClass(int index,
                                  char[] buf)
Reads a class constant pool item in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

Parameters:
index - the start index of an unsigned short value in b, whose value is the index of a class constant pool item.
buf - 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 class item.

readConst

public java.lang.Object readConst(int item,
                                  char[] buf)
Reads a numeric or string constant pool item in b. This method is intended for Attribute sub classes, and is normally not needed by class generators or adapters.

Parameters:
item - the index of a constant pool item.
buf - buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
Returns:
the Integer, Float, Long, Double, String or Type corresponding to the given constant pool item.