Package com.thoughtworks.paranamer
Class BytecodeReadingParanamer.ClassReader
java.lang.Object
com.thoughtworks.paranamer.BytecodeReadingParanamer.ClassReader
- Enclosing class:
BytecodeReadingParanamer
A Java class parser to make a Class Visitor 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 TypeFieldDescriptionfinal byte[]
The class to be parsed.(package private) static final int
The type of CONSTANT_Double constant pool items.(package private) static final int
The type of CONSTANT_Fieldref constant pool items.(package private) static final int
The type of CONSTANT_Float constant pool items.final int
Start index of the class header information (access, name...) inb
.(package private) static final int
The type of CONSTANT_InterfaceMethodref constant pool items.(package private) static final int
The type of CONSTANT_Integer constant pool items.(package private) static final int
The type of CONSTANT_InvokeDynamic constant pool items.private final int[]
The start index of each constant pool item inb
, plus one.(package private) static final int
The type of CONSTANT_Long constant pool items.private final int
Maximum length of the strings contained in the constant pool of the class.(package private) static final int
The type of CONSTANT_Methodref constant pool items.(package private) static final int
The type of CONSTANT_MethodHandle constant pool items.(package private) static final int
The type of CONSTANT_NameAndType constant pool items.private final String[]
The String objects corresponding to the CONSTANT_Utf8 items.(package private) static final int
The type of CONSTANT_Utf8 constant pool items. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
ClassReader
(byte[] b) Constructs a newBytecodeReadingParanamer.ClassReader
object.private
ClassReader
(byte[] b, int off) Constructs a newBytecodeReadingParanamer.ClassReader
object.private
Constructs a newBytecodeReadingParanamer.ClassReader
object. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
accept
(BytecodeReadingParanamer.TypeCollector classVisitor) Makes the given visitor visit the Java class of thisBytecodeReadingParanamer.ClassReader
.private static byte[]
readClass
(InputStream is) Reads the bytecode of a class.private int
readInt
(int index) Reads a signed int value inb
.private int
readMethod
(BytecodeReadingParanamer.TypeCollector classVisitor, char[] c, int u) private int
readUnsignedShort
(int index) Reads an unsigned short value inb
.private String
readUTF
(int index, int utfLen, char[] buf) Reads UTF8 string inb
.private String
readUTF8
(int index, char[] buf) Reads an UTF8 string constant pool item inb
.
-
Field Details
-
b
public final byte[] bThe 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 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
. -
FIELD
static final int FIELDThe type of CONSTANT_Fieldref constant pool items.- See Also:
-
METH
static final int METHThe type of CONSTANT_Methodref constant pool items.- See Also:
-
IMETH
static final int IMETHThe type of CONSTANT_InterfaceMethodref constant pool items.- See Also:
-
INT
static final int INTThe type of CONSTANT_Integer constant pool items.- See Also:
-
FLOAT
static final int FLOATThe type of CONSTANT_Float constant pool items.- See Also:
-
LONG
static final int LONGThe type of CONSTANT_Long constant pool items.- See Also:
-
DOUBLE
static final int DOUBLEThe type of CONSTANT_Double constant pool items.- See Also:
-
NAME_TYPE
static final int NAME_TYPEThe type of CONSTANT_NameAndType constant pool items.- See Also:
-
MHANDLE
static final int MHANDLEThe type of CONSTANT_MethodHandle constant pool items.- See Also:
-
INVOKEDYN
static final int INVOKEDYNThe type of CONSTANT_InvokeDynamic constant pool items.- See Also:
-
UTF8
static final int UTF8The type of CONSTANT_Utf8 constant pool items.- See Also:
-
-
Constructor Details
-
ClassReader
private ClassReader(byte[] b) Constructs a newBytecodeReadingParanamer.ClassReader
object.- Parameters:
b
- the bytecode of the class to be read.
-
ClassReader
private ClassReader(byte[] b, int off) Constructs a newBytecodeReadingParanamer.ClassReader
object.- Parameters:
b
- the bytecode of the class to be read.off
- the start offset of the class data.
-
ClassReader
Constructs a newBytecodeReadingParanamer.ClassReader
object.- Parameters:
is
- an input stream from which to read the class.- Throws:
IOException
- if a problem occurs during reading.
-
-
Method Details
-
readClass
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:
IOException
- if a problem occurs during reading.
-
accept
Makes the given visitor visit the Java class of thisBytecodeReadingParanamer.ClassReader
. This class is the one specified in the constructor (seeClassReader
).- Parameters:
classVisitor
- the visitor that must visit this class.
-
readMethod
-
readUnsignedShort
private int readUnsignedShort(int index) Reads an unsigned short value inb
. 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 inb
.- Returns:
- the read value.
-
readInt
private int readInt(int index) Reads a signed int value inb
. 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 inb
.- Returns:
- the read value.
-
readUTF8
Reads an UTF8 string constant pool item inb
. 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 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.
-