Interface ClassReader

All Superinterfaces:
ConstantPoolPREVIEW, Iterable<PoolEntryPREVIEW>

public sealed interface ClassReader extends ConstantPoolPREVIEW
ClassReader is a preview API of the Java platform.
Programs can only use ClassReader when preview features are enabled.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
Supports reading from a classfile. Methods are provided to read data of various numeric types (e.g., u2, u4) at a given offset within the classfile, copying raw bytes, and reading constant pool entries. Encapsulates additional reading context such as mappers for custom attributes and processing options.
Since:
22
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Returns the length of the classfile, in bytes.
    void
    copyBytesTo(BufWriterPREVIEW buf, int offset, int len)
    Copy a range of bytes from the classfile to a BufWriterPREVIEW
    Returns the table of custom attribute mappers.
    int
    Returns the access flags for the class, as a bit mask.
    byte[]
    readBytes(int offset, int len)
    Returns a copy of the bytes at the specified range in the classfile.
    double
    readDouble(int offset)
    Returns the double value at the specified offset within the classfile.
    readEntry(int offset)
    Returns the constant pool entry whose index is given at the specified offset within the classfile.
    <T extends PoolEntryPREVIEW>
    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 classfile.
    readEntryOrNull(int offset)
    Returns the constant pool entry whose index is given at the specified offset within the classfile, or null if the index at the specified offset is zero.
    <T extends PoolEntryPREVIEW>
    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 classfile, or null if the index at the specified offset is zero.
    float
    readFloat(int offset)
    Returns the float value at the specified offset within the classfile.
    int
    readInt(int offset)
    Returns the signed int at the specified offset within the classfile.
    long
    readLong(int offset)
    Returns the signed long at the specified offset within the classfile.
    int
    readS1(int offset)
    Returns the signed byte at the specified offset within the classfile.
    int
    readS2(int offset)
    Returns the signed byte at the specified offset within the classfile.
    int
    readU1(int offset)
    Returns the unsigned byte at the specified offset within the classfile.
    int
    readU2(int offset)
    Returns the unsigned short at the specified offset within the classfile.
    Returns the constant pool entry describing the name of the superclass, if any.
    Returns the constant pool entry describing the name of class.

    Methods declared in interface java.lang.classfile.constantpool.ConstantPoolPREVIEW

    bootstrapMethodCount, bootstrapMethodEntry, entryByIndex, entryByIndex, iterator, size

    Methods declared in interface java.lang.Iterable

    forEach, spliterator
  • Method Details

    • customAttributes

      Returns the table of custom attribute mappers. This is derived from the processing option ClassFile.AttributeMapperOptionPREVIEW.
      Returns:
      the table of custom attribute mappers
    • flags

      int flags()
      Returns the access flags for the class, as a bit mask .
      Returns:
      the access flags for the class, as a bit mask
    • thisClassEntry

      ClassEntryPREVIEW thisClassEntry()
      Returns the constant pool entry describing the name of class.
      Returns:
      the constant pool entry describing the name of class
    • superclassEntry

      Optional<ClassEntryPREVIEW> superclassEntry()
      Returns the constant pool entry describing the name of the superclass, if any.
      Returns:
      the constant pool entry describing the name of the superclass, if any
    • classfileLength

      int classfileLength()
      Returns the length of the classfile, in bytes.
      Returns:
      the length of the classfile, in bytes
    • readEntry

      PoolEntryPREVIEW readEntry(int offset)
      Returns the constant pool entry whose index is given at the specified offset within the classfile.
      API Note:
      If only a particular type of entry is expected, use readEntry(int, Class).
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the constant pool entry whose index is given at the specified offset within the classfile
      Throws:
      ConstantPoolExceptionPREVIEW - if the index is out of range of the constant pool size, or zero
    • readEntry

      <T extends PoolEntryPREVIEW> 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 classfile.
      Type Parameters:
      T - the entry type
      Parameters:
      offset - the offset of the index within the classfile
      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:
      ConstantPoolExceptionPREVIEW - if the index is out of range of the constant pool size, or zero, or the entry is not of the given type
    • readEntryOrNull

      PoolEntryPREVIEW readEntryOrNull(int offset)
      Returns the constant pool entry whose index is given at the specified offset within the classfile, 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 the classfile
      Returns:
      the constant pool entry whose index is given at the specified offset within the classfile, or null if the index at the specified offset is zero
      Throws:
      ConstantPoolExceptionPREVIEW - if the index is out of range of the constant pool size
    • readEntryOrNull

      <T extends PoolEntryPREVIEW> 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 classfile, 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 the classfile
      cls - the entry type
      Returns:
      the constant pool entry of a given type whose index is given at the specified offset within the classfile, or null if the index at the specified offset is zero
      Throws:
      ConstantPoolExceptionPREVIEW - if the index is out of range of the constant pool size, or zero, or the entry is not of the given type
      Since:
      23
    • readU1

      int readU1(int offset)
      Returns the unsigned byte at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the unsigned byte at the specified offset within the classfile
    • readU2

      int readU2(int offset)
      Returns the unsigned short at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the unsigned short at the specified offset within the classfile
    • readS1

      int readS1(int offset)
      Returns the signed byte at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the signed byte at the specified offset within the classfile
    • readS2

      int readS2(int offset)
      Returns the signed byte at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the signed byte at the specified offset within the classfile
    • readInt

      int readInt(int offset)
      Returns the signed int at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the signed int at the specified offset within the classfile
    • readLong

      long readLong(int offset)
      Returns the signed long at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the signed long at the specified offset within the classfile
    • readFloat

      float readFloat(int offset)
      Returns the float value at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the float value at the specified offset within the classfile
    • readDouble

      double readDouble(int offset)
      Returns the double value at the specified offset within the classfile.
      Parameters:
      offset - the offset within the classfile
      Returns:
      the double value at the specified offset within the classfile
    • readBytes

      byte[] readBytes(int offset, int len)
      Returns a copy of the bytes at the specified range in the classfile.
      Parameters:
      offset - the offset within the classfile
      len - the length of the range
      Returns:
      a copy of the bytes at the specified range in the classfile
    • copyBytesTo

      void copyBytesTo(BufWriterPREVIEW buf, int offset, int len)
      Copy a range of bytes from the classfile to a BufWriterPREVIEW
      Parameters:
      buf - the BufWriter
      offset - the offset within the classfile
      len - the length of the range