Код: Выделить всё
MemoryLayout. Цель состоит в том, чтобы исключить использование Unsafe и заменить его неустаревшими API.
Два примера:
private static long methodA(Buffer x) {
return UNSAFE.getLong(x, addressFieldOffset);
}
Код: Выделить всё
private static Object methodB(ByteBuffer x) {
return UNSAFE.getObject(x, hbFieldOffset);
}
Я пробовал:
Код: Выделить всё
MemorySegment segment = MemorySegment.ofBuffer(x);
Код: Выделить всё
long result = segment.get(ValueLayout.ofLong, int offset) // I've tried several different layouts and different offsets,
Также пробовал:
Код: Выделить всё
MemorySegment segment = MemorySegment.ofArray(new long[10]); // also tried with byte[10] and int[10]
Код: Выделить всё
VarHandle handle = MethodHandles.arrayElementVarHandle(long[].class); \\ also tried byte[].class, int[].class, and long[].class
Buffer bufferField = Buffer.allocate(16)
VarHandle handle = MethodHandles.lookup().findStaticVarHandle(ClassIAmIn.class, "bufferField", Buffer.class);
Подробнее здесь: https://stackoverflow.com/questions/790 ... oreign-api