Interface ClassReader

All Superinterfaces:
ConstantPool, Iterable<PoolEntry>
All Known Implementing Classes:
ClassReaderImpl

public sealed interface ClassReader extends ConstantPool permits ClassReaderImpl
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.
    boolean
    compare(BufWriter bufWriter, int bufWriterOffset, int classReaderOffset, int length)
    Compare a range of bytes from the classfile to a range of bytes within a BufWriter.
    void
    copyBytesTo(BufWriter buf, int offset, int len)
    Copy a range of bytes from the classfile to a BufWriter
    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.
    readClassEntry(int offset)
    Returns the class entry whose index is given at the specified offset within 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 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 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.
    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.
    Returns the method handle entry whose index is given at the specified offset within the classfile.
    readModuleEntry(int offset)
    Returns the module entry whose index is given at the specified offset within the classfile.
    Returns the name-and-type entry whose index is given at the specified offset within the classfile.
    readPackageEntry(int offset)
    Returns the package entry whose index is given 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.
    readUtf8Entry(int offset)
    Returns the UTF8 entry whose index is given at the specified offset within the classfile.
    readUtf8EntryOrNull(int offset)
    Returns the UTF8 entry whose index is given at the specified offset within the classfile, or null if the index at the specified offset is zero.
    int
    skipAttributeHolder(int offset)
    Returns the offset following the block of attributes starting at the specified position.
    Returns the constant pool entry describing the name of the superclass, if any.
    Returns the constant pool entry describing the name of class.
    int
    Returns the offset into the classfile of the this_class field.
    utf8EntryByIndex(int index)
    Returns the UTF8 constant pool entry at the given index of the constant pool.

    Methods inherited from interface org.glavo.classfile.constantpool.ConstantPool

    bootstrapMethodCount, bootstrapMethodEntry, entryByIndex, iterator, size

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Method Details

    • customAttributes

      Function<Utf8Entry,AttributeMapper<?>> customAttributes()
      Returns the table of custom attribute mappers. This is derived from the processing option ClassFile.AttributeMapperOption.
      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

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

      Optional<ClassEntry> 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
    • thisClassPos

      int thisClassPos()
      Returns the offset into the classfile of the this_class field.
      Returns:
      the offset into the classfile of the this_class field
    • classfileLength

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

      int skipAttributeHolder(int offset)
      Returns the offset following the block of attributes starting at the specified position.
      Parameters:
      offset - the offset into the classfile at which the attribute block starts
      Returns:
      the offset following the block of attributes starting at the specified position
    • utf8EntryByIndex

      Utf8Entry utf8EntryByIndex(int index)
      Returns the UTF8 constant pool entry at the given index of the constant pool. The given index must correspond to a valid constant pool index whose slot holds a UTF8 constant.
      Parameters:
      index - the index into the constant pool
      Returns:
      the UTF8 constant pool entry at the given index of the constant pool
    • readEntry

      PoolEntry readEntry(int offset)
      Returns the constant pool entry whose index is given at the specified offset within the classfile.
      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:
      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)
      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:
      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)
      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.
      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:
      ConstantPoolException - if the index is out of range of the constant pool size
    • readUtf8Entry

      Utf8Entry readUtf8Entry(int offset)
      Returns the UTF8 entry whose index is given at the specified offset within the classfile.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the UTF8 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, or the index does not correspond to a UTF8 entry
    • readUtf8EntryOrNull

      Utf8Entry readUtf8EntryOrNull(int offset)
      Returns the UTF8 entry whose index is given at the specified offset within the classfile, or null if the index at the specified offset is zero.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the UTF8 entry whose index is given at the specified offset within the classfile, or null if the index at the specified offset is zero
      Throws:
      ConstantPoolException - if the index is out of range of the constant pool size, or the index does not correspond to a UTF8 entry
    • readModuleEntry

      ModuleEntry readModuleEntry(int offset)
      Returns the module entry whose index is given at the specified offset within the classfile.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the module 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, or the index does not correspond to a module entry
    • readPackageEntry

      PackageEntry readPackageEntry(int offset)
      Returns the package entry whose index is given at the specified offset within the classfile.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the package 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, or the index does not correspond to a package entry
    • readClassEntry

      ClassEntry readClassEntry(int offset)
      Returns the class entry whose index is given at the specified offset within the classfile.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the class 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, or the index does not correspond to a class entry
    • readNameAndTypeEntry

      NameAndTypeEntry readNameAndTypeEntry(int offset)
      Returns the name-and-type entry whose index is given at the specified offset within the classfile.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the name-and-type 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, or the index does not correspond to a name-and-type entry
    • readMethodHandleEntry

      MethodHandleEntry readMethodHandleEntry(int offset)
      Returns the method handle entry whose index is given at the specified offset within the classfile.
      Parameters:
      offset - the offset of the index within the classfile
      Returns:
      the method handle 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, or the index does not correspond to a method handle entry
    • 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(BufWriter buf, int offset, int len)
      Copy a range of bytes from the classfile to a BufWriter
      Parameters:
      buf - the BufWriter
      offset - the offset within the classfile
      len - the length of the range
    • compare

      boolean compare(BufWriter bufWriter, int bufWriterOffset, int classReaderOffset, int length)
      Compare a range of bytes from the classfile to a range of bytes within a BufWriter.
      Parameters:
      bufWriter - the BufWriter
      bufWriterOffset - the offset within the BufWriter
      classReaderOffset - the offset within the classfile
      length - the length of the range
      Returns:
      whether the two ranges were identical