Interface ClassReader
- All Superinterfaces:
ConstantPool, Iterable<PoolEntry>
public sealed interface ClassReader extends ConstantPool
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:
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
int |
classfileLength() |
Returns the length of the class file, in number of bytes. |
void |
copyBytesTo |
Copy a range of bytes from the class file to a BufWriter. |
Function |
customAttributes() |
Returns the table of custom attribute mappers. |
int |
flags() |
Returns the access flags for the class, as a bit mask. |
byte[] |
readBytes |
Returns a copy of the bytes at the specified range in the class file. |
double |
readDouble |
Returns the double value at the specified offset within the
class file. |
PoolEntry |
readEntry |
Returns the constant pool entry whose index is given at the specified offset within the class file. |
<T extends PoolEntry> |
readEntry |
Returns the constant pool entry of a given type whose index is given at the specified offset within the class file. |
PoolEntry |
readEntryOrNull |
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. |
<T extends PoolEntry> |
readEntryOrNull |
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. |
float |
readFloat |
Returns the float value at the specified offset within the class file. |
int |
readInt |
Returns the signed int at the specified offset within the class file. |
long |
readLong |
Returns the signed long at the specified offset within the class file. |
int |
readS1 |
Returns the signed byte at the specified offset within the class file. |
int |
readS2 |
Returns the signed byte at the specified offset within the class file. |
int |
readU1 |
Returns the unsigned byte at the specified offset within the
class file. |
int |
readU2 |
Returns the u2 at the specified offset within the class file. |
Optional |
superclassEntry() |
Returns the constant pool entry describing the name of the superclass, if any. |
ClassEntry |
thisClassEntry() |
Returns the constant pool entry describing the name of class. |
Methods declared in interface ConstantPool
bootstrapMethodCount, bootstrapMethodEntry, entryByIndex, entryByIndex, iterator, size | Modifier and Type | Method | Description |
|---|---|---|
int |
bootstrapMethodCount() |
Returns the number of entries in the bootstrap method table. |
BootstrapMethodEntry |
bootstrapMethodEntry |
Returns the BootstrapMethodEntry at the specified index within the bootstrap method table. |
PoolEntry |
entryByIndex |
Returns the entry at the specified index. |
<T extends PoolEntry> |
entryByIndex |
Returns the entry of a given type at the specified index. |
default Iterator |
iterator() |
Returns an iterator over pool entries. |
int |
size() |
Returns the exclusive upper bound of the valid indices of this constant pool. |
Methods declared in interface Iterable
forEach, spliterator | Modifier and Type | Method | Description |
|---|---|---|
default void |
forEach |
Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
default Spliterator |
spliterator() |
Creates a Spliterator over the elements described by this Iterable. |
Method Details
customAttributes
Function<Utf8Entry, AttributeMapper<?>> customAttributes()
ClassFile.AttributeMapperOption.- Returns:
- the table of custom attribute mappers
flags
int flags()
- Returns:
- the access flags for the class, as a bit mask
- See Also:
thisClassEntry
ClassEntry thisClassEntry()
- Returns:
- the constant pool entry describing the name of class
- See Also:
superclassEntry
Optional<ClassEntry> superclassEntry()
- Returns:
- the constant pool entry describing the name of the superclass, if any
- See Also:
classfileLength
int classfileLength()
class file, in number of bytes.- Returns:
- the length of the
classfile, in number of bytes
readEntry
PoolEntry readEntry(int offset)
class file.- API Note:
- If only a particular type of entry is expected, use
readEntry(int, Class). - Parameters:
-
offset- the offset of the index within theclassfile - Returns:
- the constant pool entry whose index is given at the specified offset within the
classfile - Throws:
-
ConstantPoolException- if the index is out of range of the constant pool size, or zero
readEntry
<T extends PoolEntry> T readEntry(int offset, Class<T> cls)
class file.- Type Parameters:
T- the entry type- Parameters:
-
offset- the offset of the index within theclassfile -
cls- the entry type - Returns:
- the constant pool entry of a given type whose index is given at the specified offset within the
classfile - Throws:
-
ConstantPoolException- if the index is out of range of the constant pool size, or zero, or the entry is not of the given type
readEntryOrNull
PoolEntry readEntryOrNull(int offset)
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- the offset of the index within theclassfile - Returns:
- the constant pool entry whose index is given at the specified offset within the
classfile, ornullif the index at the specified offset is zero - Throws:
-
ConstantPoolException- if the index is out of range of the constant pool size
readEntryOrNull
<T extends PoolEntry> T readEntryOrNull(int offset, Class<T> cls)
class file, or null if the index at the specified offset is zero.- Type Parameters:
T- the entry type- Parameters:
-
offset- the offset of the index within theclassfile -
cls- the entry type - Returns:
- the constant pool entry of a given type whose index is given at the specified offset within the
classfile, ornullif the index at the specified offset is zero - Throws:
-
ConstantPoolException- if the index is out of range of the constant pool size, or zero, or the entry is not of the given type
readU1
int readU1(int offset)
class file. Reads a byte and zero-extends it to an int.- Parameters:
-
offset- the offset within theclassfile - Returns:
- the unsigned byte at the specified offset within the
classfile
readU2
readS1
int readS1(int offset)
class file. Reads a byte and sign-extends it to an int.- Parameters:
-
offset- the offset within theclassfile - Returns:
- the signed byte at the specified offset within the
classfile
readS2
int readS2(int offset)
class file. Reads a 2-byte value and sign-extends it to an int.- Parameters:
-
offset- the offset within theclassfile - Returns:
- the signed byte at the specified offset within the
classfile
readInt
int readInt(int offset)
class file. Reads 4 bytes of value.- Parameters:
-
offset- the offset within theclassfile - Returns:
- the signed int at the specified offset within the
classfile
readLong
long readLong(int offset)
class file. Reads 8 bytes of value.- Parameters:
-
offset- the offset within theclassfile - Returns:
- the signed long at the specified offset within the
classfile
readFloat
float readFloat(int offset)
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- the offset within theclassfile - Returns:
- the float value at the specified offset within the
classfile
readDouble
double readDouble(int offset)
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- the offset within theclassfile - Returns:
- the double value at the specified offset within the
classfile
readBytes
byte[] readBytes(int offset, int len)
class file.- Parameters:
-
offset- the offset within theclassfile -
len- the length of the range - Returns:
- a copy of the bytes at the specified range in the
classfile
copyBytesTo
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.