< prev index next >

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

Print this page

   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 junit/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 junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  32  * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1                         VarHandleTestAccess$Type$
  33  * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  34  */
  35 
  36 import java.lang.invoke.MethodHandles;
  37 import java.lang.invoke.VarHandle;
  38 import java.util.ArrayList;
  39 import java.util.Arrays;
  40 import java.util.List;
  41 
  42 import static org.junit.jupiter.api.Assertions.*;
  43 import org.junit.jupiter.api.BeforeAll;
  44 import org.junit.jupiter.api.Test;
  45 import org.junit.jupiter.api.TestInstance;

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

 115             throw new InternalError(e);
 116         }
 117         return vhs.toArray(new VarHandle[0]);
 118     }
 119 
 120     @BeforeAll
 121     public void setup() throws Exception {
 122         vhFinalField = MethodHandles.lookup().findVarHandle(
 123                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 124 
 125         vhField = MethodHandles.lookup().findVarHandle(
 126                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 127 
 128         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 129             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 130 
 131         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 132             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 133 
 134         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 135 #if[String]
 136         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 137 #end[String]
 138     }
 139 
 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++) {
 156                 if (i != j) {
 157                     assertNotEquals(vhs1[i], vhs1[j]);

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

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

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

   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 junit/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 junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  38  * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1                         VarHandleTestAccess$Type$
  39  * @run junit/othervm -Diters=2000 -XX:CompileThresholdScaling=0.1 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  40  */
  41 
  42 import java.lang.invoke.MethodHandles;
  43 import java.lang.invoke.VarHandle;
  44 import java.util.ArrayList;
  45 import java.util.Arrays;
  46 import java.util.List;
  47 
  48 import static org.junit.jupiter.api.Assertions.*;
  49 import org.junit.jupiter.api.BeforeAll;
  50 import org.junit.jupiter.api.Test;
  51 import org.junit.jupiter.api.TestInstance;

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

 121             throw new InternalError(e);
 122         }
 123         return vhs.toArray(new VarHandle[0]);
 124     }
 125 
 126     @BeforeAll
 127     public void setup() throws Exception {
 128         vhFinalField = MethodHandles.lookup().findVarHandle(
 129                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 130 
 131         vhField = MethodHandles.lookup().findVarHandle(
 132                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 133 
 134         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 135             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 136 
 137         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 138             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 139 
 140         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 141 #if[Object]
 142         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 143 #end[Object]
 144     }
 145 
 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++) {
 162                 if (i != j) {
 163                     assertNotEquals(vhs1[i], vhs1[j]);

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

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

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