Top Description Fields Constructors Methods
net.bytebuddy.jar.asm

public Class ClassReader

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .IOException, .InputStream

A parser to make a ClassVisitor visit a ClassFile structure, as defined in the Java Virtual Machine Specification (JVMS). This class parses the ClassFile content and calls the appropriate visit methods of a given ClassVisitor for each field, method and bytecode instruction encountered.
Authors
Eric Bruneton, Eugene Kuleshov
See Also
JVMS 4

Field Summary

Modifier and TypeField and Description
public final byte[]
b

A byte array containing the JVMS ClassFile structure to be parsed.

private final int[]
bootstrapMethodOffsets

The start offsets in classFileBuffer of each element of the bootstrap_methods array (in the BootstrapMethods attribute).

pack-priv final byte[]
classFileBuffer

A byte array containing the JVMS ClassFile structure to be parsed.

private final ConstantDynamic[]
constantDynamicValues

The ConstantDynamic objects corresponding to the CONSTANT_Dynamic constant pool items.

private final String[]
constantUtf8Values

The String objects corresponding to the CONSTANT_Utf8 constant pool items.

private final int[]
cpInfoOffsets

The offset in bytes, in classFileBuffer, of each cp_info entry of the ClassFile's constant_pool array, plus one.

pack-priv static final int
EXPAND_ASM_INSNS

A flag to expand the ASM specific instructions into an equivalent sequence of standard bytecode instructions.

public static final int
EXPAND_FRAMES

A flag to expand the stack map frames.

public final int
header

The offset in bytes of the ClassFile's access_flags field.

private static final int
INPUT_STREAM_DATA_CHUNK_SIZE

The size of the temporary byte array used to read class input streams chunk by chunk.

private static final int
MAX_BUFFER_SIZE

The maximum size of array to allocate.

private final int
maxStringLength

A conservative estimate of the maximum length of the strings contained in the constant pool of the class.

public static final int
SKIP_CODE

A flag to skip the Code attributes.

public static final int
SKIP_DEBUG

A flag to skip the SourceFile, SourceDebugExtension, LocalVariableTable, LocalVariableTypeTable, LineNumberTable and MethodParameters attributes.

public static final int
SKIP_FRAMES

A flag to skip the StackMap and StackMapTable attributes.

Constructor Summary

AccessConstructor and Description
public
ClassReader(final byte[]
the JVMS ClassFile structure to be read.
classFile
)

Constructs a new ClassReader object.

public
ClassReader(final byte[]
a byte array containing the JVMS ClassFile structure to be read.
classFileBuffer
,
final int
the offset in byteBuffer of the first byte of the ClassFile to be read.
classFileOffset
,
final int
the length in bytes of the ClassFile to be read.
classFileLength
)

Constructs a new ClassReader object.

pack-priv
ClassReader(final byte[]
a byte array containing the JVMS ClassFile structure to be read.
classFileBuffer
,
final int
the offset in byteBuffer of the first byte of the ClassFile to be read.
classFileOffset
,
final boolean
whether to check the class version or not.
checkClassVersion
)

Constructs a new ClassReader object.

public
ClassReader(final InputStream
an input stream of the JVMS ClassFile structure to be read. This input stream must contain nothing more than the ClassFile structure itself. It is read from its current position to its end.
inputStream
)

Constructs a new ClassReader object.

public
ClassReader(final String
the fully qualified name of the class to be read. The ClassFile structure is retrieved with the current class loader's ClassLoader#getSystemResourceAsStream.
className
)

Constructs a new ClassReader object.

Method Summary

Modifier and TypeMethod and Description
public void
accept(final ClassVisitor
the visitor that must visit this class.
classVisitor
,
final int
the options to use to parse this class. One or more of SKIP_CODE, SKIP_DEBUG, SKIP_FRAMES or EXPAND_FRAMES.
parsingOptions
)

Makes the given visitor visit the JVMS ClassFile structure passed to the constructor of this ClassReader.

public void
accept(final ClassVisitor
the visitor that must visit this class.
classVisitor
,
final Attribute[]
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.
attributePrototypes
,
final int
the options to use to parse this class. One or more of SKIP_CODE, SKIP_DEBUG, SKIP_FRAMES or EXPAND_FRAMES.
parsingOptions
)

Makes the given visitor visit the JVMS ClassFile structure passed to the constructor of this ClassReader.

private static int
computeBufferSize(final InputStream inputStream)

private void
computeImplicitFrame(final Context
information about the class being parsed.
context
)

Computes the implicit frame of the method currently being parsed (as defined in the given Context) and stores it in the given context.

private void
createDebugLabel(final int
a bytecode offset in a method.
bytecodeOffset
,
final Label[]
the already created labels, indexed by their offset.
labels
)

Creates a label with the Label#FLAG_DEBUG_ONLY flag set, if there is no already existing label for the given bytecode offset (otherwise does nothing).

private Label

Returns:

a Label without the Label#FLAG_DEBUG_ONLY flag set.
createLabel
(final int
a bytecode offset in a method.
bytecodeOffset
,
final Label[]
the already created labels, indexed by their offset.
labels
)

Creates a label without the Label#FLAG_DEBUG_ONLY flag set, for the given bytecode offset.

public int

Returns:

the class access flags.
getAccess
()

Returns the class's access flags (see Opcodes).

public String

Returns:

the internal class name.
getClassName
()

Returns the internal name of the class (see Type#getInternalName()).

pack-priv final int

Returns:

the offset in classFileBuffer of the first ClassFile's 'attributes' array field entry.
getFirstAttributeOffset
()

Returns the offset in classFileBuffer of the first ClassFile's 'attributes' array field entry.

public String[]

Returns:

the internal names of the directly implemented interfaces. Inherited implemented interfaces are not returned.
getInterfaces
()

Returns the internal names of the implemented interfaces (see Type#getInternalName()).

public int

Returns:

the start offset in this ClassReader of the corresponding JVMS 'cp_info' structure, plus one.
getItem
(final int
the index a constant pool entry in the class's constant pool table.
constantPoolEntryIndex
)

Returns the start offset in this ClassReader of a JVMS 'cp_info' structure (i.e. a constant pool entry), plus one.

public int

Returns:

the number of entries in the class's constant pool table.
getItemCount
()

Returns the number of entries in the class's constant pool table.

public int

Returns:

a conservative estimate of the maximum length of the strings contained in the class's constant pool table.
getMaxStringLength
()

Returns a conservative estimate of the maximum length of the strings contained in the class's constant pool table.

public String

Returns:

the internal name of the super class, or null for Object class.
getSuperName
()

Returns the internal name of the super class (see Type#getInternalName()).

private int

Returns:

bytecode offset corresponding to the specified JVMS 'type_annotation' structure, or -1 if there is no such type_annotation of if it does not have a bytecode offset.
getTypeAnnotationBytecodeOffset
(final int[]
the offset of each 'type_annotation' entry in a Runtime[In]VisibleTypeAnnotations attribute, or null.
typeAnnotationOffsets
,
final int
the index a 'type_annotation' entry in typeAnnotationOffsets.
typeAnnotationIndex
)

Returns the bytecode offset corresponding to the specified JVMS 'type_annotation' structure, or -1 if there is no such type_annotation of if it does not have a bytecode offset.

private Attribute

Returns:

the attribute that has been read.
readAttribute
(final Attribute[]
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.
attributePrototypes
,
final String
the type of the attribute.
type
,
final int
the start offset of the JVMS 'attribute' structure in classFileBuffer. The 6 attribute header bytes (attribute_name_index and attribute_length) are not taken into account here.
offset
,
final int
the length of the attribute's content (excluding the 6 attribute header bytes).
length
,
final char[]
the buffer to be used to read strings in the constant pool.
charBuffer
,
final int
the start offset of the enclosing Code attribute in classFileBuffer, or -1 if the attribute to be read is not a code attribute. The 6 attribute header bytes (attribute_name_index and attribute_length) are not taken into account here.
codeAttributeOffset
,
final Label[]
the labels of the method's code, or null if the attribute to be read is not a code attribute.
labels
)

Reads a non standard JVMS 'attribute' structure in classFileBuffer.

private int[]

Returns:

the offsets of the bootstrap methods.
readBootstrapMethodsAttribute
(final int
a conservative estimate of the maximum length of the strings contained in the constant pool of the class.
maxStringLength
)

Reads the BootstrapMethods attribute to compute the offset of each bootstrap method.

public int

Returns:

the read value.
readByte
(final int
the start offset of the value to be read in this ClassReader.
offset
)

Reads a byte value in this ClassReader.

protected void
readBytecodeInstructionOffset(final int
the bytecode offset of the next instruction to be visited.
bytecodeOffset
)

Handles the bytecode offset of the next instruction to be visited in accept(ClassVisitor, int).

public String

Returns:

the String corresponding to the specified CONSTANT_Class entry.
readClass
(final int
the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Class entry in class's constant pool table.
offset
,
final char[]
the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Class constant pool entry in this ClassReader.

private void
readCode(final MethodVisitor
the visitor that must visit the Code attribute.
methodVisitor
,
final Context
information about the class being parsed.
context
,
final int
the start offset in classFileBuffer of the Code attribute, excluding its attribute_name_index and attribute_length fields.
codeOffset
)

Reads a JVMS 'Code' attribute and makes the given visitor visit it.

public Object

Returns:

the Integer, Float, Long, Double, String, Type, Handle or ConstantDynamic corresponding to the specified constant pool entry.
readConst
(final int
the index of a CONSTANT_Integer, CONSTANT_Float, CONSTANT_Long, CONSTANT_Double, CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_MethodHandle or CONSTANT_Dynamic entry in the class's constant pool.
constantPoolEntryIndex
,
final char[]
the buffer to be used to read strings. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a numeric or string constant pool entry in this ClassReader.

private ConstantDynamic

Returns:

the ConstantDynamic corresponding to the specified CONSTANT_Dynamic entry.
readConstantDynamic
(final int
the index of a CONSTANT_Dynamic entry in the class's constant pool table.
constantPoolEntryIndex
,
final char[]
the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Dynamic constant pool entry in classFileBuffer.

private int

Returns:

the end offset of the JVMS 'element_value' structure.
readElementValue
(final AnnotationVisitor
the visitor that must visit the element_value structure.
annotationVisitor
,
final int
the start offset in classFileBuffer of the element_value structure to be read.
elementValueOffset
,
final String
the name of the element_value structure to be read, or null.
elementName
,
final char[]
the buffer used to read strings in the constant pool.
charBuffer
)

Reads a JVMS 'element_value' structure and makes the given visitor visit it.

private int

Returns:

the end offset of the JVMS 'annotation' or 'array_value' structure.
readElementValues
(final AnnotationVisitor
the visitor that must visit the values.
annotationVisitor
,
final int
the start offset of an 'annotation' structure (excluding its type_index field) or of an 'array_value' structure.
annotationOffset
,
final boolean
if the annotation values are named or not. This should be true to parse the values of a JVMS 'annotation' structure, and false to parse the JVMS 'array_value' of an annotation's element_value.
named
,
final char[]
the buffer used to read strings in the constant pool.
charBuffer
)

Reads the element values of a JVMS 'annotation' structure and makes the given visitor visit them.

private int

Returns:

the offset of the first byte following the field_info structure.
readField
(final ClassVisitor
the visitor that must visit the field.
classVisitor
,
final Context
information about the class being parsed.
context
,
final int
the start offset of the field_info structure.
fieldInfoOffset
)

Reads a JVMS field_info structure and makes the given visitor visit it.

public int

Returns:

the read value.
readInt
(final int
the start offset of the value to be read in this ClassReader.
offset
)

Reads a signed int value in this ClassReader.

protected Label

Returns:

a non null Label, which must be equal to labels[bytecodeOffset].
readLabel
(final int
a bytecode offset in a method.
bytecodeOffset
,
final Label[]
the already created labels, indexed by their offset. If a label already exists for bytecodeOffset this method must not create a new one. Otherwise it must store the new label in this array.
labels
)

Returns the label corresponding to the given bytecode offset.

public long

Returns:

the read value.
readLong
(final int
the start offset of the value to be read in this ClassReader.
offset
)

Reads a signed long value in this ClassReader.

private int

Returns:

the offset of the first byte following the method_info structure.
readMethod
(final ClassVisitor
the visitor that must visit the method.
classVisitor
,
final Context
information about the class being parsed.
context
,
final int
the start offset of the method_info structure.
methodInfoOffset
)

Reads a JVMS method_info structure and makes the given visitor visit it.

public String

Returns:

the String corresponding to the specified CONSTANT_Module entry.
readModule
(final int
the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Module entry in class's constant pool table.
offset
,
final char[]
the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Module constant pool entry in this ClassReader.

private void
readModuleAttributes(final ClassVisitor
the current class visitor
classVisitor
,
final Context
information about the class being parsed.
context
,
final int
the offset of the Module attribute (excluding the attribute_info's attribute_name_index and attribute_length fields).
moduleOffset
,
final int
the offset of the ModulePackages attribute (excluding the attribute_info's attribute_name_index and attribute_length fields), or 0.
modulePackagesOffset
,
final String
the string corresponding to the ModuleMainClass attribute, or null.
moduleMainClass
)

Reads the Module, ModulePackages and ModuleMainClass attributes and visit them.

public String

Returns:

the String corresponding to the specified CONSTANT_Package entry.
readPackage
(final int
the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Package entry in class's constant pool table.
offset
,
final char[]
the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Package constant pool entry in this ClassReader.

private void
readParameterAnnotations(final MethodVisitor
the visitor that must visit the parameter annotations.
methodVisitor
,
final Context
information about the class being parsed.
context
,
final int
the start offset of a Runtime[In]VisibleParameterAnnotations attribute, excluding the attribute_info's attribute_name_index and attribute_length fields.
runtimeParameterAnnotationsOffset
,
final boolean
true if the attribute to parse is a RuntimeVisibleParameterAnnotations attribute, false it is a RuntimeInvisibleParameterAnnotations attribute.
visible
)

Reads a Runtime[In]VisibleParameterAnnotations attribute and makes the given visitor visit it.

private int

Returns:

the offset of the first byte following the record component.
readRecordComponent
(final ClassVisitor
the current class visitor
classVisitor
,
final Context
information about the class being parsed.
context
,
final int
the offset of the current record component.
recordComponentOffset
)

Reads a record component and visit it.

public short

Returns:

the read value.
readShort
(final int
the start offset of the value to be read in this ClassReader.
offset
)

Reads a signed short value in this ClassReader.

private int

Returns:

the end offset of the JVMS 'stack_map_frame' or 'full_frame' structure.
readStackMapFrame
(final int
the start offset in classFileBuffer of the stack_map_frame_value structure to be read, or the start offset of a full_frame structure (excluding its frame_type field).
stackMapFrameOffset
,
final boolean
true to read a 'stack_map_frame' structure, false to read a 'full_frame' structure without its frame_type field.
compressed
,
final boolean
if the stack map frame must be expanded. See EXPAND_FRAMES.
expand
,
final Context
where the parsed stack map frame must be stored.
context
)

Reads a JVMS 'stack_map_frame' structure and stores the result in the given Context object.

private static byte[]

Returns:

the content of the given input stream.
readStream
(final InputStream
an input stream.
inputStream
,
final boolean
true to close the input stream after reading.
close
)

Reads the given input stream and returns its content as a byte array.

private String

Returns:

the String corresponding to the specified constant pool entry.
readStringish
(final int
the start offset of an unsigned short value in classFileBuffer, whose value is the index of a CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_Module or CONSTANT_Package entry in class's constant pool table.
offset
,
final char[]
the buffer to be used to read the item. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Class, CONSTANT_String, CONSTANT_MethodType, CONSTANT_Module or CONSTANT_Package constant pool entry in classFileBuffer.

private int[]

Returns:

the start offset of each entry of the Runtime[In]VisibleTypeAnnotations_attribute's 'annotations' array field.
readTypeAnnotations
(final MethodVisitor
the method visitor to be used to visit the try catch block annotations.
methodVisitor
,
final Context
information about the class being parsed.
context
,
final int
the start offset of a Runtime[In]VisibleTypeAnnotations attribute, excluding the attribute_info's attribute_name_index and attribute_length fields.
runtimeTypeAnnotationsOffset
,
final boolean
true if the attribute to parse is a RuntimeVisibleTypeAnnotations attribute, false it is a RuntimeInvisibleTypeAnnotations attribute.
visible
)

Parses a Runtime[In]VisibleTypeAnnotations attribute to find the offset of each type_annotation entry it contains, to find the corresponding labels, and to visit the try catch block annotations.

private int

Returns:

the start offset of the rest of the type_annotation structure.
readTypeAnnotationTarget
(final Context
information about the class being parsed. This is where the extracted target_type and target_path must be stored.
context
,
final int
the start offset of a type_annotation structure.
typeAnnotationOffset
)

Parses the header of a JVMS type_annotation structure to extract its target_type, target_info and target_path (the result is stored in the given context), and returns the start offset of the rest of the type_annotation structure.

public int

Returns:

the read value.
readUnsignedShort
(final int
the start index of the value to be read in this ClassReader.
offset
)

Reads an unsigned short value in this ClassReader.

pack-priv final String

Returns:

the String corresponding to the specified CONSTANT_Utf8 entry.
readUtf
(final int
the index of a CONSTANT_Utf8 entry in the class's constant pool table.
constantPoolEntryIndex
,
final char[]
the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Utf8 constant pool entry in classFileBuffer.

private String

Returns:

the String corresponding to the specified UTF8 string.
readUtf
(final int
the start offset of the UTF8 string to be read.
utfOffset
,
final int
the length of the UTF8 string to be read.
utfLength
,
final char[]
the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads an UTF8 string in classFileBuffer.

public String

Returns:

the String corresponding to the specified CONSTANT_Utf8 entry.
readUTF8
(final int
the start offset of an unsigned short value in this ClassReader, whose value is the index of a CONSTANT_Utf8 entry in the class's constant pool table.
offset
,
final char[]
the buffer to be used to read the string. This buffer must be sufficiently large. It is not automatically resized.
charBuffer
)

Reads a CONSTANT_Utf8 constant pool entry in this ClassReader.

private int

Returns:

the end offset of the JVMS 'verification_type_info' structure.
readVerificationTypeInfo
(final int
the start offset of the 'verification_type_info' structure to read.
verificationTypeInfoOffset
,
final Object[]
the array where the parsed type must be stored.
frame
,
final int
the index in 'frame' where the parsed type must be stored.
index
,
final char[]
the buffer used to read strings in the constant pool.
charBuffer
,
final Label[]
the labels of the method currently being parsed, indexed by their offset. If the parsed type is an ITEM_Uninitialized, a new label for the corresponding NEW instruction is stored in this array if it does not already exist.
labels
)

Reads a JVMS 'verification_type_info' structure and stores it at the given index in the given array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait