public class ClassReader extends Object
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.Modifier and Type | Field and Description |
---|---|
byte[] |
b
The class to be parsed.
|
Constructor and Description |
---|
ClassReader(byte[] b)
Constructs a new
ClassReader object. |
ClassReader(byte[] b,
int off,
int len)
Constructs a new
ClassReader object. |
ClassReader(InputStream is)
Constructs a new
ClassReader object. |
ClassReader(String name)
Constructs a new
ClassReader object. |
Modifier and Type | Method and Description |
---|---|
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 . |
int |
getItem(int item)
Returns the start index of the constant pool item in
b , plus
one. |
protected Attribute |
readAttribute(Attribute[] attrs,
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 . |
String |
readClass(int index,
char[] buf)
Reads a class constant pool item in
b . |
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 . |
short |
readShort(int index)
Reads a signed short value in
b . |
int |
readUnsignedShort(int index)
Reads an unsigned short value in
b . |
String |
readUTF8(int index,
char[] buf)
Reads an UTF8 string constant pool item in
b . |
public final byte[] b
Attribute
sub classes, and is
normally not needed by class generators or adapters.public ClassReader(byte[] b)
ClassReader
object.b
- the bytecode of the class to be read.public ClassReader(byte[] b, int off, int len)
ClassReader
object.b
- the bytecode of the class to be read.off
- the start offset of the class data.len
- the length of the class data.public ClassReader(InputStream is) throws IOException
ClassReader
object.is
- an input stream from which to read the class.IOException
- if a problem occurs during reading.public ClassReader(String name) throws IOException
ClassReader
object.name
- the fully qualified name of the class to be read.IOException
- if an exception occurs during reading.public void accept(ClassVisitor classVisitor, boolean skipDebug)
ClassReader
. This class is the one specified in the constructor (see
ClassReader
).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.public void accept(ClassVisitor classVisitor, Attribute[] attrs, boolean skipDebug)
ClassReader
. This class is the one specified in the constructor (see
ClassReader
).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.public int getItem(int item)
b
, plus
one. This method is intended for Attribute
sub classes, and is
normally not needed by class generators or adapters.item
- the index a constant pool item.b
, plus
one.public int readByte(int index)
b
. This method is intended
for Attribute
sub classes, and is normally not needed by class
generators or adapters.index
- the start index of the value to be read in b
.public int readUnsignedShort(int index)
b
. This method is intended
for Attribute
sub classes, and is normally not needed by class
generators or adapters.index
- the start index of the value to be read in b
.public short readShort(int index)
b
. This method is intended
for Attribute
sub classes, and is normally not needed by class
generators or adapters.index
- the start index of the value to be read in b
.public int readInt(int index)
b
. This method is intended
for Attribute
sub classes, and is normally not needed by class
generators or adapters.index
- the start index of the value to be read in b
.public long readLong(int index)
b
. This method is intended
for Attribute
sub classes, and is normally not needed by class
generators or adapters.index
- the start index of the value to be read in b
.public String readUTF8(int index, char[] buf)
b
. This method is
intended for Attribute
sub classes, and is normally not needed by
class generators or adapters.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.public String readClass(int index, char[] buf)
b
. This method is
intended for Attribute
sub classes, and is normally not needed by
class generators or adapters.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.public Object readConst(int item, char[] buf)
protected Attribute readAttribute(Attribute[] attrs, String type, int off, int len, char[] buf, int codeOff, Label[] labels)
b
.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.Copyright © 2021. All rights reserved.