< prev index next >

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

Print this page
rev 52865 : [mq]: 8210031


  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     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63     VarHandle vhArrayObject;
  64 



































  65     @BeforeClass
  66     public void setup() throws Exception {
  67         vhFinalField = MethodHandles.lookup().findVarHandle(
  68                 VarHandleTestAccessString.class, "final_v", String.class);
  69 
  70         vhField = MethodHandles.lookup().findVarHandle(
  71                 VarHandleTestAccessString.class, "v", String.class);
  72 
  73         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  74             VarHandleTestAccessString.class, "static_final_v", String.class);
  75 
  76         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  77             VarHandleTestAccessString.class, "static_v", String.class);
  78 
  79         vhArray = MethodHandles.arrayElementVarHandle(String[].class);
  80         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
  81     }
  82 
  83 
  84     @DataProvider
  85     public Object[][] varHandlesProvider() throws Exception {
  86         List<VarHandle> vhs = new ArrayList<>();
  87         vhs.add(vhField);
  88         vhs.add(vhStaticField);
  89         vhs.add(vhArray);
  90 
  91         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);

























  92     }
  93 
  94     @Test(dataProvider = "varHandlesProvider")
  95     public void testIsAccessModeSupported(VarHandle vh) {
  96         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
  97         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
  98         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
  99         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 101         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 102         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 103         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 104 
 105         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 106         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 107         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 108         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 109         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 110         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 111         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));




  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 testEqualsAndHashCode() {
 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++) {
 144                 if (i == j) {
 145                     assertEquals(vhs1[i], vhs1[i]);
 146                     assertEquals(vhs1[i], vhs2[i]);
 147                     assertEquals(vhs1[i].hashCode(), vhs2[i].hashCode());
 148                 }
 149                 else {
 150                     assertNotEquals(vhs1[i], vhs1[j]);
 151                     assertNotEquals(vhs1[i], vhs2[j]);
 152                 }
 153             }
 154         }
 155 
 156         VarHandle[] vhs3 = allocate(false);
 157         for (int i = 0; i < vhs1.length; i++) {
 158             assertNotEquals(vhs1[i], vhs3[i]);
 159         }
 160     }
 161 
 162     @Test(dataProvider = "varHandlesProvider")
 163     public void testIsAccessModeSupported(VarHandle vh) {
 164         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 165         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 166         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 167         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 168         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 169         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 170         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 171         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 172 
 173         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 175         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 176         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_ACQUIRE));


< prev index next >