< prev index next >

test/jdk/java/lang/invoke/VarHandles/X-VarHandleTestAccess.java.template

Print this page

   1 /*
   2  * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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  * @run testng/othervm -Diters=10   -Xint                                                   VarHandleTestAccess$Type$
  27  *
  28  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
  29  *          to hit compilation thresholds
  30  *
  31  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  32  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1                         VarHandleTestAccess$Type$
  33  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  34  */
  35 
  36 import org.testng.annotations.BeforeClass;
  37 import org.testng.annotations.DataProvider;
  38 import org.testng.annotations.Test;
  39 
  40 import java.lang.invoke.MethodHandles;
  41 import java.lang.invoke.VarHandle;
  42 import java.util.ArrayList;
  43 import java.util.Arrays;
  44 import java.util.List;
  45 

  55     $type$ v;
  56 
  57     static final $type$ static_final_v2 = $value1$;
  58 
  59     static $type$ static_v2;
  60 
  61     final $type$ final_v2 = $value1$;
  62 
  63     $type$ v2;
  64 
  65     VarHandle vhFinalField;
  66 
  67     VarHandle vhField;
  68 
  69     VarHandle vhStaticField;
  70 
  71     VarHandle vhStaticFinalField;
  72 
  73     VarHandle vhArray;
  74 
  75 #if[String]
  76     VarHandle vhArrayObject;
  77 #end[String]
  78 
  79     VarHandle[] allocate(boolean same) {
  80         List<VarHandle> vhs = new ArrayList<>();
  81 
  82         String postfix = same ? "" : "2";
  83         VarHandle vh;
  84         try {
  85             vh = MethodHandles.lookup().findVarHandle(
  86                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  87             vhs.add(vh);
  88 
  89             vh = MethodHandles.lookup().findVarHandle(
  90                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
  91             vhs.add(vh);
  92 
  93             vh = MethodHandles.lookup().findStaticVarHandle(
  94                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);
  95             vhs.add(vh);
  96 
  97             vh = MethodHandles.lookup().findStaticVarHandle(

 113             throw new InternalError(e);
 114         }
 115         return vhs.toArray(new VarHandle[0]);
 116     }
 117 
 118     @BeforeClass
 119     public void setup() throws Exception {
 120         vhFinalField = MethodHandles.lookup().findVarHandle(
 121                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 122 
 123         vhField = MethodHandles.lookup().findVarHandle(
 124                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 125 
 126         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 127             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 128 
 129         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 130             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 131 
 132         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 133 #if[String]
 134         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 135 #end[String]
 136     }
 137 
 138 
 139     @DataProvider
 140     public Object[][] varHandlesProvider() throws Exception {
 141         List<VarHandle> vhs = new ArrayList<>();
 142         vhs.add(vhField);
 143         vhs.add(vhStaticField);
 144         vhs.add(vhArray);
 145 
 146         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 147     }
 148 
 149     @Test
 150     public void testEquals() {
 151         VarHandle[] vhs1 = allocate(true);
 152         VarHandle[] vhs2 = allocate(true);
 153 
 154         for (int i = 0; i < vhs1.length; i++) {
 155             for (int j = 0; j < vhs1.length; j++) {

 297         cases.add(new VarHandleAccessTestCase("Static final field",
 298                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalField));
 299         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 300                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalFieldUnsupported,
 301                                               false));
 302 
 303         cases.add(new VarHandleAccessTestCase("Instance field",
 304                                               vhField, vh -> testInstanceField(this, vh)));
 305         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 306                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 307                                               false));
 308 
 309         cases.add(new VarHandleAccessTestCase("Static field",
 310                                               vhStaticField, VarHandleTestAccess$Type$::testStaticField));
 311         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 312                                               vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported,
 313                                               false));
 314 
 315         cases.add(new VarHandleAccessTestCase("Array",
 316                                               vhArray, VarHandleTestAccess$Type$::testArray));
 317 #if[String]
 318         cases.add(new VarHandleAccessTestCase("Array Object[]",
 319                                               vhArrayObject, VarHandleTestAccess$Type$::testArray));
 320 #end[String]
 321         cases.add(new VarHandleAccessTestCase("Array unsupported",
 322                                               vhArray, VarHandleTestAccess$Type$::testArrayUnsupported,
 323                                               false));
 324         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 325                                               vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds,
 326                                               false));
 327 #if[String]
 328         cases.add(new VarHandleAccessTestCase("Array store exception",
 329                                               vhArrayObject, VarHandleTestAccess$Type$::testArrayStoreException,
 330                                               false));
 331 #end[String]
 332         // Work around issue with jtreg summary reporting which truncates
 333         // the String result of Object.toString to 30 characters, hence
 334         // the first dummy argument
 335         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 336     }
 337 
 338     @Test(dataProvider = "accessTestCaseProvider")
 339     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 340         T t = atc.get();
 341         int iters = atc.requiresLoop() ? ITERS : 1;
 342         for (int c = 0; c < iters; c++) {
 343             atc.testAccess(t);
 344         }
 345     }
 346 
 347     static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 348         // Plain
 349         {
 350             $type$ x = ($type$) vh.get(recv);
 351             assertEquals(x, $value1$, "get $type$ value");

1986 
1987             checkAIOOBE(() -> {
1988                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, $value1$);
1989             });
1990 
1991             checkAIOOBE(() -> {
1992                 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, $value1$);
1993             });
1994 
1995             checkAIOOBE(() -> {
1996                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, $value1$);
1997             });
1998 
1999             checkAIOOBE(() -> {
2000                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, $value1$);
2001             });
2002 #end[Bitwise]
2003         }
2004     }
2005 
2006 #if[String]
2007     static void testArrayStoreException(VarHandle vh) throws Throwable {
2008         Object[] array = new $type$[10];
2009         Arrays.fill(array, $value1$);
2010         Object value = new Object();
2011 
2012         // Set
2013         checkASE(() -> {
2014             vh.set(array, 0, value);
2015         });
2016 
2017         // SetVolatile
2018         checkASE(() -> {
2019             vh.setVolatile(array, 0, value);
2020         });
2021 
2022         // SetOpaque
2023         checkASE(() -> {
2024             vh.setOpaque(array, 0, value);
2025         });
2026 

2067         // CompareAndExchangeRelease
2068         checkASE(() -> { // receiver reference class
2069             $type$ x = ($type$) vh.compareAndExchangeRelease(array, 0, $value1$, value);
2070         });
2071 
2072         // GetAndSet
2073         checkASE(() -> { // receiver reference class
2074             $type$ x = ($type$) vh.getAndSet(array, 0, value);
2075         });
2076 
2077         // GetAndSetAcquire
2078         checkASE(() -> { // receiver reference class
2079             $type$ x = ($type$) vh.getAndSetAcquire(array, 0, value);
2080         });
2081 
2082         // GetAndSetRelease
2083         checkASE(() -> { // receiver reference class
2084             $type$ x = ($type$) vh.getAndSetRelease(array, 0, value);
2085         });
2086     }
2087 #end[String]
2088 }
2089 

   1 /*
   2  * Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   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 #warn
  25 
  26 /*
  27  * @test
  28 #if[Value]
  29  * @enablePreview
  30  * @modules java.base/jdk.internal.vm.annotation
  31 #end[Value]
  32  * @run testng/othervm -Diters=10   -Xint                                                   VarHandleTestAccess$Type$
  33  *
  34  * @comment Set CompileThresholdScaling to 0.1 so that the warmup loop sets to 2000 iterations
  35  *          to hit compilation thresholds
  36  *
  37  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  38  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1                         VarHandleTestAccess$Type$
  39  * @run testng/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  40  */
  41 
  42 import org.testng.annotations.BeforeClass;
  43 import org.testng.annotations.DataProvider;
  44 import org.testng.annotations.Test;
  45 
  46 import java.lang.invoke.MethodHandles;
  47 import java.lang.invoke.VarHandle;
  48 import java.util.ArrayList;
  49 import java.util.Arrays;
  50 import java.util.List;
  51 

  61     $type$ v;
  62 
  63     static final $type$ static_final_v2 = $value1$;
  64 
  65     static $type$ static_v2;
  66 
  67     final $type$ final_v2 = $value1$;
  68 
  69     $type$ v2;
  70 
  71     VarHandle vhFinalField;
  72 
  73     VarHandle vhField;
  74 
  75     VarHandle vhStaticField;
  76 
  77     VarHandle vhStaticFinalField;
  78 
  79     VarHandle vhArray;
  80 
  81 #if[Object]
  82     VarHandle vhArrayObject;
  83 #end[Object]
  84 
  85     VarHandle[] allocate(boolean same) {
  86         List<VarHandle> vhs = new ArrayList<>();
  87 
  88         String postfix = same ? "" : "2";
  89         VarHandle vh;
  90         try {
  91             vh = MethodHandles.lookup().findVarHandle(
  92                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  93             vhs.add(vh);
  94 
  95             vh = MethodHandles.lookup().findVarHandle(
  96                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
  97             vhs.add(vh);
  98 
  99             vh = MethodHandles.lookup().findStaticVarHandle(
 100                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);
 101             vhs.add(vh);
 102 
 103             vh = MethodHandles.lookup().findStaticVarHandle(

 119             throw new InternalError(e);
 120         }
 121         return vhs.toArray(new VarHandle[0]);
 122     }
 123 
 124     @BeforeClass
 125     public void setup() throws Exception {
 126         vhFinalField = MethodHandles.lookup().findVarHandle(
 127                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 128 
 129         vhField = MethodHandles.lookup().findVarHandle(
 130                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 131 
 132         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 133             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 134 
 135         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 136             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 137 
 138         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 139 #if[Object]
 140         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 141 #end[Object]
 142     }
 143 
 144 
 145     @DataProvider
 146     public Object[][] varHandlesProvider() throws Exception {
 147         List<VarHandle> vhs = new ArrayList<>();
 148         vhs.add(vhField);
 149         vhs.add(vhStaticField);
 150         vhs.add(vhArray);
 151 
 152         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 153     }
 154 
 155     @Test
 156     public void testEquals() {
 157         VarHandle[] vhs1 = allocate(true);
 158         VarHandle[] vhs2 = allocate(true);
 159 
 160         for (int i = 0; i < vhs1.length; i++) {
 161             for (int j = 0; j < vhs1.length; j++) {

 303         cases.add(new VarHandleAccessTestCase("Static final field",
 304                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalField));
 305         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 306                                               vhStaticFinalField, VarHandleTestAccess$Type$::testStaticFinalFieldUnsupported,
 307                                               false));
 308 
 309         cases.add(new VarHandleAccessTestCase("Instance field",
 310                                               vhField, vh -> testInstanceField(this, vh)));
 311         cases.add(new VarHandleAccessTestCase("Instance field unsupported",
 312                                               vhField, vh -> testInstanceFieldUnsupported(this, vh),
 313                                               false));
 314 
 315         cases.add(new VarHandleAccessTestCase("Static field",
 316                                               vhStaticField, VarHandleTestAccess$Type$::testStaticField));
 317         cases.add(new VarHandleAccessTestCase("Static field unsupported",
 318                                               vhStaticField, VarHandleTestAccess$Type$::testStaticFieldUnsupported,
 319                                               false));
 320 
 321         cases.add(new VarHandleAccessTestCase("Array",
 322                                               vhArray, VarHandleTestAccess$Type$::testArray));
 323 #if[Object]
 324         cases.add(new VarHandleAccessTestCase("Array Object[]",
 325                                               vhArrayObject, VarHandleTestAccess$Type$::testArray));
 326 #end[Object]
 327         cases.add(new VarHandleAccessTestCase("Array unsupported",
 328                                               vhArray, VarHandleTestAccess$Type$::testArrayUnsupported,
 329                                               false));
 330         cases.add(new VarHandleAccessTestCase("Array index out of bounds",
 331                                               vhArray, VarHandleTestAccess$Type$::testArrayIndexOutOfBounds,
 332                                               false));
 333 #if[Object]
 334         cases.add(new VarHandleAccessTestCase("Array store exception",
 335                                               vhArrayObject, VarHandleTestAccess$Type$::testArrayStoreException,
 336                                               false));
 337 #end[Object]
 338         // Work around issue with jtreg summary reporting which truncates
 339         // the String result of Object.toString to 30 characters, hence
 340         // the first dummy argument
 341         return cases.stream().map(tc -> new Object[]{tc.toString(), tc}).toArray(Object[][]::new);
 342     }
 343 
 344     @Test(dataProvider = "accessTestCaseProvider")
 345     public <T> void testAccess(String desc, AccessTestCase<T> atc) throws Throwable {
 346         T t = atc.get();
 347         int iters = atc.requiresLoop() ? ITERS : 1;
 348         for (int c = 0; c < iters; c++) {
 349             atc.testAccess(t);
 350         }
 351     }
 352 
 353     static void testInstanceFinalField(VarHandleTestAccess$Type$ recv, VarHandle vh) {
 354         // Plain
 355         {
 356             $type$ x = ($type$) vh.get(recv);
 357             assertEquals(x, $value1$, "get $type$ value");

1992 
1993             checkAIOOBE(() -> {
1994                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, $value1$);
1995             });
1996 
1997             checkAIOOBE(() -> {
1998                 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, $value1$);
1999             });
2000 
2001             checkAIOOBE(() -> {
2002                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, $value1$);
2003             });
2004 
2005             checkAIOOBE(() -> {
2006                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, $value1$);
2007             });
2008 #end[Bitwise]
2009         }
2010     }
2011 
2012 #if[Object]
2013     static void testArrayStoreException(VarHandle vh) throws Throwable {
2014         Object[] array = new $type$[10];
2015         Arrays.fill(array, $value1$);
2016         Object value = new Object();
2017 
2018         // Set
2019         checkASE(() -> {
2020             vh.set(array, 0, value);
2021         });
2022 
2023         // SetVolatile
2024         checkASE(() -> {
2025             vh.setVolatile(array, 0, value);
2026         });
2027 
2028         // SetOpaque
2029         checkASE(() -> {
2030             vh.setOpaque(array, 0, value);
2031         });
2032 

2073         // CompareAndExchangeRelease
2074         checkASE(() -> { // receiver reference class
2075             $type$ x = ($type$) vh.compareAndExchangeRelease(array, 0, $value1$, value);
2076         });
2077 
2078         // GetAndSet
2079         checkASE(() -> { // receiver reference class
2080             $type$ x = ($type$) vh.getAndSet(array, 0, value);
2081         });
2082 
2083         // GetAndSetAcquire
2084         checkASE(() -> { // receiver reference class
2085             $type$ x = ($type$) vh.getAndSetAcquire(array, 0, value);
2086         });
2087 
2088         // GetAndSetRelease
2089         checkASE(() -> { // receiver reference class
2090             $type$ x = ($type$) vh.getAndSetRelease(array, 0, value);
2091         });
2092     }
2093 #end[Object]
2094 }
2095 
< prev index next >