use of java.lang.invoke.VarHandle in project tutorials by eugenp.
the class VariableHandlesTest method givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged.
@Test
public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles.lookup().in(VariableHandlesTest.class).findVarHandle(VariableHandlesTest.class, "variableToSet", int.class);
publicIntHandle.set(this, 15);
assertThat((int) publicIntHandle.get(this) == 15);
}
use of java.lang.invoke.VarHandle in project tutorials by eugenp.
the class VariableHandlesTest method whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly.
@Test
public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
VarHandle privateIntHandle = MethodHandles.privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()).findVarHandle(VariableHandlesTest.class, "privateTestVariable", int.class);
assertThat(privateIntHandle.coordinateTypes().size() == 1);
assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesTest.class);
}
use of java.lang.invoke.VarHandle in project tutorials by eugenp.
the class VariableHandlesTest method whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly.
@Test
public void whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException {
VarHandle arrayVarHandle = MethodHandles.arrayElementVarHandle(int[].class);
assertThat(arrayVarHandle.coordinateTypes().size() == 2);
assertThat(arrayVarHandle.coordinateTypes().get(0) == int[].class);
}
use of java.lang.invoke.VarHandle in project tutorials by eugenp.
the class VariableHandlesTest method givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned.
@Test
public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException {
VarHandle publicIntHandle = MethodHandles.lookup().in(VariableHandlesTest.class).findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class);
assertThat((int) publicIntHandle.get(this) == 1);
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests method test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer.
@Test
public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT);
MemoryAccess.setFloat(floatSegmt, 12.12F);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt, 18.23F);
floatHandle2.set(structSegmt, 19.34F);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(floatSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_FLOAT.byteSize());
VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
float result = (float) floatHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49.69F, 0.01F);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), floatSegmt.address().toRawLongValue());
floatSegmt.close();
structSegmt.close();
resultSegmt.close();
}
Aggregations