public class ClassReader
extends java.lang.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 |
---|---|
(package private) static boolean |
ANNOTATIONS
True to enable annotations support.
|
byte[] |
b
The class to be parsed.
|
static int |
EXPAND_FRAMES
Flag to expand the stack map frames.
|
(package private) static boolean |
FRAMES
True to enable stack map frames support.
|
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.
|
(package private) static boolean |
RESIZE
True to enable JSR_W and GOTO_W support.
|
(package private) static boolean |
SIGNATURES
True to enable signatures support.
|
static int |
SKIP_CODE
Flag to skip method code.
|
static int |
SKIP_DEBUG
Flag to skip the debug information in the class.
|
static int |
SKIP_FRAMES
Flag to skip the stack map frames in the class.
|
private java.lang.String[] |
strings
The String objects corresponding to the CONSTANT_Utf8 items.
|
(package private) static boolean |
WRITER
True to enable bytecode writing support.
|
Constructor and Description |
---|
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. |
Modifier and Type | Method and Description |
---|---|
void |
accept(ClassVisitor classVisitor,
Attribute[] attrs,
int flags)
Makes the given visitor visit the Java class of this
ClassReader . |
void |
accept(ClassVisitor classVisitor,
int flags)
Makes the given visitor visit the Java class of this
ClassReader
. |
private void |
copyBootstrapMethods(ClassWriter classWriter,
Item[] items,
char[] c)
Copies the bootstrap method data into the given
ClassWriter . |
(package private) void |
copyPool(ClassWriter classWriter)
Copies the constant pool data into the given
ClassWriter . |
int |
getAccess()
Returns the class's access flags (see
Opcodes ). |
private int |
getAttributes()
Returns the start index of the attribute_info structure of this class.
|
java.lang.String |
getClassName()
Returns the internal name of the class (see
getInternalName ). |
private void |
getImplicitFrame(Context frame)
Computes the implicit frame of the method currently being parsed (as
defined in the given
Context ) and stores it in the given context. |
java.lang.String[] |
getInterfaces()
Returns the internal names of the class's interfaces (see
getInternalName ). |
int |
getItem(int item)
Returns the start index of the constant pool item in
b , plus
one. |
int |
getItemCount()
Returns the number of constant pool items in
b . |
int |
getMaxStringLength()
Returns the maximum length of the strings contained in the constant pool
of the class.
|
java.lang.String |
getSuperName()
Returns the internal of name of the super class (see
getInternalName ). |
private int |
readAnnotationTarget(Context context,
int u)
Parses the header of a type annotation to extract its target_type and
target_path (the result is stored in the given context), and returns the
start offset of the rest of the type_annotation structure (i.e.
|
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,
boolean named,
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,
boolean close)
Reads the bytecode of a class.
|
java.lang.String |
readClass(int index,
char[] buf)
Reads a class constant pool item in
b . |
private void |
readCode(MethodVisitor mv,
Context context,
int u)
Reads the bytecode of a method and makes the given visitor visit it.
|
java.lang.Object |
readConst(int item,
char[] buf)
Reads a numeric or string constant pool item in
b . |
private int |
readField(ClassVisitor classVisitor,
Context context,
int u)
Reads a field and makes the given visitor visit it.
|
private int |
readFrame(int stackMap,
boolean zip,
boolean unzip,
Context frame)
Reads a stack map frame and stores the result in the given
Context object. |
private int |
readFrameType(java.lang.Object[] frame,
int index,
int v,
char[] buf,
Label[] labels)
Reads a stack map frame type and stores it at the given index in the
given array.
|
int |
readInt(int index)
Reads a signed int value in
b . |
protected Label |
readLabel(int offset,
Label[] labels)
Returns the label corresponding to the given offset.
|
long |
readLong(int index)
Reads a signed long value in
b . |
private int |
readMethod(ClassVisitor classVisitor,
Context context,
int u)
Reads a method and makes the given visitor visit it.
|
private void |
readParameterAnnotations(MethodVisitor mv,
Context context,
int v,
boolean visible)
Reads parameter annotations and makes the given visitor visit them.
|
short |
readShort(int index)
Reads a signed short value in
b . |
private int[] |
readTypeAnnotations(MethodVisitor mv,
Context context,
int u,
boolean visible)
Parses a type annotation table to find the labels, and to visit the try
catch block annotations.
|
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 . |
static final boolean SIGNATURES
static final boolean ANNOTATIONS
static final boolean FRAMES
static final boolean WRITER
static final boolean RESIZE
public static final int SKIP_CODE
CODE
attribute won't be visited. This can be used, for example, to retrieve
annotations for methods and method parameters.public static final int SKIP_DEBUG
visitLocalVariable
and
visitLineNumber
methods will not be
called.public static final int SKIP_FRAMES
visitFrame
method will not be called.
This flag is useful when the ClassWriter.COMPUTE_FRAMES
option is
used: it avoids visiting frames that will be ignored and recomputed from
scratch in the class writer.public static final int EXPAND_FRAMES
public final byte[] b
Attribute
sub classes, and
is normally not needed by class generators or adapters.private final int[] items
b
, plus one. The
one byte offset skips the constant pool item tag that indicates its type.private final java.lang.String[] strings
private final int maxStringLength
public final int header
b
.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(java.io.InputStream is) throws java.io.IOException
ClassReader
object.is
- an input stream from which to read the class.java.io.IOException
- if a problem occurs during reading.public ClassReader(java.lang.String name) throws java.io.IOException
ClassReader
object.name
- the binary qualified name of the class to be read.java.io.IOException
- if an exception occurs during reading.public int getAccess()
Opcodes
). This value may
not reflect Deprecated and Synthetic flags when bytecode is before 1.5
and those flags are represented by attributes.ClassVisitor.visit(int, int, String, String, String, String[])
public java.lang.String getClassName()
getInternalName
).ClassVisitor.visit(int, int, String, String, String, String[])
public java.lang.String getSuperName()
getInternalName
). For interfaces, the
super class is Object
.Object
class.ClassVisitor.visit(int, int, String, String, String, String[])
public java.lang.String[] getInterfaces()
getInternalName
).ClassVisitor.visit(int, int, String, String, String, String[])
void copyPool(ClassWriter classWriter)
ClassWriter
. Should
be called before the accept(ClassVisitor,int)
method.classWriter
- the ClassWriter
to copy constant pool into.private void copyBootstrapMethods(ClassWriter classWriter, Item[] items, char[] c)
ClassWriter
.
Should be called before the accept(ClassVisitor,int)
method.classWriter
- the ClassWriter
to copy bootstrap methods into.private static byte[] readClass(java.io.InputStream is, boolean close) throws java.io.IOException
is
- an input stream from which to read the class.close
- true to close the input stream after reading.java.io.IOException
- if a problem occurs during reading.public void accept(ClassVisitor classVisitor, int flags)
ClassReader
. This class is the one specified in the constructor (see
ClassReader
).classVisitor
- the visitor that must visit this class.flags
- option flags that can be used to modify the default behavior
of this class. See SKIP_DEBUG
, EXPAND_FRAMES
, SKIP_FRAMES
, SKIP_CODE
.public void accept(ClassVisitor classVisitor, Attribute[] attrs, int flags)
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 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.flags
- option flags that can be used to modify the default behavior
of this class. See SKIP_DEBUG
, EXPAND_FRAMES
, SKIP_FRAMES
, SKIP_CODE
.private int readField(ClassVisitor classVisitor, Context context, int u)
classVisitor
- the visitor that must visit the field.context
- information about the class being parsed.u
- the start offset of the field in the class file.private int readMethod(ClassVisitor classVisitor, Context context, int u)
classVisitor
- the visitor that must visit the method.context
- information about the class being parsed.u
- the start offset of the method in the class file.private void readCode(MethodVisitor mv, Context context, int u)
mv
- the visitor that must visit the method's code.context
- information about the class being parsed.u
- the start offset of the code attribute in the class file.private int[] readTypeAnnotations(MethodVisitor mv, Context context, int u, boolean visible)
u
- the start offset of a type annotation table.mv
- the method visitor to be used to visit the try catch block
annotations.context
- information about the class being parsed.visible
- if the type annotation table to parse contains runtime visible
annotations.private int readAnnotationTarget(Context context, int u)
context
- information about the class being parsed. This is where the
extracted target_type and target_path must be stored.u
- the start offset of a type_annotation structure.private void readParameterAnnotations(MethodVisitor mv, Context context, int v, boolean visible)
mv
- the visitor that must visit the annotations.context
- information about the class being parsed.v
- start offset in b
of the annotations to be read.visible
- true if the annotations to be read are visible at
runtime.private int readAnnotationValues(int v, char[] buf, boolean named, AnnotationVisitor av)
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
.named
- if the annotation values are named or not.av
- the visitor that must visit the values.private int readAnnotationValue(int v, char[] buf, java.lang.String name, AnnotationVisitor av)
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.private void getImplicitFrame(Context frame)
Context
) and stores it in the given context.frame
- information about the class being parsed.private int readFrame(int stackMap, boolean zip, boolean unzip, Context frame)
Context
object.stackMap
- the start offset of a stack map frame in the class file.zip
- if the stack map frame at stackMap is compressed or not.unzip
- if the stack map frame must be uncompressed.frame
- where the parsed stack map frame must be stored.private int readFrameType(java.lang.Object[] frame, int index, int v, char[] buf, Label[] labels)
frame
- the array where the parsed type must be stored.index
- the index in 'frame' where the parsed type must be stored.v
- the start offset of the stack map frame type to read.buf
- a buffer to read strings.labels
- the labels of the method currently being parsed, indexed by
their offset. If the parsed type is an Uninitialized type, a
new label for the corresponding NEW instruction is stored in
this array if it does not already exist.protected Label readLabel(int offset, Label[] labels)
offset
- a bytecode offset in a method.labels
- the already created labels, indexed by their offset. If a
label already exists for offset this method must not create a
new one. Otherwise it must store the new label in this array.private int getAttributes()
private Attribute readAttribute(Attribute[] attrs, java.lang.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.public int getItemCount()
b
.b
.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 getMaxStringLength()
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 java.lang.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.private java.lang.String readUTF(int index, int utfLen, char[] buf)
b
.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.public java.lang.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 java.lang.Object readConst(int item, char[] buf)