APIdia Launch Partner
Top Description Methods
java.lang.classfile

public sealed Interface ClassReader

extends ConstantPool
permits ClassReaderImpl
Known Direct Implementers
jdk.internal.classfile.impl.ClassReaderImpl
Imports
java.lang.classfile.constantpool.ClassEntry, .ConstantPool, .ConstantPoolException, .PoolEntry, .Utf8Entry, java.util.Optional, java.util.function.Function, jdk.internal.classfile.impl.ClassReaderImpl

Advanced class file reading support for AttributeMappers. Supports reading arbitrary offsets within a class file and reading data of various numeric types (e.g., u2, u4) in addition to constant pool access.

All numeric values in the class file format are big endian.

Unless otherwise specified, all out-of-bounds access result in an IllegalArgumentException to indicate the class file data is malformed. Since the class file data is arbitrary, users should sanity-check the structural integrity of the data before attempting to interpret the potentially malformed data.

Since
24
See Also
AttributeMapper#readAttribute(AttributedElement, ClassReader, int)

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the length of the class file, in number of bytes
classfileLength
()

Returns the length of the class file, in number of bytes.

public void
copyBytesTo(BufWriter buf, int
the offset within the class file
offset
,
int
the length of the range
len
)

Copy a range of bytes from the class file to a BufWriter.

public Function<Utf8Entry, AttributeMapper<?>>

Returns:

the table of custom attribute mappers
customAttributes
()

Returns the table of custom attribute mappers.

public int

Returns:

the access flags for the class, as a bit mask
flags
()

Returns the access flags for the class, as a bit mask.

public byte[]

Returns:

a copy of the bytes at the specified range in the class file
readBytes
(int
the offset within the class file
offset
,
int
the length of the range
len
)

Returns a copy of the bytes at the specified range in the class file.

public double

Returns:

the double value at the specified offset within the class file
readDouble
(int
the offset within the class file
offset
)

Returns the double value at the specified offset within the class file.

public PoolEntry

Returns:

the constant pool entry whose index is given at the specified offset within the class file
readEntry
(int
the offset of the index within the class file
offset
)

Returns the constant pool entry whose index is given at the specified offset within the class file.

public <
the entry type
T extends PoolEntry
>
T

Returns:

the constant pool entry of a given type whose index is given at the specified offset within the class file
readEntry
(int
the offset of the index within the class file
offset
,
Class<T>
the entry type
cls
)

Returns the constant pool entry of a given type whose index is given at the specified offset within the class file.

public PoolEntry

Returns:

the constant pool entry whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero
readEntryOrNull
(int
the offset of the index within the class file
offset
)

Returns the constant pool entry whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero.

public <
the entry type
T extends PoolEntry
>
T

Returns:

the constant pool entry of a given type whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero
readEntryOrNull
(int
the offset of the index within the class file
offset
,
Class<T>
the entry type
cls
)

Returns the constant pool entry of a given type whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero.

public float

Returns:

the float value at the specified offset within the class file
readFloat
(int
the offset within the class file
offset
)

Returns the float value at the specified offset within the class file.

public int

Returns:

the signed int at the specified offset within the class file
readInt
(int
the offset within the class file
offset
)

Returns the signed int at the specified offset within the class file.

public long

Returns:

the signed long at the specified offset within the class file
readLong
(int
the offset within the class file
offset
)

Returns the signed long at the specified offset within the class file.

public int

Returns:

the signed byte at the specified offset within the class file
readS1
(int
the offset within the class file
offset
)

Returns the signed byte at the specified offset within the class file.

public int

Returns:

the signed byte at the specified offset within the class file
readS2
(int
the offset within the class file
offset
)

Returns the signed byte at the specified offset within the class file.

public int

Returns:

the unsigned byte at the specified offset within the class file
readU1
(int
the offset within the class file
offset
)

Returns the unsigned byte at the specified offset within the class file.

public int

Returns:

the u2 at the specified offset within the class file
readU2
(int
the offset within the class file
offset
)

Returns the u2 at the specified offset within the class file.

public Optional<ClassEntry>

Returns:

the constant pool entry describing the name of the superclass, if any
superclassEntry
()

Returns the constant pool entry describing the name of the superclass, if any.

public ClassEntry

Returns:

the constant pool entry describing the name of class
thisClassEntry
()

Returns the constant pool entry describing the name of class.

Inherited from java.lang.classfile.constantpool.ConstantPool:
bootstrapMethodCountbootstrapMethodEntryentryByIndexentryByIndexiteratorsize

Method Detail

classfileLengthback to summary
public int classfileLength()

Returns the length of the class file, in number of bytes.

Returns:int

the length of the class file, in number of bytes

copyBytesToback to summary
public void copyBytesTo(BufWriter buf, int offset, int len)

Copy a range of bytes from the class file to a BufWriter.

Parameters
buf:BufWriter

the BufWriter

offset:int

the offset within the class file

len:int

the length of the range

customAttributesback to summary
public Function<Utf8Entry, AttributeMapper<?>> customAttributes()

Returns the table of custom attribute mappers. This is derived from the processing option ClassFile.AttributeMapperOption.

Returns:Function<Utf8Entry, AttributeMapper<?>>

the table of custom attribute mappers

flagsback to summary
public int flags()

Returns the access flags for the class, as a bit mask.

Returns:int

the access flags for the class, as a bit mask

See Also
ClassModel#flags()
readBytesback to summary
public byte[] readBytes(int offset, int len)

Returns a copy of the bytes at the specified range in the class file.

Parameters
offset:int

the offset within the class file

len:int

the length of the range

Returns:byte[]

a copy of the bytes at the specified range in the class file

readDoubleback to summary
public double readDouble(int offset)

Returns the double value at the specified offset within the class file. Reads 8 bytes of value.

In the conversions, all NaN values of the double may or may not be collapsed into a single "canonical" NaN value.

Parameters
offset:int

the offset within the class file

Returns:double

the double value at the specified offset within the class file

readEntryback to summary
public PoolEntry readEntry(int offset)

Returns the constant pool entry whose index is given at the specified offset within the class file.

API Note

If only a particular type of entry is expected, use readEntry(int, Class).

Parameters
offset:int

the offset of the index within the class file

Returns:PoolEntry

the constant pool entry whose index is given at the specified offset within the class file

Exceptions
ConstantPoolException:
if the index is out of range of the constant pool size, or zero
readEntryback to summary
public <T extends PoolEntry> T readEntry(int offset, Class<T> cls)

Returns the constant pool entry of a given type whose index is given at the specified offset within the class file.

Parameters
<T>
the entry type
offset:int

the offset of the index within the class file

cls:Class<T>

the entry type

Returns:T

the constant pool entry of a given type whose index is given at the specified offset within the class file

Exceptions
ConstantPoolException:
if the index is out of range of the constant pool size, or zero, or the entry is not of the given type
readEntryOrNullback to summary
public PoolEntry readEntryOrNull(int offset)

Returns the constant pool entry whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero.

API Note

If only a particular type of entry is expected, use readEntryOrNull(int, Class).

Parameters
offset:int

the offset of the index within the class file

Returns:PoolEntry

the constant pool entry whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero

Exceptions
ConstantPoolException:
if the index is out of range of the constant pool size
readEntryOrNullback to summary
public <T extends PoolEntry> T readEntryOrNull(int offset, Class<T> cls)

Returns the constant pool entry of a given type whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero.

Parameters
<T>
the entry type
offset:int

the offset of the index within the class file

cls:Class<T>

the entry type

Returns:T

the constant pool entry of a given type whose index is given at the specified offset within the class file, or null if the index at the specified offset is zero

Exceptions
ConstantPoolException:
if the index is out of range of the constant pool size, or zero, or the entry is not of the given type
readFloatback to summary
public float readFloat(int offset)

Returns the float value at the specified offset within the class file. Reads 4 bytes of value.

In the conversions, all NaN values of the float may or may not be collapsed into a single "canonical" NaN value.

Parameters
offset:int

the offset within the class file

Returns:float

the float value at the specified offset within the class file

readIntback to summary
public int readInt(int offset)

Returns the signed int at the specified offset within the class file. Reads 4 bytes of value.

Parameters
offset:int

the offset within the class file

Returns:int

the signed int at the specified offset within the class file

readLongback to summary
public long readLong(int offset)

Returns the signed long at the specified offset within the class file. Reads 8 bytes of value.

Parameters
offset:int

the offset within the class file

Returns:long

the signed long at the specified offset within the class file

readS1back to summary
public int readS1(int offset)

Returns the signed byte at the specified offset within the class file. Reads a byte and sign-extends it to an int.

Parameters
offset:int

the offset within the class file

Returns:int

the signed byte at the specified offset within the class file

readS2back to summary
public int readS2(int offset)

Returns the signed byte at the specified offset within the class file. Reads a 2-byte value and sign-extends it to an int.

Parameters
offset:int

the offset within the class file

Returns:int

the signed byte at the specified offset within the class file

readU1back to summary
public int readU1(int offset)

Returns the unsigned byte at the specified offset within the class file. Reads a byte and zero-extends it to an int.

Parameters
offset:int

the offset within the class file

Returns:int

the unsigned byte at the specified offset within the class file

readU2back to summary
public int readU2(int offset)

Returns the u2 at the specified offset within the class file. Reads a 2-byte value and zero-extends it to an int.

Parameters
offset:int

the offset within the class file

Returns:int

the u2 at the specified offset within the class file

superclassEntryback to summary
public Optional<ClassEntry> superclassEntry()

Returns the constant pool entry describing the name of the superclass, if any.

Returns:Optional<ClassEntry>

the constant pool entry describing the name of the superclass, if any

See Also
ClassModel#superclass()
thisClassEntryback to summary
public ClassEntry thisClassEntry()

Returns the constant pool entry describing the name of class.

Returns:ClassEntry

the constant pool entry describing the name of class

See Also
ClassModel#thisClass()