< prev index next >

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

Print this page
rev 52786 : 8210031: implementation for JVM Constants API


  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 VarHandleTestAccess$Type$ extends VarHandleBaseTest {
  45     static final $type$ static_final_v = $value1$;
  46 
  47     static $type$ static_v;
  48 
  49     final $type$ final_v = $value1$;
  50 
  51     $type$ v;
  52 








  53     VarHandle vhFinalField;
  54 
  55     VarHandle vhField;
  56 
  57     VarHandle vhStaticField;
  58 
  59     VarHandle vhStaticFinalField;
  60 
  61     VarHandle vhArray;
  62 
  63 #if[String]
  64     VarHandle vhArrayObject;
  65 #end[String]
  66 







































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

























  96     }
  97 
  98     @Test(dataProvider = "varHandlesProvider")
  99     public void testIsAccessModeSupported(VarHandle vh) {
 100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 101         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 102         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 103         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 104         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 105         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 106         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 107         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 108 
 109 #if[CAS]
 110         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 111         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 112         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 113         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 114         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 115         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));




  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 VarHandleTestAccess$Type$ extends VarHandleBaseTest {
  45     static final $type$ static_final_v = $value1$;
  46 
  47     static $type$ static_v;
  48 
  49     final $type$ final_v = $value1$;
  50 
  51     $type$ v;
  52 
  53     static final $type$ static_final_v2 = $value1$;
  54 
  55     static $type$ static_v2;
  56 
  57     final $type$ final_v2 = $value1$;
  58 
  59     $type$ v2;
  60 
  61     VarHandle vhFinalField;
  62 
  63     VarHandle vhField;
  64 
  65     VarHandle vhStaticField;
  66 
  67     VarHandle vhStaticFinalField;
  68 
  69     VarHandle vhArray;
  70 
  71 #if[String]
  72     VarHandle vhArrayObject;
  73 #end[String]
  74 
  75     VarHandle[] allocate(boolean same) {
  76         List<VarHandle> vhs = new ArrayList<>();
  77 
  78         String postfix = same ? "" : "2";
  79         VarHandle vh;
  80         try {
  81             vh = MethodHandles.lookup().findVarHandle(
  82                     VarHandleTestAccess$Type$.class, "final_v" + postfix, $type$.class);
  83             vhs.add(vh);
  84 
  85             vh = MethodHandles.lookup().findVarHandle(
  86                     VarHandleTestAccess$Type$.class, "v" + postfix, $type$.class);
  87             vhs.add(vh);
  88 
  89             vh = MethodHandles.lookup().findStaticVarHandle(
  90                 VarHandleTestAccess$Type$.class, "static_final_v" + postfix, $type$.class);
  91             vhs.add(vh);
  92 
  93             vh = MethodHandles.lookup().findStaticVarHandle(
  94                 VarHandleTestAccess$Type$.class, "static_v" + postfix, $type$.class);
  95             vhs.add(vh);
  96 
  97             if (same) {
  98                 vh = MethodHandles.arrayElementVarHandle($type$[].class);
  99             }
 100             else {
 101 #if[String]
 102                 vh = MethodHandles.arrayElementVarHandle(int[].class);
 103 #else[String]
 104                 vh = MethodHandles.arrayElementVarHandle(String[].class);
 105 #end[String]
 106             }
 107             vhs.add(vh);
 108         } catch (Exception e) {
 109             throw new InternalError(e);
 110         }
 111         return vhs.toArray(new VarHandle[0]);
 112     }
 113 
 114     @BeforeClass
 115     public void setup() throws Exception {
 116         vhFinalField = MethodHandles.lookup().findVarHandle(
 117                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
 118 
 119         vhField = MethodHandles.lookup().findVarHandle(
 120                 VarHandleTestAccess$Type$.class, "v", $type$.class);
 121 
 122         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
 123             VarHandleTestAccess$Type$.class, "static_final_v", $type$.class);
 124 
 125         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
 126             VarHandleTestAccess$Type$.class, "static_v", $type$.class);
 127 
 128         vhArray = MethodHandles.arrayElementVarHandle($type$[].class);
 129 #if[String]
 130         vhArrayObject = MethodHandles.arrayElementVarHandle(Object[].class);
 131 #end[String]
 132     }
 133 
 134 
 135     @DataProvider
 136     public Object[][] varHandlesProvider() throws Exception {
 137         List<VarHandle> vhs = new ArrayList<>();
 138         vhs.add(vhField);
 139         vhs.add(vhStaticField);
 140         vhs.add(vhArray);
 141 
 142         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
 143     }
 144 
 145     @Test
 146     public void testEqualsAndHashCode() {
 147         VarHandle[] vhs1 = allocate(true);
 148         VarHandle[] vhs2 = allocate(true);
 149 
 150         for (int i = 0; i < vhs1.length; i++) {
 151             for (int j = 0; j < vhs1.length; j++) {
 152                 if (i == j) {
 153                     assertEquals(vhs1[i], vhs1[i]);
 154                     assertEquals(vhs1[i], vhs2[i]);
 155                     assertEquals(vhs1[i].hashCode(), vhs2[i].hashCode());
 156                 }
 157                 else {
 158                     assertNotEquals(vhs1[i], vhs1[j]);
 159                     assertNotEquals(vhs1[i], vhs2[j]);
 160                 }
 161             }
 162         }
 163 
 164         VarHandle[] vhs3 = allocate(false);
 165         for (int i = 0; i < vhs1.length; i++) {
 166             assertNotEquals(vhs1[i], vhs3[i]);
 167         }
 168     }
 169 
 170     @Test(dataProvider = "varHandlesProvider")
 171     public void testIsAccessModeSupported(VarHandle vh) {
 172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
 173         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET));
 174         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_VOLATILE));
 175         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_VOLATILE));
 176         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_ACQUIRE));
 177         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_RELEASE));
 178         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET_OPAQUE));
 179         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.SET_OPAQUE));
 180 
 181 #if[CAS]
 182         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_SET));
 183         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE));
 184         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_ACQUIRE));
 185         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.COMPARE_AND_EXCHANGE_RELEASE));
 186         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET_PLAIN));
 187         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.WEAK_COMPARE_AND_SET));


< prev index next >