Package io.github.connellite.reflection
Class UnsafeReflectionUtil
java.lang.Object
io.github.connellite.reflection.UnsafeReflectionUtil
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longdirectBufferAddress(ByteBuffer buffer) Returns the address of index 0 in a direct buffer.static booleangetBoolean(Object base, long offset) static bytestatic charstatic doublestatic floatstatic intstatic longstatic ObjectReads a reference atoffsetwithinbase.static shortstatic booleanReturns whether field access or a direct-buffer address can be served.static longobjectFieldOffset(Field field) Returns the offset of an instance field.static voidputBoolean(Object base, long offset, boolean value) static voidputBooleanVolatile(Object base, long offset, boolean value) static voidstatic voidputByteVolatile(Object base, long offset, byte value) static voidstatic voidputCharVolatile(Object base, long offset, char value) static voidstatic voidputDoubleVolatile(Object base, long offset, double value) static voidstatic voidputFloatVolatile(Object base, long offset, float value) static voidstatic voidputIntVolatile(Object base, long offset, int value) static voidstatic voidputLongVolatile(Object base, long offset, long value) static voidWrites a reference atoffsetwithinbase.static voidputObjectVolatile(Object base, long offset, Object value) Writes a reference withvolatilesemantics.static voidstatic voidputShortVolatile(Object base, long offset, short value) static longreferenceBitsOf(Object object) Returns the reference bits stored forobject.static ObjectstaticFieldBase(Field field) Returns the base object for a static field access.static longstaticFieldOffset(Field field) Returns the offset of a static field.
-
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
Returns the reference bits stored forobject.nullis0. The value is the slot contents of anObject[], which is a narrow reference when the JVM compresses ordinary object pointers.- Parameters:
object- referenced object, ornull- Returns:
- reference bits, or
0whenobjectisnull - Throws:
IllegalStateException- when field access is unavailable
-
objectFieldOffset
Returns the offset of an instance field.- Parameters:
field- instance field- Returns:
- byte offset of
fieldwithin its declaring class - Throws:
IllegalStateException- when field access is unavailable
-
staticFieldOffset
Returns the offset of a static field.- Parameters:
field- static field- Returns:
- byte offset of
field - Throws:
IllegalStateException- when field access is unavailable
-
staticFieldBase
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
Reads a reference atoffsetwithinbase.- Throws:
IllegalStateException- when field access is unavailable
-
putObject
Writes a reference atoffsetwithinbase.- Throws:
IllegalStateException- when field access is unavailable
-
putObjectVolatile
Writes a reference withvolatilesemantics.- Throws:
IllegalStateException- when field access is unavailable
-
getBoolean
- Throws:
IllegalStateException- when field access is unavailable
-
putBoolean
- Throws:
IllegalStateException- when field access is unavailable
-
putBooleanVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getByte
- Throws:
IllegalStateException- when field access is unavailable
-
putByte
- Throws:
IllegalStateException- when field access is unavailable
-
putByteVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getChar
- Throws:
IllegalStateException- when field access is unavailable
-
putChar
- Throws:
IllegalStateException- when field access is unavailable
-
putCharVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getShort
- Throws:
IllegalStateException- when field access is unavailable
-
putShort
- Throws:
IllegalStateException- when field access is unavailable
-
putShortVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getInt
- Throws:
IllegalStateException- when field access is unavailable
-
putInt
- Throws:
IllegalStateException- when field access is unavailable
-
putIntVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getLong
- Throws:
IllegalStateException- when field access is unavailable
-
putLong
- Throws:
IllegalStateException- when field access is unavailable
-
putLongVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getFloat
- Throws:
IllegalStateException- when field access is unavailable
-
putFloat
- Throws:
IllegalStateException- when field access is unavailable
-
putFloatVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
getDouble
- Throws:
IllegalStateException- when field access is unavailable
-
putDouble
- Throws:
IllegalStateException- when field access is unavailable
-
putDoubleVolatile
- Throws:
IllegalStateException- when field access is unavailable
-
directBufferAddress
Returns the address of index 0 in a direct buffer. On Java 22 and later this isMemorySegment.ofBuffer(buffer).address()minusBuffer.position(). If that call fails, or the segment API is absent, the address is thejava.nio.Buffer.addressfield.- Parameters:
buffer- direct byte buffer- Returns:
- base address, not including
buffer.position() - Throws:
IllegalArgumentException- whenbufferis not directIllegalStateException- when neither address path is available
-