org.objectweb.asm

Class ClassReader

public class ClassReader extends Object

A Java class parser to make a {@link ClassVisitor 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

Field Summary
byte[]b
The class to be parsed.
Constructor Summary
ClassReader(byte[] b)
Constructs a new {@link ClassReader ClassReader} object.
ClassReader(byte[] b, int off, int len)
Constructs a new {@link ClassReader ClassReader} object.
ClassReader(InputStream is)
Constructs a new {@link ClassReader ClassReader} object.
ClassReader(String name)
Constructs a new {@link ClassReader ClassReader} object.
Method Summary
voidaccept(ClassVisitor classVisitor, boolean skipDebug)
Makes the given visitor visit the Java class of this {@link ClassReader ClassReader}.
voidaccept(ClassVisitor classVisitor, Attribute[] attrs, boolean skipDebug)
Makes the given visitor visit the Java class of this {@link ClassReader ClassReader}.
intgetItem(int item)
Returns the start index of the constant pool item in {@link #b b}, plus one.
protected AttributereadAttribute(Attribute[] attrs, String type, int off, int len, char[] buf, int codeOff, Label[] labels)
Reads an attribute in {@link #b b}.
intreadByte(int index)
Reads a byte value in {@link #b b}.
StringreadClass(int index, char[] buf)
Reads a class constant pool item in {@link #b b}.
ObjectreadConst(int item, char[] buf)
Reads a numeric or string constant pool item in {@link #b b}.
intreadInt(int index)
Reads a signed int value in {@link #b b}.
longreadLong(int index)
Reads a signed long value in {@link #b b}.
shortreadShort(int index)
Reads a signed short value in {@link #b b}.
intreadUnsignedShort(int index)
Reads an unsigned short value in {@link #b b}.
StringreadUTF8(int index, char[] buf)
Reads an UTF8 string constant pool item in {@link #b b}.

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 {@link Attribute} sub classes, and is normally not needed by class generators or adapters.

Constructor Detail

ClassReader

public ClassReader(byte[] b)
Constructs a new {@link ClassReader ClassReader} object.

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

ClassReader

public ClassReader(byte[] b, int off, int len)
Constructs a new {@link ClassReader 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(InputStream is)
Constructs a new {@link ClassReader ClassReader} object.

Parameters: is an input stream from which to read the class.

Throws: IOException if a problem occurs during reading.

ClassReader

public ClassReader(String name)
Constructs a new {@link ClassReader ClassReader} object.

Parameters: name the fully qualified name of the class to be read.

Throws: IOException if an exception occurs during reading.

Method Detail

accept

public void accept(ClassVisitor classVisitor, boolean skipDebug)
Makes the given visitor visit the Java class of this {@link ClassReader ClassReader}. This class is the one specified in the constructor (see {@link #ClassReader(byte[]) 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 {@link CodeVisitor#visitLocalVariable visitLocalVariable} and {@link CodeVisitor#visitLineNumber 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 {@link ClassReader ClassReader}. This class is the one specified in the constructor (see {@link #ClassReader(byte[]) 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 {@link CodeVisitor#visitLocalVariable visitLocalVariable} and {@link CodeVisitor#visitLineNumber visitLineNumber} methods will not be called.

getItem

public int getItem(int item)
Returns the start index of the constant pool item in {@link #b b}, plus one. This method is intended for {@link 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 {@link #b b}, plus one.

readAttribute

protected Attribute readAttribute(Attribute[] attrs, String type, int off, int len, char[] buf, int codeOff, Label[] labels)
Reads an attribute in {@link #b 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 {@link Attribute} instance is returned). type the type of the attribute. off index of the first byte of the attribute's content in {@link #b 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 {@link #readUTF8 readUTF8}, {@link #readClass(int,char[]) readClass} or {@link #readConst readConst}. codeOff index of the first byte of code's attribute content in {@link #b 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.

readByte

public int readByte(int index)
Reads a byte value in {@link #b b}. This method is intended for {@link 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 {@link #b b}.

Returns: the read value.

readClass

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

Parameters: index the start index of an unsigned short value in {@link #b 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 Object readConst(int item, char[] buf)
Reads a numeric or string constant pool item in {@link #b b}. This method is intended for {@link 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 {@link java.lang.Integer Integer}, {@link java.lang.Float Float}, {@link java.lang.Long Long}, {@link java.lang.Double Double}, {@link String String} or {@link Type Type} corresponding to the given constant pool item.

readInt

public int readInt(int index)
Reads a signed int value in {@link #b b}. This method is intended for {@link 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 {@link #b b}.

Returns: the read value.

readLong

public long readLong(int index)
Reads a signed long value in {@link #b b}. This method is intended for {@link 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 {@link #b b}.

Returns: the read value.

readShort

public short readShort(int index)
Reads a signed short value in {@link #b b}. This method is intended for {@link 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 {@link #b b}.

Returns: the read value.

readUnsignedShort

public int readUnsignedShort(int index)
Reads an unsigned short value in {@link #b b}. This method is intended for {@link 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 {@link #b b}.

Returns: the read value.

readUTF8

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

Parameters: index the start index of an unsigned short value in {@link #b 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.