Package org.eclipse.sisu.space.asm
Class ClassReader
java.lang.Object
org.eclipse.sisu.space.asm.ClassReader
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.-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final boolean
True to enable annotations support.final byte[]
The class to be parsed.static final int
Flag to expand the stack map frames.(package private) static final boolean
True to enable stack map frames support.final int
Start index of the class header information (access, name...) inb
.private final int[]
The start index of each constant pool item inb
, plus one.private final int
Maximum length of the strings contained in the constant pool of the class.(package private) static final boolean
True to enable JSR_W and GOTO_W support.(package private) static final boolean
True to enable signatures support.static final int
Flag to skip method code.static final int
Flag to skip the debug information in the class.static final int
Flag to skip the stack map frames in the class.private final String[]
The String objects corresponding to the CONSTANT_Utf8 items.(package private) static final boolean
True to enable bytecode writing support. -
Constructor Summary
ConstructorsConstructorDescriptionClassReader
(byte[] b) Constructs a newClassReader
object.ClassReader
(byte[] b, int off, int len) Constructs a newClassReader
object.Constructs a newClassReader
object.ClassReader
(String name) Constructs a newClassReader
object. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(ClassVisitor classVisitor, int flags) Makes the given visitor visit the Java class of thisClassReader
.void
accept
(ClassVisitor classVisitor, Attribute[] attrs, int flags) Makes the given visitor visit the Java class of thisClassReader
.private void
copyBootstrapMethods
(ClassWriter classWriter, Item[] items, char[] c) Copies the bootstrap method data into the givenClassWriter
.(package private) void
copyPool
(ClassWriter classWriter) Copies the constant pool data into the givenClassWriter
.int
Returns the class's access flags (seeOpcodes
).private int
Returns the start index of the attribute_info structure of this class.Returns the internal name of the class (seegetInternalName
).private void
getImplicitFrame
(Context frame) Computes the implicit frame of the method currently being parsed (as defined in the givenContext
) and stores it in the given context.String[]
Returns the internal names of the class's interfaces (seegetInternalName
).int
getItem
(int item) Returns the start index of the constant pool item inb
, plus one.int
Returns the number of constant pool items inb
.int
Returns the maximum length of the strings contained in the constant pool of the class.Returns the internal of name of the super class (seegetInternalName
).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, 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, String type, int off, int len, char[] buf, int codeOff, Label[] labels) Reads an attribute inb
.int
readByte
(int index) Reads a byte value inb
.readClass
(int index, char[] buf) Reads a class constant pool item inb
.private static byte[]
readClass
(InputStream is, boolean close) Reads the bytecode of a class.private void
readCode
(MethodVisitor mv, Context context, int u) Reads the bytecode of a method and makes the given visitor visit it.readConst
(int item, char[] buf) Reads a numeric or string constant pool item inb
.private int
readField
(ClassVisitor classVisitor, Context context, int u) Reads a field and makes the given visitor visit it.private int
Reads a stack map frame and stores the result in the givenContext
object.private int
readFrameType
(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 inb
.protected Label
Returns the label corresponding to the given offset.long
readLong
(int index) Reads a signed long value inb
.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 inb
.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 inb
.private String
readUTF
(int index, int utfLen, char[] buf) Reads UTF8 string inb
.readUTF8
(int index, char[] buf) Reads an UTF8 string constant pool item inb
.
-
Field Details
-
SIGNATURES
static final boolean SIGNATURESTrue to enable signatures support.- See Also:
-
ANNOTATIONS
static final boolean ANNOTATIONSTrue to enable annotations support.- See Also:
-
FRAMES
static final boolean FRAMESTrue to enable stack map frames support.- See Also:
-
WRITER
static final boolean WRITERTrue to enable bytecode writing support.- See Also:
-
RESIZE
static final boolean RESIZETrue to enable JSR_W and GOTO_W support.- See Also:
-
SKIP_CODE
public static final int SKIP_CODEFlag to skip method code. If this class is setCODE
attribute won't be visited. This can be used, for example, to retrieve annotations for methods and method parameters.- See Also:
-
SKIP_DEBUG
public static final int SKIP_DEBUGFlag to skip the debug information in the class. If this flag is set the debug information of the class is not visited, i.e. thevisitLocalVariable
andvisitLineNumber
methods will not be called.- See Also:
-
SKIP_FRAMES
public static final int SKIP_FRAMESFlag to skip the stack map frames in the class. If this flag is set the stack map frames of the class is not visited, i.e. thevisitFrame
method will not be called. This flag is useful when theClassWriter.COMPUTE_FRAMES
option is used: it avoids visiting frames that will be ignored and recomputed from scratch in the class writer.- See Also:
-
EXPAND_FRAMES
public static final int EXPAND_FRAMESFlag to expand the stack map frames. By default stack map frames are visited in their original format (i.e. "expanded" for classes whose version is less than V1_6, and "compressed" for the other classes). If this flag is set, stack map frames are always visited in expanded format (this option adds a decompression/recompression step in ClassReader and ClassWriter which degrades performances quite a lot).- See Also:
-
b
public final byte[] bThe class to be parsed. The content of this array must not be modified. This field is intended forAttribute
sub classes, and is normally not needed by class generators or adapters. -
items
private final int[] itemsThe start index of each constant pool item inb
, plus one. The one byte offset skips the constant pool item tag that indicates its type. -
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 final int maxStringLengthMaximum length of the strings contained in the constant pool of the class. -
header
public final int headerStart index of the class header information (access, name...) inb
.
-
-
Constructor Details
-
ClassReader
public ClassReader(byte[] b) Constructs a newClassReader
object.- Parameters:
b
- the bytecode of the class to be read.
-
ClassReader
public ClassReader(byte[] b, int off, int len) Constructs a newClassReader
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
Constructs a newClassReader
object.- Parameters:
is
- an input stream from which to read the class.- Throws:
IOException
- if a problem occurs during reading.
-
ClassReader
Constructs a newClassReader
object.- Parameters:
name
- the binary qualified name of the class to be read.- Throws:
IOException
- if an exception occurs during reading.
-
-
Method Details
-
getAccess
public int getAccess()Returns the class's access flags (seeOpcodes
). 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
Returns the internal name of the class (seegetInternalName
).- Returns:
- the internal class name
- See Also:
-
getSuperName
Returns the internal of name of the super class (seegetInternalName
). For interfaces, the super class isObject
.- Returns:
- the internal name of super class, or null for
Object
class. - See Also:
-
getInterfaces
Returns the internal names of the class's interfaces (seegetInternalName
).- Returns:
- the array of internal names for all implemented interfaces or null.
- See Also:
-
copyPool
Copies the constant pool data into the givenClassWriter
. Should be called before theaccept(ClassVisitor,int)
method.- Parameters:
classWriter
- theClassWriter
to copy constant pool into.
-
copyBootstrapMethods
Copies the bootstrap method data into the givenClassWriter
. Should be called before theaccept(ClassVisitor,int)
method.- Parameters:
classWriter
- theClassWriter
to copy bootstrap methods into.
-
readClass
Reads the bytecode of a class.- Parameters:
is
- an input stream from which to read the class.close
- true to close the input stream after reading.- Returns:
- the bytecode read from the given input stream.
- Throws:
IOException
- if a problem occurs during reading.
-
accept
Makes the given visitor visit the Java class of thisClassReader
. This class is the one specified in the constructor (seeClassReader
).- Parameters:
classVisitor
- the visitor that must visit this class.flags
- option flags that can be used to modify the default behavior of this class. SeeSKIP_DEBUG
,EXPAND_FRAMES
,SKIP_FRAMES
,SKIP_CODE
.
-
accept
Makes the given visitor visit the Java class of thisClassReader
. This class is the one specified in the constructor (seeClassReader
).- 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 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. SeeSKIP_DEBUG
,EXPAND_FRAMES
,SKIP_FRAMES
,SKIP_CODE
.
-
readField
Reads a field and makes the given visitor visit it.- Parameters:
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.- Returns:
- the offset of the first byte following the field in the class.
-
readMethod
Reads a method and makes the given visitor visit it.- Parameters:
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.- Returns:
- the offset of the first byte following the method in the class.
-
readCode
Reads the bytecode of a method and makes the given visitor visit it.- Parameters:
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.
-
readTypeAnnotations
Parses a type annotation table to find the labels, and to visit the try catch block annotations.- Parameters:
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.- Returns:
- the start offset of each type annotation in the parsed table.
-
readAnnotationTarget
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. the offset to the type_index field, which is followed by num_element_value_pairs and then the name,value pairs).- Parameters:
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.- Returns:
- the start offset of the rest of the type_annotation structure.
-
readParameterAnnotations
Reads parameter annotations and makes the given visitor visit them.- Parameters:
mv
- the visitor that must visit the annotations.context
- information about the class being parsed.v
- start offset inb
of the annotations to be read.visible
- true if the annotations to be read are visible at runtime.
-
readAnnotationValues
Reads the values of an annotation and makes the given visitor visit them.- Parameters:
v
- the start offset inb
of the values to be read (including the unsigned short that gives the number of values).buf
- buffer to be used to callreadUTF8
,readClass
orreadConst
.named
- if the annotation values are named or not.av
- the visitor that must visit the values.- Returns:
- the end offset of the annotation values.
-
readAnnotationValue
Reads a value of an annotation and makes the given visitor visit it.- Parameters:
v
- the start offset inb
of the value to be read (not including the value name constant pool index).buf
- buffer to be used to callreadUTF8
,readClass
orreadConst
.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.
-
getImplicitFrame
Computes the implicit frame of the method currently being parsed (as defined in the givenContext
) and stores it in the given context.- Parameters:
frame
- information about the class being parsed.
-
readFrame
Reads a stack map frame and stores the result in the givenContext
object.- Parameters:
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.- Returns:
- the offset of the first byte following the parsed frame.
-
readFrameType
Reads a stack map frame type and stores it at the given index in the given array.- Parameters:
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.- Returns:
- the offset of the first byte after the parsed type.
-
readLabel
Returns the label corresponding to the given offset. The default implementation of this method creates a label for the given offset if it has not been already created.- Parameters:
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.- Returns:
- a non null Label, which must be equal to labels[offset].
-
getAttributes
private int getAttributes()Returns the start index of the attribute_info structure of this class.- Returns:
- the start index of the attribute_info structure of this class.
-
readAttribute
private Attribute readAttribute(Attribute[] attrs, String type, int off, int len, char[] buf, int codeOff, Label[] labels) Reads an attribute inb
.- 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 emptyAttribute
instance is returned).type
- the type of the attribute.off
- index of the first byte of the attribute's content inb
. 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 callreadUTF8
,readClass
orreadConst
.codeOff
- index of the first byte of code's attribute content inb
, 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.
-
getItemCount
public int getItemCount()Returns the number of constant pool items inb
.- Returns:
- the number of constant pool items in
b
.
-
getItem
public int getItem(int item) Returns the start index of the constant pool item inb
, plus one. This method is intended forAttribute
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.
-
getMaxStringLength
public int getMaxStringLength()Returns the maximum length of the strings contained in the constant pool of the class.- Returns:
- the maximum length of the strings contained in the constant pool of the class.
-
readByte
public int readByte(int index) Reads a byte value inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of the value to be read inb
.- Returns:
- the read value.
-
readUnsignedShort
public int readUnsignedShort(int index) Reads an unsigned short value inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of the value to be read inb
.- Returns:
- the read value.
-
readShort
public short readShort(int index) Reads a signed short value inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of the value to be read inb
.- Returns:
- the read value.
-
readInt
public int readInt(int index) Reads a signed int value inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of the value to be read inb
.- Returns:
- the read value.
-
readLong
public long readLong(int index) Reads a signed long value inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of the value to be read inb
.- Returns:
- the read value.
-
readUTF8
Reads an UTF8 string constant pool item inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of an unsigned short value inb
, 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
Reads UTF8 string inb
.- 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
Reads a class constant pool item inb
. This method is intended forAttribute
sub classes, and is normally not needed by class generators or adapters.- Parameters:
index
- the start index of an unsigned short value inb
, 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
-