< prev index next >

test/jdk/java/lang/invoke/VarHandles/VarHandleTestExact.java

Print this page

 30  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 31  *   VarHandleTestExact
 32  * @run testng/othervm -Xverify:all
 33  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 34  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true
 35  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true
 36  *   VarHandleTestExact
 37  * @run testng/othervm -Xverify:all
 38  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 39  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false
 40  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false
 41  *   VarHandleTestExact
 42  * @run testng/othervm -Xverify:all
 43  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 44  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false
 45  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true
 46  *   VarHandleTestExact
 47  */
 48 
 49 import jdk.incubator.foreign.MemoryHandles;

 50 import jdk.incubator.foreign.MemorySegment;
 51 import jdk.incubator.foreign.ResourceScope;
 52 import jdk.internal.access.foreign.MemorySegmentProxy;
 53 import org.testng.SkipException;
 54 import org.testng.annotations.DataProvider;
 55 import org.testng.annotations.Test;
 56 
 57 import java.lang.invoke.MethodHandles;
 58 import java.lang.invoke.VarHandle;
 59 import java.lang.invoke.WrongMethodTypeException;
 60 import java.lang.reflect.Array;
 61 import java.nio.ByteBuffer;
 62 import java.nio.ByteOrder;
 63 import java.util.ArrayList;
 64 import java.util.List;
 65 import java.util.function.Consumer;
 66 
 67 import static org.testng.Assert.*;
 68 
 69 public class VarHandleTestExact {

153 
154         doTest(vh,
155             tvh -> tvh.set(arr, 0, testValue),
156             tvh -> setter.set(tvh, arr, testValue),
157             ".*\\Qexpected (" + arrayClass.getSimpleName() + ",int," + arrayClass.componentType().getSimpleName() + ")void \\E.*");
158     }
159 
160     @Test(dataProvider = "dataSetBuffer")
161     public void testExactBufferSet(Class<?> arrayClass, Object testValue, SetBufferX setter) {
162         VarHandle vh = MethodHandles.byteBufferViewVarHandle(arrayClass, ByteOrder.nativeOrder());
163         ByteBuffer buff = ByteBuffer.allocateDirect(8);
164 
165         doTest(vh,
166             tvh -> tvh.set(buff, 0, testValue),
167             tvh -> setter.set(tvh, buff, testValue),
168             ".*\\Qexpected (ByteBuffer,int," + arrayClass.componentType().getSimpleName() + ")void \\E.*");
169     }
170 
171     @Test(dataProvider = "dataSetMemorySegment")
172     public void testExactSegmentSet(Class<?> carrier, Object testValue, SetSegmentX setter) {
173         VarHandle vh = MemoryHandles.varHandle(carrier, ByteOrder.nativeOrder());
174         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
175             MemorySegment seg = MemorySegment.allocateNative(8, scope);
176             doTest(vh,
177                 tvh -> tvh.set(seg, 0L, testValue),
178                 tvh -> setter.set(tvh, seg, 0L, testValue),
179                 ".*\\Qexpected (MemorySegmentProxy,long," + carrier.getSimpleName() + ")void \\E.*");
180         }
181     }
182 
183     private static void doTest(VarHandle invokeHandle, Consumer<VarHandle> invokeTest,
184                                Consumer<VarHandle> invokeExactTest, String expectedMessage) {
185         assertFalse(invokeHandle.hasInvokeExactBehavior());
186         assertSame(invokeHandle, invokeHandle.withInvokeBehavior());
187         try {
188             invokeTest.accept(invokeHandle);
189         } catch (WrongMethodTypeException wmte) {
190             fail("Unexpected exception", wmte);
191         }
192 
193         VarHandle invokeExactHandle = invokeHandle.withInvokeExactBehavior();

 30  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 31  *   VarHandleTestExact
 32  * @run testng/othervm -Xverify:all
 33  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 34  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true
 35  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true
 36  *   VarHandleTestExact
 37  * @run testng/othervm -Xverify:all
 38  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 39  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false
 40  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false
 41  *   VarHandleTestExact
 42  * @run testng/othervm -Xverify:all
 43  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 44  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false
 45  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true
 46  *   VarHandleTestExact
 47  */
 48 
 49 import jdk.incubator.foreign.MemoryHandles;
 50 import jdk.incubator.foreign.MemoryLayout;
 51 import jdk.incubator.foreign.MemorySegment;
 52 import jdk.incubator.foreign.ResourceScope;
 53 import jdk.internal.access.foreign.MemorySegmentProxy;
 54 import org.testng.SkipException;
 55 import org.testng.annotations.DataProvider;
 56 import org.testng.annotations.Test;
 57 
 58 import java.lang.invoke.MethodHandles;
 59 import java.lang.invoke.VarHandle;
 60 import java.lang.invoke.WrongMethodTypeException;
 61 import java.lang.reflect.Array;
 62 import java.nio.ByteBuffer;
 63 import java.nio.ByteOrder;
 64 import java.util.ArrayList;
 65 import java.util.List;
 66 import java.util.function.Consumer;
 67 
 68 import static org.testng.Assert.*;
 69 
 70 public class VarHandleTestExact {

154 
155         doTest(vh,
156             tvh -> tvh.set(arr, 0, testValue),
157             tvh -> setter.set(tvh, arr, testValue),
158             ".*\\Qexpected (" + arrayClass.getSimpleName() + ",int," + arrayClass.componentType().getSimpleName() + ")void \\E.*");
159     }
160 
161     @Test(dataProvider = "dataSetBuffer")
162     public void testExactBufferSet(Class<?> arrayClass, Object testValue, SetBufferX setter) {
163         VarHandle vh = MethodHandles.byteBufferViewVarHandle(arrayClass, ByteOrder.nativeOrder());
164         ByteBuffer buff = ByteBuffer.allocateDirect(8);
165 
166         doTest(vh,
167             tvh -> tvh.set(buff, 0, testValue),
168             tvh -> setter.set(tvh, buff, testValue),
169             ".*\\Qexpected (ByteBuffer,int," + arrayClass.componentType().getSimpleName() + ")void \\E.*");
170     }
171 
172     @Test(dataProvider = "dataSetMemorySegment")
173     public void testExactSegmentSet(Class<?> carrier, Object testValue, SetSegmentX setter) {
174         VarHandle vh = MemoryHandles.varHandle(MemoryLayout.valueLayout(carrier, ByteOrder.nativeOrder()));
175         try (ResourceScope scope = ResourceScope.newConfinedScope()) {
176             MemorySegment seg = MemorySegment.allocateNative(8, scope);
177             doTest(vh,
178                 tvh -> tvh.set(seg, 0L, testValue),
179                 tvh -> setter.set(tvh, seg, 0L, testValue),
180                 ".*\\Qexpected (MemorySegmentProxy,long," + carrier.getSimpleName() + ")void \\E.*");
181         }
182     }
183 
184     private static void doTest(VarHandle invokeHandle, Consumer<VarHandle> invokeTest,
185                                Consumer<VarHandle> invokeExactTest, String expectedMessage) {
186         assertFalse(invokeHandle.hasInvokeExactBehavior());
187         assertSame(invokeHandle, invokeHandle.withInvokeBehavior());
188         try {
189             invokeTest.accept(invokeHandle);
190         } catch (WrongMethodTypeException wmte) {
191             fail("Unexpected exception", wmte);
192         }
193 
194         VarHandle invokeExactHandle = invokeHandle.withInvokeExactBehavior();
< prev index next >