< prev index next >

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

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 testng/othervm -Diters=10    -Xint                   VarHandleTestAccessString
  27  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessString
  28  * @run testng/othervm -Diters=20000                         VarHandleTestAccessString
  29  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessString
  30  */
  31 
  32 import org.testng.annotations.BeforeClass;
  33 import org.testng.annotations.DataProvider;
  34 import org.testng.annotations.Test;
  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.testng.Assert.*;
  43 
  44 public class VarHandleTestAccessString extends VarHandleBaseTest {


  45     static final String static_final_v = "foo";
  46 
  47     static String static_v;
  48 
  49     final String final_v = "foo";
  50 
  51     String v;
  52 
  53     static final String static_final_v2 = "foo";
  54 
  55     static String static_v2;
  56 
  57     final String final_v2 = "foo";
  58 
  59     String v2;
  60 
  61     VarHandle vhFinalField;
  62 
  63     VarHandle vhField;
  64 
  65     VarHandle vhStaticField;
  66 
  67     VarHandle vhStaticFinalField;
  68 
  69     VarHandle vhArray;
  70 
  71     VarHandle vhArrayObject;
  72 
  73     VarHandle[] allocate(boolean same) {
  74         List<VarHandle> vhs = new ArrayList<>();
  75 
  76         String postfix = same ? "" : "2";
  77         VarHandle vh;
  78         try {
  79             vh = MethodHandles.lookup().findVarHandle(
  80                     VarHandleTestAccessString.class, "final_v" + postfix, String.class);
  81             vhs.add(vh);
  82 
  83             vh = MethodHandles.lookup().findVarHandle(
  84                     VarHandleTestAccessString.class, "v" + postfix, String.class);
  85             vhs.add(vh);
  86 
  87             vh = MethodHandles.lookup().findStaticVarHandle(
  88                 VarHandleTestAccessString.class, "static_final_v" + postfix, String.class);
  89             vhs.add(vh);
  90 
  91             vh = MethodHandles.lookup().findStaticVarHandle(
  92                 VarHandleTestAccessString.class, "static_v" + postfix, String.class);
  93             vhs.add(vh);
  94 
  95             if (same) {
  96                 vh = MethodHandles.arrayElementVarHandle(String[].class);
  97             }
  98             else {
  99                 vh = MethodHandles.arrayElementVarHandle(int[].class);
 100             }
 101             vhs.add(vh);
 102         } catch (Exception e) {
 103             throw new InternalError(e);
 104         }
 105         return vhs.toArray(new VarHandle[0]);
 106     }
 107 
 108     @BeforeClass
 109     public void setup() throws Exception {
 110         vhFinalField = MethodHandles.lookup().findVarHandle(
 111                 VarHandleTestAccessString.class, "final_v", String.class);
 112 
 113         vhField = MethodHandles.lookup().findVarHandle(
 114                 VarHandleTestAccessString.class, "v", String.class);
 115 
 116         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 117             VarHandleTestAccessString.class, "static_final_v", String.class);
 118 
 119         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 120             VarHandleTestAccessString.class, "static_v", String.class);
 121 
 122         vhArray = MethodHandles.arrayElementVarHandle(String[].class);
 123         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);

 124     }
 125 
 126 
 127     @DataProvider
 128     public Object[][] varHandlesProvider() throws Exception {
 129         List<VarHandle> vhs = new ArrayList<>();
 130         vhs.add(vhField);
 131         vhs.add(vhStaticField);
 132         vhs.add(vhArray);
 133 
 134         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 135     }
 136 
 137     @Test
 138     public void testEquals() {
 139         VarHandle[] vhs1 = allocate(true);
 140         VarHandle[] vhs2 = allocate(true);
 141 
 142         for (int i = 0; i < vhs1.length; i++) {
 143             for (int j = 0; j < vhs1.length; j++) {

 188         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 189         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 190         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 191         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 192         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 193     }
 194 
 195 
 196     @DataProvider
 197     public Object[][] typesProvider() throws Exception {
 198         List<Object[]> types = new ArrayList<>();
 199         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessString.class)});
 200         types.add(new Object[] {vhStaticField, Arrays.asList()});
 201         types.add(new Object[] {vhArray, Arrays.asList(String[].class, int.class)});
 202 
 203         return types.stream().toArray(Object[][]::new);
 204     }
 205 
 206     @Test(dataProvider = "typesProvider")
 207     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 208         assertEquals(vh.varType(), String.class);
 209 
 210         assertEquals(vh.coordinateTypes(), pts);
 211 
 212         testTypes(vh);
 213     }
 214 
 215 
 216     @Test
 217     public void testLookupInstanceToStatic() {
 218         checkIAE("Lookup of static final field to instance final field", () -> {
 219             MethodHandles.lookup().findStaticVarHandle(
 220                     VarHandleTestAccessString.class, "final_v", String.class);
 221         });
 222 
 223         checkIAE("Lookup of static field to instance field", () -> {
 224             MethodHandles.lookup().findStaticVarHandle(
 225                     VarHandleTestAccessString.class, "v", String.class);
 226         });
 227     }
 228 
 229     @Test
 230     public void testLookupStaticToInstance() {
 231         checkIAE("Lookup of instance final field to static final field", () -> {
 232             MethodHandles.lookup().findVarHandle(
 233                 VarHandleTestAccessString.class, "static_final_v", String.class);
 234         });
 235 
 236         checkIAE("Lookup of instance field to static field", () -> {
 237             vhStaticField = MethodHandles.lookup().findVarHandle(
 238                 VarHandleTestAccessString.class, "static_v", String.class);
 239         });
 240     }
 241 
 242 
 243     @DataProvider
 244     public Object[][] accessTestCaseProvider() throws Exception {
 245         List<AccessTestCase<?>> cases = new ArrayList<>();
 246 
 247         cases.add(new VarHandleAccessTestCase("Instance final field",
 248                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 249         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 250                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 251                                               false));
 252 
 253         cases.add(new VarHandleAccessTestCase("Static final field",
 254                                               vhStaticFinalField, VarHandleTestAccessString::testStaticFinalField));
 255         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 256                                               vhStaticFinalField, VarHandleTestAccessString::testStaticFinalFieldUnsupported,
 257                                               false));
 258 

   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 // -- This file was mechanically generated: Do not edit! -- //
  25 
  26 /*
  27  * @test
  28  * @run testng/othervm -Diters=10    -Xint                   VarHandleTestAccessString
  29  * @run testng/othervm -Diters=20000 -XX:TieredStopAtLevel=1 VarHandleTestAccessString
  30  * @run testng/othervm -Diters=20000                         VarHandleTestAccessString
  31  * @run testng/othervm -Diters=20000 -XX:-TieredCompilation  VarHandleTestAccessString
  32  */
  33 
  34 import org.testng.annotations.BeforeClass;
  35 import org.testng.annotations.DataProvider;
  36 import org.testng.annotations.Test;
  37 
  38 import java.lang.invoke.MethodHandles;
  39 import java.lang.invoke.VarHandle;
  40 import java.util.ArrayList;
  41 import java.util.Arrays;
  42 import java.util.List;
  43 
  44 import static org.testng.Assert.*;
  45 
  46 public class VarHandleTestAccessString extends VarHandleBaseTest {
  47     static final Class<?> type = String.class;
  48 
  49     static final String static_final_v = "foo";
  50 
  51     static String static_v;
  52 
  53     final String final_v = "foo";
  54 
  55     String v;
  56 
  57     static final String static_final_v2 = "foo";
  58 
  59     static String static_v2;
  60 
  61     final String final_v2 = "foo";
  62 
  63     String v2;
  64 
  65     VarHandle vhFinalField;
  66 
  67     VarHandle vhField;
  68 
  69     VarHandle vhStaticField;
  70 
  71     VarHandle vhStaticFinalField;
  72 
  73     VarHandle vhArray;
  74 
  75     VarHandle vhArrayObject;
  76 
  77     VarHandle[] allocate(boolean same) {
  78         List<VarHandle> vhs = new ArrayList<>();
  79 
  80         String postfix = same ? "" : "2";
  81         VarHandle vh;
  82         try {
  83             vh = MethodHandles.lookup().findVarHandle(
  84                     VarHandleTestAccessString.class, "final_v" + postfix, type);
  85             vhs.add(vh);
  86 
  87             vh = MethodHandles.lookup().findVarHandle(
  88                     VarHandleTestAccessString.class, "v" + postfix, type);
  89             vhs.add(vh);
  90 
  91             vh = MethodHandles.lookup().findStaticVarHandle(
  92                 VarHandleTestAccessString.class, "static_final_v" + postfix, type);
  93             vhs.add(vh);
  94 
  95             vh = MethodHandles.lookup().findStaticVarHandle(
  96                 VarHandleTestAccessString.class, "static_v" + postfix, type);
  97             vhs.add(vh);
  98 
  99             if (same) {
 100                 vh = MethodHandles.arrayElementVarHandle(String[].class);
 101             }
 102             else {
 103                 vh = MethodHandles.arrayElementVarHandle(int[].class);
 104             }
 105             vhs.add(vh);
 106         } catch (Exception e) {
 107             throw new InternalError(e);
 108         }
 109         return vhs.toArray(new VarHandle[0]);
 110     }
 111 
 112     @BeforeClass
 113     public void setup() throws Exception {
 114         vhFinalField = MethodHandles.lookup().findVarHandle(
 115                 VarHandleTestAccessString.class, "final_v", type);
 116 
 117         vhField = MethodHandles.lookup().findVarHandle(
 118                 VarHandleTestAccessString.class, "v", type);
 119 
 120         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 121             VarHandleTestAccessString.class, "static_final_v", type);
 122 
 123         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 124             VarHandleTestAccessString.class, "static_v", type);
 125 
 126         vhArray = MethodHandles.arrayElementVarHandle(String[].class);
 127         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 128 
 129     }
 130 
 131 
 132     @DataProvider
 133     public Object[][] varHandlesProvider() throws Exception {
 134         List<VarHandle> vhs = new ArrayList<>();
 135         vhs.add(vhField);
 136         vhs.add(vhStaticField);
 137         vhs.add(vhArray);
 138 
 139         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 140     }
 141 
 142     @Test
 143     public void testEquals() {
 144         VarHandle[] vhs1 = allocate(true);
 145         VarHandle[] vhs2 = allocate(true);
 146 
 147         for (int i = 0; i < vhs1.length; i++) {
 148             for (int j = 0; j < vhs1.length; j++) {

 193         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_ACQUIRE));
 194         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_AND_RELEASE));
 195         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR));
 196         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_ACQUIRE));
 197         assertFalse(vh.isAccessModeSupported(VarHandle.AccessMode.GET_AND_BITWISE_XOR_RELEASE));
 198     }
 199 
 200 
 201     @DataProvider
 202     public Object[][] typesProvider() throws Exception {
 203         List<Object[]> types = new ArrayList<>();
 204         types.add(new Object[] {vhField, Arrays.asList(VarHandleTestAccessString.class)});
 205         types.add(new Object[] {vhStaticField, Arrays.asList()});
 206         types.add(new Object[] {vhArray, Arrays.asList(String[].class, int.class)});
 207 
 208         return types.stream().toArray(Object[][]::new);
 209     }
 210 
 211     @Test(dataProvider = "typesProvider")
 212     public void testTypes(VarHandle vh, List<Class<?>> pts) {
 213         assertEquals(vh.varType(), type);
 214 
 215         assertEquals(vh.coordinateTypes(), pts);
 216 
 217         testTypes(vh);
 218     }
 219 
 220 
 221     @Test
 222     public void testLookupInstanceToStatic() {
 223         checkIAE("Lookup of static final field to instance final field", () -> {
 224             MethodHandles.lookup().findStaticVarHandle(
 225                     VarHandleTestAccessString.class, "final_v", type);
 226         });
 227 
 228         checkIAE("Lookup of static field to instance field", () -> {
 229             MethodHandles.lookup().findStaticVarHandle(
 230                     VarHandleTestAccessString.class, "v", type);
 231         });
 232     }
 233 
 234     @Test
 235     public void testLookupStaticToInstance() {
 236         checkIAE("Lookup of instance final field to static final field", () -> {
 237             MethodHandles.lookup().findVarHandle(
 238                 VarHandleTestAccessString.class, "static_final_v", type);
 239         });
 240 
 241         checkIAE("Lookup of instance field to static field", () -> {
 242             vhStaticField = MethodHandles.lookup().findVarHandle(
 243                 VarHandleTestAccessString.class, "static_v", type);
 244         });
 245     }
 246 
 247 
 248     @DataProvider
 249     public Object[][] accessTestCaseProvider() throws Exception {
 250         List<AccessTestCase<?>> cases = new ArrayList<>();
 251 
 252         cases.add(new VarHandleAccessTestCase("Instance final field",
 253                                               vhFinalField, vh -> testInstanceFinalField(this, vh)));
 254         cases.add(new VarHandleAccessTestCase("Instance final field unsupported",
 255                                               vhFinalField, vh -> testInstanceFinalFieldUnsupported(this, vh),
 256                                               false));
 257 
 258         cases.add(new VarHandleAccessTestCase("Static final field",
 259                                               vhStaticFinalField, VarHandleTestAccessString::testStaticFinalField));
 260         cases.add(new VarHandleAccessTestCase("Static final field unsupported",
 261                                               vhStaticFinalField, VarHandleTestAccessString::testStaticFinalFieldUnsupported,
 262                                               false));
 263 
< prev index next >