< prev index next >

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

Print this page
rev 55900 : 8207257: Add VarHandle access test for value type declared in ref type
Reviewed-by: tbd


   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 #if[Value]
  25 /*
  26  * @test
  27  * @run testng/othervm -Diters=10    -XX:+EnableValhalla -Xint                   VarHandleTestAccess$Type$
  28  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  29  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla                         VarHandleTestAccess$Type$
  30  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla -XX:-TieredCompilation  VarHandleTestAccess$Type$
  31  */
  32 #else[Value]
  33 /*
  34  * @test
  35  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccess$Type$
  36  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  37  * @run testng/othervm -Diters=20000                         VarHandleTestAccess$Type$
  38  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  39  */
  40 #end[Value]
  41 
  42 import org.testng.annotations.BeforeClass;
  43 import org.testng.annotations.DataProvider;


  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[String]
  82     VarHandle vhArrayObject;
  83 #end[String]
  84 #if[Value]
  85     VarHandle vhValueTypeField;
  86 #end[Value]
  87 
  88     VarHandle[] allocate(boolean same) {
  89         List<VarHandle> vhs = new ArrayList<>();
  90 
  91         String postfix = same ? "" : "2";
  92         VarHandle vh;
  93         try {
  94             vh = MethodHandles.lookup().findVarHandle(
  95                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  96             vhs.add(vh);
  97 
  98             vh = MethodHandles.lookup().findVarHandle(
  99                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
 100             vhs.add(vh);
 101 
 102             vh = MethodHandles.lookup().findStaticVarHandle(
 103                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);


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


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


1929 
1930             checkIOOBE(() -> {
1931                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, $value1$);
1932             });
1933 
1934             checkIOOBE(() -> {
1935                 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, $value1$);
1936             });
1937 
1938             checkIOOBE(() -> {
1939                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, $value1$);
1940             });
1941 
1942             checkIOOBE(() -> {
1943                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, $value1$);
1944             });
1945 #end[Bitwise]
1946         }
1947     }
1948 
1949 #if[String]
1950     static void testArrayStoreException(VarHandle vh) throws Throwable {
1951         Object[] array = new $type$[10];
1952         Arrays.fill(array, $value1$);
1953         Object value = new Object();
1954 
1955         // Set
1956         checkASE(() -> {
1957             vh.set(array, 0, value);
1958         });
1959 
1960         // SetVolatile
1961         checkASE(() -> {
1962             vh.setVolatile(array, 0, value);
1963         });
1964 
1965         // SetOpaque
1966         checkASE(() -> {
1967             vh.setOpaque(array, 0, value);
1968         });
1969 


2010         // CompareAndExchangeRelease
2011         checkASE(() -> { // receiver reference class
2012             $type$ x = ($type$) vh.compareAndExchangeRelease(array, 0, $value1$, value);
2013         });
2014 
2015         // GetAndSet
2016         checkASE(() -> { // receiver reference class
2017             $type$ x = ($type$) vh.getAndSet(array, 0, value);
2018         });
2019 
2020         // GetAndSetAcquire
2021         checkASE(() -> { // receiver reference class
2022             $type$ x = ($type$) vh.getAndSetAcquire(array, 0, value);
2023         });
2024 
2025         // GetAndSetRelease
2026         checkASE(() -> { // receiver reference class
2027             $type$ x = ($type$) vh.getAndSetRelease(array, 0, value);
2028         });
2029     }
2030 #end[String]
2031 }
2032 


   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 #if[Value]
  27 /*
  28  * @test
  29  * @run testng/othervm -Diters=10    -XX:+EnableValhalla -Xint                   VarHandleTestAccess$Type$
  30  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  31  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla                         VarHandleTestAccess$Type$
  32  * @run testng/othervm -Diters=20000 -XX:+EnableValhalla -XX:-TieredCompilation  VarHandleTestAccess$Type$
  33  */
  34 #else[Value]
  35 /*
  36  * @test
  37  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccess$Type$
  38  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccess$Type$
  39  * @run testng/othervm -Diters=20000                         VarHandleTestAccess$Type$
  40  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccess$Type$
  41  */
  42 #end[Value]
  43 
  44 import org.testng.annotations.BeforeClass;
  45 import org.testng.annotations.DataProvider;


  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[Class]]
  84     VarHandle vhArrayObject;
  85 #end[Class]]
  86 #if[Value]
  87     VarHandle vhValueTypeField;
  88 #end[Value]
  89 
  90     VarHandle[] allocate(boolean same) {
  91         List<VarHandle> vhs = new ArrayList<>();
  92 
  93         String postfix = same ? "" : "2";
  94         VarHandle vh;
  95         try {
  96             vh = MethodHandles.lookup().findVarHandle(
  97                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  98             vhs.add(vh);
  99 
 100             vh = MethodHandles.lookup().findVarHandle(
 101                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
 102             vhs.add(vh);
 103 
 104             vh = MethodHandles.lookup().findStaticVarHandle(
 105                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);


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


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


1931 
1932             checkIOOBE(() -> {
1933                 $type$ o = ($type$) vh.getAndBitwiseAndRelease(array, ci, $value1$);
1934             });
1935 
1936             checkIOOBE(() -> {
1937                 $type$ o = ($type$) vh.getAndBitwiseXor(array, ci, $value1$);
1938             });
1939 
1940             checkIOOBE(() -> {
1941                 $type$ o = ($type$) vh.getAndBitwiseXorAcquire(array, ci, $value1$);
1942             });
1943 
1944             checkIOOBE(() -> {
1945                 $type$ o = ($type$) vh.getAndBitwiseXorRelease(array, ci, $value1$);
1946             });
1947 #end[Bitwise]
1948         }
1949     }
1950 
1951 #if[Class]]
1952     static void testArrayStoreException(VarHandle vh) throws Throwable {
1953         Object[] array = new $type$[10];
1954         Arrays.fill(array, $value1$);
1955         Object value = new Object();
1956 
1957         // Set
1958         checkASE(() -> {
1959             vh.set(array, 0, value);
1960         });
1961 
1962         // SetVolatile
1963         checkASE(() -> {
1964             vh.setVolatile(array, 0, value);
1965         });
1966 
1967         // SetOpaque
1968         checkASE(() -> {
1969             vh.setOpaque(array, 0, value);
1970         });
1971 


2012         // CompareAndExchangeRelease
2013         checkASE(() -> { // receiver reference class
2014             $type$ x = ($type$) vh.compareAndExchangeRelease(array, 0, $value1$, value);
2015         });
2016 
2017         // GetAndSet
2018         checkASE(() -> { // receiver reference class
2019             $type$ x = ($type$) vh.getAndSet(array, 0, value);
2020         });
2021 
2022         // GetAndSetAcquire
2023         checkASE(() -> { // receiver reference class
2024             $type$ x = ($type$) vh.getAndSetAcquire(array, 0, value);
2025         });
2026 
2027         // GetAndSetRelease
2028         checkASE(() -> { // receiver reference class
2029             $type$ x = ($type$) vh.getAndSetRelease(array, 0, value);
2030         });
2031     }
2032 #end[Class]]
2033 }
2034 
< prev index next >