Interface ClassReader

All Superinterfaces:
ConstantPool,Iterable<PoolEntry>

public sealed interfaceClassReaderextendsConstantPool
Advancedclass file reading support forAttributeMappers. Supports reading arbitrary offsets within aclass file and reading data of various numeric types (e.g.,u2,u4) in addition to constant pool access.

All numeric values in theclass file format arebig endian.

Unless otherwise specified, all out-of-bounds access result in anIllegalArgumentException to indicate theclass file data is malformed. Since theclass 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
    Returns the length of theclass file, in number of bytes.
    void
    copyBytesTo(BufWriter buf, int offset, int len)
    Copy a range of bytes from theclass file to aBufWriter.
    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 theclass file.
    double
    readDouble(int offset)
    Returns the double value at the specified offset within the class file.
    readEntry(int offset)
    Returns the constant pool entry whose index is given at the specified offset within theclass file.
    <T extendsPoolEntry>
    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 theclass file.
    readEntryOrNull(int offset)
    Returns the constant pool entry whose index is given at the specified offset within theclass file, ornull if the index at the specified offset is zero.
    <T extendsPoolEntry>
    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 theclass file, ornull if the index at the specified offset is zero.
    float
    readFloat(int offset)
    Returns the float value at the specified offset within theclass file.
    int
    readInt(int offset)
    Returns the signed int at the specified offset within theclass file.
    long
    readLong(int offset)
    Returns the signed long at the specified offset within theclass file.
    int
    readS1(int offset)
    Returns the signed byte at the specified offset within theclass file.
    int
    readS2(int offset)
    Returns the signed byte at the specified offset within theclass file.
    int
    readU1(int offset)
    Returns the unsigned byte at the specified offset within the class file.
    int
    readU2(int offset)
    Returns the unsigned short at the specified offset within the class file.
    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.ConstantPool

    bootstrapMethodCount,bootstrapMethodEntry,entryByIndex,entryByIndex,iterator,size

    Methods declared in 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 optionClassFile.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
      See Also:
    • thisClassEntry

      ClassEntry thisClassEntry()
      Returns the constant pool entry describing the name of class.
      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.
      Returns:
      the constant pool entry describing the name of the superclass, if any
      See Also:
    • classfileLength

      int classfileLength()
      Returns the length of theclass file, in number of bytes.
      Returns:
      the length of theclass file, in number of bytes
    • readEntry

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

      <T extendsPoolEntry> 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 theclass file.
      Type Parameters:
      T - the entry type
      Parameters:
      offset - the offset of the index within theclass file
      cls - the entry type
      Returns:
      the constant pool entry of a given type whose index is given at the specified offset within theclass file
      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 theclass file, ornull if the index at the specified offset is zero.
      API Note:
      If only a particular type of entry is expected, usereadEntryOrNull(int, Class).
      Parameters:
      offset - the offset of the index within theclass file
      Returns:
      the constant pool entry whose index is given at the specified offset within theclass file, ornull if the index at the specified offset is zero
      Throws:
      ConstantPoolException - if the index is out of range of the constant pool size
    • readEntryOrNull

      <T extendsPoolEntry> 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 theclass file, ornull if the index at the specified offset is zero.
      Type Parameters:
      T - the entry type
      Parameters:
      offset - the offset of the index within theclass file
      cls - the entry type
      Returns:
      the constant pool entry of a given type whose index is given at the specified offset within theclass file, ornull if 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)
      Returns the unsigned byte at the specified offset within the class file. Reads a byte and zero-extends it to anint.
      Parameters:
      offset - the offset within theclass file
      Returns:
      the unsigned byte at the specified offset within the class file
    • readU2

      int readU2(int offset)
      Returns the unsigned short at the specified offset within the class file. Reads a 2-byte value and zero-extends it to anint.
      Parameters:
      offset - the offset within theclass file
      Returns:
      the unsigned short at the specified offset within the class file
    • readS1

      int readS1(int offset)
      Returns the signed byte at the specified offset within theclass file. Reads a byte and sign-extends it to anint.
      Parameters:
      offset - the offset within theclass file
      Returns:
      the signed byte at the specified offset within theclass file
    • readS2

      int readS2(int offset)
      Returns the signed byte at the specified offset within theclass file. Reads a 2-byte value and sign-extends it to anint.
      Parameters:
      offset - the offset within theclass file
      Returns:
      the signed byte at the specified offset within theclass file
    • readInt

      int readInt(int offset)
      Returns the signed int at the specified offset within theclass file. Reads 4 bytes of value.
      Parameters:
      offset - the offset within theclass file
      Returns:
      the signed int at the specified offset within theclass file
    • readLong

      long readLong(int offset)
      Returns the signed long at the specified offset within theclass file. Reads 8 bytes of value.
      Parameters:
      offset - the offset within theclass file
      Returns:
      the signed long at the specified offset within theclass file
    • readFloat

      float readFloat(int offset)
      Returns the float value at the specified offset within theclass file. Reads 4 bytes of value.

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

      Parameters:
      offset - the offset within theclass file
      Returns:
      the float value at the specified offset within theclass file
    • readDouble

      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 thedouble may or may not be collapsed into a single"canonical" NaN value.

      Parameters:
      offset - the offset within theclass file
      Returns:
      the double value at the specified offset within the class file
    • readBytes

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

      void copyBytesTo(BufWriter buf, int offset, int len)
      Copy a range of bytes from theclass file to aBufWriter.
      Parameters:
      buf - theBufWriter
      offset - the offset within theclass file
      len - the length of the range