Class UnsafeReflectionUtil

java.lang.Object
io.github.connellite.reflection.UnsafeReflectionUtil

public class UnsafeReflectionUtil extends Object
Field access and the base address of a direct byte buffer.

Object fields go through sun.misc.Unsafe when that class can be used. The address of a direct buffer prefers java.lang.foreign.MemorySegment on Java 22 and later, and otherwise reads java.nio.Buffer.address. If neither path is available, isAvailable() is false and the other methods throw IllegalStateException. Loading this class does not fail when sun.misc.Unsafe is absent.

  • Constructor Details

    • UnsafeReflectionUtil

      public UnsafeReflectionUtil()
  • Method Details

    • isAvailable

      public static boolean isAvailable()
      Returns whether field access or a direct-buffer address can be served.
    • referenceBitsOf

      public static long referenceBitsOf(Object object)
      Returns the reference bits stored for object. null is 0. The value is the slot contents of an Object[], which is a narrow reference when the JVM compresses ordinary object pointers.
      Parameters:
      object - referenced object, or null
      Returns:
      reference bits, or 0 when object is null
      Throws:
      IllegalStateException - when field access is unavailable
    • objectFieldOffset

      public static long objectFieldOffset(Field field)
      Returns the offset of an instance field.
      Parameters:
      field - instance field
      Returns:
      byte offset of field within its declaring class
      Throws:
      IllegalStateException - when field access is unavailable
    • staticFieldOffset

      public static long staticFieldOffset(Field field)
      Returns the offset of a static field.
      Parameters:
      field - static field
      Returns:
      byte offset of field
      Throws:
      IllegalStateException - when field access is unavailable
    • staticFieldBase

      public static Object staticFieldBase(Field field)
      Returns the base object for a static field access.
      Parameters:
      field - static field
      Returns:
      base to pass with staticFieldOffset(Field)
      Throws:
      IllegalStateException - when field access is unavailable
    • getObject

      public static Object getObject(Object base, long offset)
      Reads a reference at offset within base.
      Throws:
      IllegalStateException - when field access is unavailable
    • putObject

      public static void putObject(Object base, long offset, Object value)
      Writes a reference at offset within base.
      Throws:
      IllegalStateException - when field access is unavailable
    • putObjectVolatile

      public static void putObjectVolatile(Object base, long offset, Object value)
      Writes a reference with volatile semantics.
      Throws:
      IllegalStateException - when field access is unavailable
    • getBoolean

      public static boolean getBoolean(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putBoolean

      public static void putBoolean(Object base, long offset, boolean value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putBooleanVolatile

      public static void putBooleanVolatile(Object base, long offset, boolean value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getByte

      public static byte getByte(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putByte

      public static void putByte(Object base, long offset, byte value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putByteVolatile

      public static void putByteVolatile(Object base, long offset, byte value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getChar

      public static char getChar(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putChar

      public static void putChar(Object base, long offset, char value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putCharVolatile

      public static void putCharVolatile(Object base, long offset, char value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getShort

      public static short getShort(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putShort

      public static void putShort(Object base, long offset, short value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putShortVolatile

      public static void putShortVolatile(Object base, long offset, short value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getInt

      public static int getInt(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putInt

      public static void putInt(Object base, long offset, int value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putIntVolatile

      public static void putIntVolatile(Object base, long offset, int value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getLong

      public static long getLong(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putLong

      public static void putLong(Object base, long offset, long value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putLongVolatile

      public static void putLongVolatile(Object base, long offset, long value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getFloat

      public static float getFloat(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putFloat

      public static void putFloat(Object base, long offset, float value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putFloatVolatile

      public static void putFloatVolatile(Object base, long offset, float value)
      Throws:
      IllegalStateException - when field access is unavailable
    • getDouble

      public static double getDouble(Object base, long offset)
      Throws:
      IllegalStateException - when field access is unavailable
    • putDouble

      public static void putDouble(Object base, long offset, double value)
      Throws:
      IllegalStateException - when field access is unavailable
    • putDoubleVolatile

      public static void putDoubleVolatile(Object base, long offset, double value)
      Throws:
      IllegalStateException - when field access is unavailable
    • directBufferAddress

      public static long directBufferAddress(ByteBuffer buffer)
      Returns the address of index 0 in a direct buffer. On Java 22 and later this is MemorySegment.ofBuffer(buffer).address() minus Buffer.position(). If that call fails, or the segment API is absent, the address is the java.nio.Buffer.address field.
      Parameters:
      buffer - direct byte buffer
      Returns:
      base address, not including buffer.position()
      Throws:
      IllegalArgumentException - when buffer is not direct
      IllegalStateException - when neither address path is available