Search in sources :

Example 1 with VarHandle

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);
}
Also used : VarHandle(java.lang.invoke.VarHandle) Test(org.junit.Test)

Example 2 with VarHandle

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);
}
Also used : VarHandle(java.lang.invoke.VarHandle) Test(org.junit.Test)

Example 3 with VarHandle

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);
}
Also used : VarHandle(java.lang.invoke.VarHandle) Test(org.junit.Test)

Example 4 with VarHandle

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);
}
Also used : VarHandle(java.lang.invoke.VarHandle) Test(org.junit.Test)

Example 5 with VarHandle

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();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

VarHandle (java.lang.invoke.VarHandle)400 Test (org.testng.annotations.Test)388 MethodHandle (java.lang.invoke.MethodHandle)319 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)318 GroupLayout (jdk.incubator.foreign.GroupLayout)318 MemorySegment (jdk.incubator.foreign.MemorySegment)318 ResourceScope (jdk.incubator.foreign.ResourceScope)254 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)254 MethodType (java.lang.invoke.MethodType)192 Addressable (jdk.incubator.foreign.Addressable)128 NativeSymbol (jdk.incubator.foreign.NativeSymbol)126 MemoryAddress (jdk.incubator.foreign.MemoryAddress)78 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)64 Test (org.junit.Test)8 VarHandleDesc (java.lang.invoke.VarHandle.VarHandleDesc)2 ClassDesc (java.lang.constant.ClassDesc)1 Field (java.lang.reflect.Field)1