< prev index next >

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

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @enablePreview
 27  * @modules java.base/jdk.internal.access.foreign
 28  * @modules java.base/jdk.internal.foreign.layout
 29  *
 30  * @run testng/othervm -Xverify:all
 31  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 32  *   VarHandleTestExact
 33  * @run testng/othervm -Xverify:all
 34  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 35  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true
 36  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true
 37  *   VarHandleTestExact
 38  * @run testng/othervm -Xverify:all
 39  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 40  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false
 41  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false
 42  *   VarHandleTestExact
 43  * @run testng/othervm -Xverify:all
 44  *   -Djdk.internal.foreign.SHOULD_ADAPT_HANDLES=false
 45  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false
 46  *   -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true

154 
155         doTest(vh,
156             tvh -> tvh.set(arr, 0, testValue),
157             tvh -> setter.set(tvh, arr, testValue),
158             ".*\\Qhandle's method type (" + 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             ".*\\Qhandle's method type (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 = MethodHandles.memorySegmentViewVarHandle(ValueLayouts.valueLayout(carrier, ByteOrder.nativeOrder()));
175         try (Arena arena = Arena.ofConfined()) {
176             MemorySegment seg = arena.allocate(8);
177             doTest(vh,
178                 tvh -> tvh.set(seg, 0L, testValue),
179                 tvh -> setter.set(tvh, seg, 0L, testValue),
180                 ".*\\Qhandle's method type (MemorySegment,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();

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test

 26  * @modules java.base/jdk.internal.access.foreign
 27  * @modules java.base/jdk.internal.foreign.layout
 28  *
 29  * @run testng/othervm -Xverify:all
 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

153 
154         doTest(vh,
155             tvh -> tvh.set(arr, 0, testValue),
156             tvh -> setter.set(tvh, arr, testValue),
157             ".*\\Qhandle's method type (" + 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             ".*\\Qhandle's method type (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 = ValueLayouts.valueLayout(carrier, ByteOrder.nativeOrder()).varHandle();
174         try (Arena arena = Arena.ofConfined()) {
175             MemorySegment seg = arena.allocate(8);
176             doTest(vh,
177                 tvh -> tvh.set(seg, 0L, testValue),
178                 tvh -> setter.set(tvh, seg, 0L, testValue),
179                 ".*\\Qhandle's method type (MemorySegment,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();
< prev index next >