< prev index next >

test/jdk/java/lang/invoke/VarHandles/VarHandleTestMethodTypeString.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  * @bug 8156486
  27  * @run testng/othervm VarHandleTestMethodTypeString
  28  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true VarHandleTestMethodTypeString
  29  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false VarHandleTestMethodTypeString
  30  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true VarHandleTestMethodTypeString
  31  */
  32 
  33 import org.testng.annotations.BeforeClass;
  34 import org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 
  37 import java.lang.invoke.MethodHandles;
  38 import java.lang.invoke.VarHandle;
  39 import java.util.ArrayList;
  40 import java.util.Arrays;
  41 import java.util.List;
  42 
  43 import static org.testng.Assert.*;
  44 
  45 import static java.lang.invoke.MethodType.*;
  46 
  47 public class VarHandleTestMethodTypeString extends VarHandleBaseTest {


  48     static final String static_final_v = "foo";
  49 
  50     static String static_v = "foo";
  51 
  52     final String final_v = "foo";
  53 
  54     String v = "foo";
  55 
  56     VarHandle vhFinalField;
  57 
  58     VarHandle vhField;
  59 
  60     VarHandle vhStaticField;
  61 
  62     VarHandle vhStaticFinalField;
  63 
  64     VarHandle vhArray;
  65 
  66     @BeforeClass
  67     public void setup() throws Exception {
  68         vhFinalField = MethodHandles.lookup().findVarHandle(
  69                 VarHandleTestMethodTypeString.class, "final_v", String.class);
  70 
  71         vhField = MethodHandles.lookup().findVarHandle(
  72                 VarHandleTestMethodTypeString.class, "v", String.class);
  73 
  74         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  75             VarHandleTestMethodTypeString.class, "static_final_v", String.class);
  76 
  77         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  78             VarHandleTestMethodTypeString.class, "static_v", String.class);
  79 
  80         vhArray = MethodHandles.arrayElementVarHandle(String[].class);
  81     }
  82 
  83     @DataProvider
  84     public Object[][] accessTestCaseProvider() throws Exception {
  85         List<AccessTestCase<?>> cases = new ArrayList<>();
  86 
  87         cases.add(new VarHandleAccessTestCase("Instance field",
  88                                               vhField, vh -> testInstanceFieldWrongMethodType(this, vh),
  89                                               false));
  90 
  91         cases.add(new VarHandleAccessTestCase("Static field",
  92                                               vhStaticField, VarHandleTestMethodTypeString::testStaticFieldWrongMethodType,
  93                                               false));
  94 
  95         cases.add(new VarHandleAccessTestCase("Array",
  96                                               vhArray, VarHandleTestMethodTypeString::testArrayWrongMethodType,
  97                                               false));
  98 

 632             Void r = (Void) vh.getAndSetRelease(recv, "foo");
 633         });
 634         checkWMTE(() -> { // primitive class
 635             boolean x = (boolean) vh.getAndSetRelease(recv, "foo");
 636         });
 637         // Incorrect arity
 638         checkWMTE(() -> { // 0
 639             String x = (String) vh.getAndSetRelease();
 640         });
 641         checkWMTE(() -> { // >
 642             String x = (String) vh.getAndSetRelease(recv, "foo", Void.class);
 643         });
 644 
 645 
 646     }
 647 
 648     static void testInstanceFieldWrongMethodType(VarHandleTestMethodTypeString recv, Handles hs) throws Throwable {
 649         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
 650             // Incorrect argument types
 651             checkNPE(() -> { // null receiver
 652                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class)).
 653                     invokeExact((VarHandleTestMethodTypeString) null);
 654             });
 655             hs.checkWMTEOrCCE(() -> { // receiver reference class
 656                 String x = (String) hs.get(am, methodType(String.class, Class.class)).
 657                     invokeExact(Void.class);
 658             });
 659             checkWMTE(() -> { // receiver primitive class
 660                 String x = (String) hs.get(am, methodType(String.class, int.class)).
 661                     invokeExact(0);
 662             });
 663             // Incorrect return type
 664             hs.checkWMTEOrCCE(() -> { // reference class
 665                 Void x = (Void) hs.get(am, methodType(Void.class, VarHandleTestMethodTypeString.class)).
 666                     invokeExact(recv);
 667             });
 668             checkWMTE(() -> { // primitive class
 669                 boolean x = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class)).
 670                     invokeExact(recv);
 671             });
 672             // Incorrect arity
 673             checkWMTE(() -> { // 0
 674                 String x = (String) hs.get(am, methodType(String.class)).
 675                     invokeExact();
 676             });
 677             checkWMTE(() -> { // >
 678                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, Class.class)).
 679                     invokeExact(recv, Void.class);
 680             });
 681         }
 682 
 683         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 684             // Incorrect argument types
 685             checkNPE(() -> { // null receiver
 686                 hs.get(am, methodType(void.class, VarHandleTestMethodTypeString.class, String.class)).
 687                     invokeExact((VarHandleTestMethodTypeString) null, "foo");
 688             });
 689             hs.checkWMTEOrCCE(() -> { // receiver reference class
 690                 hs.get(am, methodType(void.class, Class.class, String.class)).
 691                     invokeExact(Void.class, "foo");
 692             });
 693             hs.checkWMTEOrCCE(() -> { // value reference class
 694                 hs.get(am, methodType(void.class, VarHandleTestMethodTypeString.class, Class.class)).
 695                     invokeExact(recv, Void.class);
 696             });
 697             checkWMTE(() -> { // receiver primitive class
 698                 hs.get(am, methodType(void.class, int.class, String.class)).
 699                     invokeExact(0, "foo");
 700             });
 701             // Incorrect arity
 702             checkWMTE(() -> { // 0
 703                 hs.get(am, methodType(void.class)).
 704                     invokeExact();
 705             });
 706             checkWMTE(() -> { // >
 707                 hs.get(am, methodType(void.class, VarHandleTestMethodTypeString.class, String.class, Class.class)).
 708                     invokeExact(recv, "foo", Void.class);
 709             });
 710         }
 711 
 712         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 713             // Incorrect argument types
 714             checkNPE(() -> { // null receiver
 715                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, String.class, String.class)).
 716                     invokeExact((VarHandleTestMethodTypeString) null, "foo", "foo");
 717             });
 718             hs.checkWMTEOrCCE(() -> { // receiver reference class
 719                 boolean r = (boolean) hs.get(am, methodType(boolean.class, Class.class, String.class, String.class)).
 720                     invokeExact(Void.class, "foo", "foo");
 721             });
 722             hs.checkWMTEOrCCE(() -> { // expected reference class
 723                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, Class.class, String.class)).
 724                     invokeExact(recv, Void.class, "foo");
 725             });
 726             hs.checkWMTEOrCCE(() -> { // actual reference class
 727                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, String.class, Class.class)).
 728                     invokeExact(recv, "foo", Void.class);
 729             });
 730             checkWMTE(() -> { // receiver primitive class
 731                 boolean r = (boolean) hs.get(am, methodType(boolean.class, int.class , String.class, String.class)).
 732                     invokeExact(0, "foo", "foo");
 733             });
 734             // Incorrect arity
 735             checkWMTE(() -> { // 0
 736                 boolean r = (boolean) hs.get(am, methodType(boolean.class)).
 737                     invokeExact();
 738             });
 739             checkWMTE(() -> { // >
 740                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, String.class, String.class, Class.class)).
 741                     invokeExact(recv, "foo", "foo", Void.class);
 742             });
 743         }
 744 
 745         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 746             checkNPE(() -> { // null receiver
 747                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, String.class, String.class)).
 748                     invokeExact((VarHandleTestMethodTypeString) null, "foo", "foo");
 749             });
 750             hs.checkWMTEOrCCE(() -> { // receiver reference class
 751                 String x = (String) hs.get(am, methodType(String.class, Class.class, String.class, String.class)).
 752                     invokeExact(Void.class, "foo", "foo");
 753             });
 754             hs.checkWMTEOrCCE(() -> { // expected reference class
 755                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, Class.class, String.class)).
 756                     invokeExact(recv, Void.class, "foo");
 757             });
 758             hs.checkWMTEOrCCE(() -> { // actual reference class
 759                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, String.class, Class.class)).
 760                     invokeExact(recv, "foo", Void.class);
 761             });
 762             checkWMTE(() -> { // reciever primitive class
 763                 String x = (String) hs.get(am, methodType(String.class, int.class , String.class, String.class)).
 764                     invokeExact(0, "foo", "foo");
 765             });
 766             // Incorrect return type
 767             hs.checkWMTEOrCCE(() -> { // reference class
 768                 Void r = (Void) hs.get(am, methodType(Void.class, VarHandleTestMethodTypeString.class , String.class, String.class)).
 769                     invokeExact(recv, "foo", "foo");
 770             });
 771             checkWMTE(() -> { // primitive class
 772                 boolean x = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class , String.class, String.class)).
 773                     invokeExact(recv, "foo", "foo");
 774             });
 775             // Incorrect arity
 776             checkWMTE(() -> { // 0
 777                 String x = (String) hs.get(am, methodType(String.class)).
 778                     invokeExact();
 779             });
 780             checkWMTE(() -> { // >
 781                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, String.class, String.class, Class.class)).
 782                     invokeExact(recv, "foo", "foo", Void.class);
 783             });
 784         }
 785 
 786         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 787             checkNPE(() -> { // null receiver
 788                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, String.class)).
 789                     invokeExact((VarHandleTestMethodTypeString) null, "foo");
 790             });
 791             hs.checkWMTEOrCCE(() -> { // receiver reference class
 792                 String x = (String) hs.get(am, methodType(String.class, Class.class, String.class)).
 793                     invokeExact(Void.class, "foo");
 794             });
 795             hs.checkWMTEOrCCE(() -> { // value reference class
 796                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, Class.class)).
 797                     invokeExact(recv, Void.class);
 798             });
 799             checkWMTE(() -> { // reciever primitive class
 800                 String x = (String) hs.get(am, methodType(String.class, int.class, String.class)).
 801                     invokeExact(0, "foo");
 802             });
 803             // Incorrect return type
 804             hs.checkWMTEOrCCE(() -> { // reference class
 805                 Void r = (Void) hs.get(am, methodType(Void.class, VarHandleTestMethodTypeString.class, String.class)).
 806                     invokeExact(recv, "foo");
 807             });
 808             checkWMTE(() -> { // primitive class
 809                 boolean x = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, String.class)).
 810                     invokeExact(recv, "foo");
 811             });
 812             // Incorrect arity
 813             checkWMTE(() -> { // 0
 814                 String x = (String) hs.get(am, methodType(String.class)).
 815                     invokeExact();
 816             });
 817             checkWMTE(() -> { // >
 818                 String x = (String) hs.get(am, methodType(String.class, VarHandleTestMethodTypeString.class, String.class)).
 819                     invokeExact(recv, "foo", Void.class);
 820             });
 821         }
 822 
 823 
 824     }
 825 
 826 
 827     static void testStaticFieldWrongMethodType(VarHandle vh) throws Throwable {
 828         // Get
 829         // Incorrect return type
 830         checkCCE(() -> { // reference class
 831             Void x = (Void) vh.get();
 832         });
 833         checkWMTE(() -> { // primitive class
 834             boolean x = (boolean) vh.get();
 835         });
 836         // Incorrect arity
 837         checkWMTE(() -> { // >
 838             String x = (String) vh.get(Void.class);

1170                     invokeExact();
1171             });
1172             // Incorrect arity
1173             checkWMTE(() -> { // >
1174                 String x = (String) hs.get(am, methodType(Class.class)).
1175                     invokeExact(Void.class);
1176             });
1177         }
1178 
1179         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1180             hs.checkWMTEOrCCE(() -> { // value reference class
1181                 hs.get(am, methodType(void.class, Class.class)).
1182                     invokeExact(Void.class);
1183             });
1184             // Incorrect arity
1185             checkWMTE(() -> { // 0
1186                 hs.get(am, methodType(void.class)).
1187                     invokeExact();
1188             });
1189             checkWMTE(() -> { // >
1190                 hs.get(am, methodType(void.class, String.class, Class.class)).
1191                     invokeExact("foo", Void.class);
1192             });
1193         }
1194         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1195             // Incorrect argument types
1196             hs.checkWMTEOrCCE(() -> { // expected reference class
1197                 boolean r = (boolean) hs.get(am, methodType(boolean.class, Class.class, String.class)).
1198                     invokeExact(Void.class, "foo");
1199             });
1200             hs.checkWMTEOrCCE(() -> { // actual reference class
1201                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String.class, Class.class)).
1202                     invokeExact("foo", Void.class);
1203             });
1204             // Incorrect arity
1205             checkWMTE(() -> { // 0
1206                 boolean r = (boolean) hs.get(am, methodType(boolean.class)).
1207                     invokeExact();
1208             });
1209             checkWMTE(() -> { // >
1210                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String.class, String.class, Class.class)).
1211                     invokeExact("foo", "foo", Void.class);
1212             });
1213         }
1214 
1215         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1216             // Incorrect argument types
1217             hs.checkWMTEOrCCE(() -> { // expected reference class
1218                 String x = (String) hs.get(am, methodType(String.class, Class.class, String.class)).
1219                     invokeExact(Void.class, "foo");
1220             });
1221             hs.checkWMTEOrCCE(() -> { // actual reference class
1222                 String x = (String) hs.get(am, methodType(String.class, String.class, Class.class)).
1223                     invokeExact("foo", Void.class);
1224             });
1225             // Incorrect return type
1226             hs.checkWMTEOrCCE(() -> { // reference class
1227                 Void r = (Void) hs.get(am, methodType(Void.class, String.class, String.class)).
1228                     invokeExact("foo", "foo");
1229             });
1230             checkWMTE(() -> { // primitive class
1231                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String.class, String.class)).
1232                     invokeExact("foo", "foo");
1233             });
1234             // Incorrect arity
1235             checkWMTE(() -> { // 0
1236                 String x = (String) hs.get(am, methodType(String.class)).
1237                     invokeExact();
1238             });
1239             checkWMTE(() -> { // >
1240                 String x = (String) hs.get(am, methodType(String.class, String.class, String.class, Class.class)).
1241                     invokeExact("foo", "foo", Void.class);
1242             });
1243         }
1244 
1245         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
1246             // Incorrect argument types
1247             hs.checkWMTEOrCCE(() -> { // value reference class
1248                 String x = (String) hs.get(am, methodType(String.class, Class.class)).
1249                     invokeExact(Void.class);
1250             });
1251             // Incorrect return type
1252             hs.checkWMTEOrCCE(() -> { // reference class
1253                 Void r = (Void) hs.get(am, methodType(Void.class, String.class)).
1254                     invokeExact("foo");
1255             });
1256             checkWMTE(() -> { // primitive class
1257                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String.class)).
1258                     invokeExact("foo");
1259             });
1260             // Incorrect arity
1261             checkWMTE(() -> { // 0
1262                 String x = (String) hs.get(am, methodType(String.class)).
1263                     invokeExact();
1264             });
1265             checkWMTE(() -> { // >
1266                 String x = (String) hs.get(am, methodType(String.class, String.class, Class.class)).
1267                     invokeExact("foo", Void.class);
1268             });
1269         }
1270 
1271 
1272     }
1273 
1274 
1275     static void testArrayWrongMethodType(VarHandle vh) throws Throwable {
1276         String[] array = new String[10];
1277         Arrays.fill(array, "foo");
1278 
1279         // Get
1280         // Incorrect argument types
1281         checkNPE(() -> { // null array
1282             String x = (String) vh.get(null, 0);
1283         });
1284         checkCCE(() -> { // array reference class
1285             String x = (String) vh.get(Void.class, 0);
1286         });

1844             boolean x = (boolean) vh.getAndSetRelease(array, 0, "foo");
1845         });
1846         // Incorrect arity
1847         checkWMTE(() -> { // 0
1848             String x = (String) vh.getAndSetRelease();
1849         });
1850         checkWMTE(() -> { // >
1851             String x = (String) vh.getAndSetRelease(array, 0, "foo", Void.class);
1852         });
1853 
1854 
1855     }
1856 
1857     static void testArrayWrongMethodType(Handles hs) throws Throwable {
1858         String[] array = new String[10];
1859         Arrays.fill(array, "foo");
1860 
1861         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
1862             // Incorrect argument types
1863             checkNPE(() -> { // null array
1864                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class)).
1865                     invokeExact((String[]) null, 0);
1866             });
1867             hs.checkWMTEOrCCE(() -> { // array reference class
1868                 String x = (String) hs.get(am, methodType(String.class, Class.class, int.class)).
1869                     invokeExact(Void.class, 0);
1870             });
1871             checkWMTE(() -> { // array primitive class
1872                 String x = (String) hs.get(am, methodType(String.class, int.class, int.class)).
1873                     invokeExact(0, 0);
1874             });
1875             checkWMTE(() -> { // index reference class
1876                 String x = (String) hs.get(am, methodType(String.class, String[].class, Class.class)).
1877                     invokeExact(array, Void.class);
1878             });
1879             // Incorrect return type
1880             hs.checkWMTEOrCCE(() -> { // reference class
1881                 Void x = (Void) hs.get(am, methodType(Void.class, String[].class, int.class)).
1882                     invokeExact(array, 0);
1883             });
1884             checkWMTE(() -> { // primitive class
1885                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class)).
1886                     invokeExact(array, 0);
1887             });
1888             // Incorrect arity
1889             checkWMTE(() -> { // 0
1890                 String x = (String) hs.get(am, methodType(String.class)).
1891                     invokeExact();
1892             });
1893             checkWMTE(() -> { // >
1894                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, Class.class)).
1895                     invokeExact(array, 0, Void.class);
1896             });
1897         }
1898 
1899         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1900             // Incorrect argument types
1901             checkNPE(() -> { // null array
1902                 hs.get(am, methodType(void.class, String[].class, int.class, String.class)).
1903                     invokeExact((String[]) null, 0, "foo");
1904             });
1905             hs.checkWMTEOrCCE(() -> { // array reference class
1906                 hs.get(am, methodType(void.class, Class.class, int.class, String.class)).
1907                     invokeExact(Void.class, 0, "foo");
1908             });
1909             hs.checkWMTEOrCCE(() -> { // value reference class
1910                 hs.get(am, methodType(void.class, String[].class, int.class, Class.class)).
1911                     invokeExact(array, 0, Void.class);
1912             });
1913             checkWMTE(() -> { // receiver primitive class
1914                 hs.get(am, methodType(void.class, int.class, int.class, String.class)).
1915                     invokeExact(0, 0, "foo");
1916             });
1917             checkWMTE(() -> { // index reference class
1918                 hs.get(am, methodType(void.class, String[].class, Class.class, String.class)).
1919                     invokeExact(array, Void.class, "foo");
1920             });
1921             // Incorrect arity
1922             checkWMTE(() -> { // 0
1923                 hs.get(am, methodType(void.class)).
1924                     invokeExact();
1925             });
1926             checkWMTE(() -> { // >
1927                 hs.get(am, methodType(void.class, String[].class, int.class, Class.class)).
1928                     invokeExact(array, 0, "foo", Void.class);
1929             });
1930         }
1931         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1932             // Incorrect argument types
1933             checkNPE(() -> { // null receiver
1934                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, String.class, String.class)).
1935                     invokeExact((String[]) null, 0, "foo", "foo");
1936             });
1937             hs.checkWMTEOrCCE(() -> { // receiver reference class
1938                 boolean r = (boolean) hs.get(am, methodType(boolean.class, Class.class, int.class, String.class, String.class)).
1939                     invokeExact(Void.class, 0, "foo", "foo");
1940             });
1941             hs.checkWMTEOrCCE(() -> { // expected reference class
1942                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, Class.class, String.class)).
1943                     invokeExact(array, 0, Void.class, "foo");
1944             });
1945             hs.checkWMTEOrCCE(() -> { // actual reference class
1946                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, String.class, Class.class)).
1947                     invokeExact(array, 0, "foo", Void.class);
1948             });
1949             checkWMTE(() -> { // receiver primitive class
1950                 boolean r = (boolean) hs.get(am, methodType(boolean.class, int.class, int.class, String.class, String.class)).
1951                     invokeExact(0, 0, "foo", "foo");
1952             });
1953             checkWMTE(() -> { // index reference class
1954                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, Class.class, String.class, String.class)).
1955                     invokeExact(array, Void.class, "foo", "foo");
1956             });
1957             // Incorrect arity
1958             checkWMTE(() -> { // 0
1959                 boolean r = (boolean) hs.get(am, methodType(boolean.class)).
1960                     invokeExact();
1961             });
1962             checkWMTE(() -> { // >
1963                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, String.class, String.class, Class.class)).
1964                     invokeExact(array, 0, "foo", "foo", Void.class);
1965             });
1966         }
1967 
1968         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1969             // Incorrect argument types
1970             checkNPE(() -> { // null receiver
1971                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, String.class, String.class)).
1972                     invokeExact((String[]) null, 0, "foo", "foo");
1973             });
1974             hs.checkWMTEOrCCE(() -> { // array reference class
1975                 String x = (String) hs.get(am, methodType(String.class, Class.class, int.class, String.class, String.class)).
1976                     invokeExact(Void.class, 0, "foo", "foo");
1977             });
1978             hs.checkWMTEOrCCE(() -> { // expected reference class
1979                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, Class.class, String.class)).
1980                     invokeExact(array, 0, Void.class, "foo");
1981             });
1982             hs.checkWMTEOrCCE(() -> { // actual reference class
1983                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, String.class, Class.class)).
1984                     invokeExact(array, 0, "foo", Void.class);
1985             });
1986             checkWMTE(() -> { // array primitive class
1987                 String x = (String) hs.get(am, methodType(String.class, int.class, int.class, String.class, String.class)).
1988                     invokeExact(0, 0, "foo", "foo");
1989             });
1990             checkWMTE(() -> { // index reference class
1991                 String x = (String) hs.get(am, methodType(String.class, String[].class, Class.class, String.class, String.class)).
1992                     invokeExact(array, Void.class, "foo", "foo");
1993             });
1994             // Incorrect return type
1995             hs.checkWMTEOrCCE(() -> { // reference class
1996                 Void r = (Void) hs.get(am, methodType(Void.class, String[].class, int.class, String.class, String.class)).
1997                     invokeExact(array, 0, "foo", "foo");
1998             });
1999             checkWMTE(() -> { // primitive class
2000                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, String.class, String.class)).
2001                     invokeExact(array, 0, "foo", "foo");
2002             });
2003             // Incorrect arity
2004             checkWMTE(() -> { // 0
2005                 String x = (String) hs.get(am, methodType(String.class)).
2006                     invokeExact();
2007             });
2008             checkWMTE(() -> { // >
2009                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, String.class, String.class, Class.class)).
2010                     invokeExact(array, 0, "foo", "foo", Void.class);
2011             });
2012         }
2013 
2014         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
2015             // Incorrect argument types
2016             checkNPE(() -> { // null array
2017                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, String.class)).
2018                     invokeExact((String[]) null, 0, "foo");
2019             });
2020             hs.checkWMTEOrCCE(() -> { // array reference class
2021                 String x = (String) hs.get(am, methodType(String.class, Class.class, int.class, String.class)).
2022                     invokeExact(Void.class, 0, "foo");
2023             });
2024             hs.checkWMTEOrCCE(() -> { // value reference class
2025                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, Class.class)).
2026                     invokeExact(array, 0, Void.class);
2027             });
2028             checkWMTE(() -> { // array primitive class
2029                 String x = (String) hs.get(am, methodType(String.class, int.class, int.class, String.class)).
2030                     invokeExact(0, 0, "foo");
2031             });
2032             checkWMTE(() -> { // index reference class
2033                 String x = (String) hs.get(am, methodType(String.class, String[].class, Class.class, String.class)).
2034                     invokeExact(array, Void.class, "foo");
2035             });
2036             // Incorrect return type
2037             hs.checkWMTEOrCCE(() -> { // reference class
2038                 Void r = (Void) hs.get(am, methodType(Void.class, String[].class, int.class, String.class)).
2039                     invokeExact(array, 0, "foo");
2040             });
2041             checkWMTE(() -> { // primitive class
2042                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, String.class)).
2043                     invokeExact(array, 0, "foo");
2044             });
2045             // Incorrect arity
2046             checkWMTE(() -> { // 0
2047                 String x = (String) hs.get(am, methodType(String.class)).
2048                     invokeExact();
2049             });
2050             checkWMTE(() -> { // >
2051                 String x = (String) hs.get(am, methodType(String.class, String[].class, int.class, String.class, Class.class)).
2052                     invokeExact(array, 0, "foo", Void.class);
2053             });
2054         }
2055 
2056 
2057     }
2058 }

   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  * @bug 8156486
  29  * @run testng/othervm VarHandleTestMethodTypeString
  30  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=true -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true VarHandleTestMethodTypeString
  31  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=false VarHandleTestMethodTypeString
  32  * @run testng/othervm -Djava.lang.invoke.VarHandle.VAR_HANDLE_GUARDS=false -Djava.lang.invoke.VarHandle.VAR_HANDLE_IDENTITY_ADAPT=true VarHandleTestMethodTypeString
  33  */
  34 
  35 import org.testng.annotations.BeforeClass;
  36 import org.testng.annotations.DataProvider;
  37 import org.testng.annotations.Test;
  38 
  39 import java.lang.invoke.MethodHandles;
  40 import java.lang.invoke.VarHandle;
  41 import java.util.ArrayList;
  42 import java.util.Arrays;
  43 import java.util.List;
  44 
  45 import static org.testng.Assert.*;
  46 
  47 import static java.lang.invoke.MethodType.*;
  48 
  49 public class VarHandleTestMethodTypeString extends VarHandleBaseTest {
  50     static final Class<?> type = String.class;
  51 
  52     static final String static_final_v = "foo";
  53 
  54     static String static_v = "foo";
  55 
  56     final String final_v = "foo";
  57 
  58     String v = "foo";
  59 
  60     VarHandle vhFinalField;
  61 
  62     VarHandle vhField;
  63 
  64     VarHandle vhStaticField;
  65 
  66     VarHandle vhStaticFinalField;
  67 
  68     VarHandle vhArray;
  69 
  70     @BeforeClass
  71     public void setup() throws Exception {
  72         vhFinalField = MethodHandles.lookup().findVarHandle(
  73                 VarHandleTestMethodTypeString.class, "final_v", type);
  74 
  75         vhField = MethodHandles.lookup().findVarHandle(
  76                 VarHandleTestMethodTypeString.class, "v", type);
  77 
  78         vhStaticFinalField = MethodHandles.lookup().findStaticVarHandle(
  79             VarHandleTestMethodTypeString.class, "static_final_v", type);
  80 
  81         vhStaticField = MethodHandles.lookup().findStaticVarHandle(
  82             VarHandleTestMethodTypeString.class, "static_v", type);
  83 
  84         vhArray = MethodHandles.arrayElementVarHandle(String[].class);
  85     }
  86 
  87     @DataProvider
  88     public Object[][] accessTestCaseProvider() throws Exception {
  89         List<AccessTestCase<?>> cases = new ArrayList<>();
  90 
  91         cases.add(new VarHandleAccessTestCase("Instance field",
  92                                               vhField, vh -> testInstanceFieldWrongMethodType(this, vh),
  93                                               false));
  94 
  95         cases.add(new VarHandleAccessTestCase("Static field",
  96                                               vhStaticField, VarHandleTestMethodTypeString::testStaticFieldWrongMethodType,
  97                                               false));
  98 
  99         cases.add(new VarHandleAccessTestCase("Array",
 100                                               vhArray, VarHandleTestMethodTypeString::testArrayWrongMethodType,
 101                                               false));
 102 

 636             Void r = (Void) vh.getAndSetRelease(recv, "foo");
 637         });
 638         checkWMTE(() -> { // primitive class
 639             boolean x = (boolean) vh.getAndSetRelease(recv, "foo");
 640         });
 641         // Incorrect arity
 642         checkWMTE(() -> { // 0
 643             String x = (String) vh.getAndSetRelease();
 644         });
 645         checkWMTE(() -> { // >
 646             String x = (String) vh.getAndSetRelease(recv, "foo", Void.class);
 647         });
 648 
 649 
 650     }
 651 
 652     static void testInstanceFieldWrongMethodType(VarHandleTestMethodTypeString recv, Handles hs) throws Throwable {
 653         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
 654             // Incorrect argument types
 655             checkNPE(() -> { // null receiver
 656                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class)).
 657                     invokeExact((VarHandleTestMethodTypeString) null);
 658             });
 659             hs.checkWMTEOrCCE(() -> { // receiver reference class
 660                 String x = (String) hs.get(am, methodType(type, Class.class)).
 661                     invokeExact(Void.class);
 662             });
 663             checkWMTE(() -> { // receiver primitive class
 664                 String x = (String) hs.get(am, methodType(type, int.class)).
 665                     invokeExact(0);
 666             });
 667             // Incorrect return type
 668             hs.checkWMTEOrCCE(() -> { // reference class
 669                 Void x = (Void) hs.get(am, methodType(Void.class, VarHandleTestMethodTypeString.class)).
 670                     invokeExact(recv);
 671             });
 672             checkWMTE(() -> { // primitive class
 673                 boolean x = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class)).
 674                     invokeExact(recv);
 675             });
 676             // Incorrect arity
 677             checkWMTE(() -> { // 0
 678                 String x = (String) hs.get(am, methodType(type)).
 679                     invokeExact();
 680             });
 681             checkWMTE(() -> { // >
 682                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, Class.class)).
 683                     invokeExact(recv, Void.class);
 684             });
 685         }
 686 
 687         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
 688             // Incorrect argument types
 689             checkNPE(() -> { // null receiver
 690                 hs.get(am, methodType(void.class, VarHandleTestMethodTypeString.class, type)).
 691                     invokeExact((VarHandleTestMethodTypeString) null, "foo");
 692             });
 693             hs.checkWMTEOrCCE(() -> { // receiver reference class
 694                 hs.get(am, methodType(void.class, Class.class, type)).
 695                     invokeExact(Void.class, "foo");
 696             });
 697             hs.checkWMTEOrCCE(() -> { // value reference class
 698                 hs.get(am, methodType(void.class, VarHandleTestMethodTypeString.class, Class.class)).
 699                     invokeExact(recv, Void.class);
 700             });
 701             checkWMTE(() -> { // receiver primitive class
 702                 hs.get(am, methodType(void.class, int.class, type)).
 703                     invokeExact(0, "foo");
 704             });
 705             // Incorrect arity
 706             checkWMTE(() -> { // 0
 707                 hs.get(am, methodType(void.class)).
 708                     invokeExact();
 709             });
 710             checkWMTE(() -> { // >
 711                 hs.get(am, methodType(void.class, VarHandleTestMethodTypeString.class, type, Class.class)).
 712                     invokeExact(recv, "foo", Void.class);
 713             });
 714         }
 715 
 716         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
 717             // Incorrect argument types
 718             checkNPE(() -> { // null receiver
 719                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, type, type)).
 720                     invokeExact((VarHandleTestMethodTypeString) null, "foo", "foo");
 721             });
 722             hs.checkWMTEOrCCE(() -> { // receiver reference class
 723                 boolean r = (boolean) hs.get(am, methodType(boolean.class, Class.class, type, type)).
 724                     invokeExact(Void.class, "foo", "foo");
 725             });
 726             hs.checkWMTEOrCCE(() -> { // expected reference class
 727                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, Class.class, type)).
 728                     invokeExact(recv, Void.class, "foo");
 729             });
 730             hs.checkWMTEOrCCE(() -> { // actual reference class
 731                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, type, Class.class)).
 732                     invokeExact(recv, "foo", Void.class);
 733             });
 734             checkWMTE(() -> { // receiver primitive class
 735                 boolean r = (boolean) hs.get(am, methodType(boolean.class, int.class , type, type)).
 736                     invokeExact(0, "foo", "foo");
 737             });
 738             // Incorrect arity
 739             checkWMTE(() -> { // 0
 740                 boolean r = (boolean) hs.get(am, methodType(boolean.class)).
 741                     invokeExact();
 742             });
 743             checkWMTE(() -> { // >
 744                 boolean r = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, type, type, Class.class)).
 745                     invokeExact(recv, "foo", "foo", Void.class);
 746             });
 747         }
 748 
 749         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
 750             checkNPE(() -> { // null receiver
 751                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, type, type)).
 752                     invokeExact((VarHandleTestMethodTypeString) null, "foo", "foo");
 753             });
 754             hs.checkWMTEOrCCE(() -> { // receiver reference class
 755                 String x = (String) hs.get(am, methodType(type, Class.class, type, type)).
 756                     invokeExact(Void.class, "foo", "foo");
 757             });
 758             hs.checkWMTEOrCCE(() -> { // expected reference class
 759                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, Class.class, type)).
 760                     invokeExact(recv, Void.class, "foo");
 761             });
 762             hs.checkWMTEOrCCE(() -> { // actual reference class
 763                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, type, Class.class)).
 764                     invokeExact(recv, "foo", Void.class);
 765             });
 766             checkWMTE(() -> { // reciever primitive class
 767                 String x = (String) hs.get(am, methodType(type, int.class , type, type)).
 768                     invokeExact(0, "foo", "foo");
 769             });
 770             // Incorrect return type
 771             hs.checkWMTEOrCCE(() -> { // reference class
 772                 Void r = (Void) hs.get(am, methodType(Void.class, VarHandleTestMethodTypeString.class , type, type)).
 773                     invokeExact(recv, "foo", "foo");
 774             });
 775             checkWMTE(() -> { // primitive class
 776                 boolean x = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class , type, type)).
 777                     invokeExact(recv, "foo", "foo");
 778             });
 779             // Incorrect arity
 780             checkWMTE(() -> { // 0
 781                 String x = (String) hs.get(am, methodType(type)).
 782                     invokeExact();
 783             });
 784             checkWMTE(() -> { // >
 785                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, type, type, Class.class)).
 786                     invokeExact(recv, "foo", "foo", Void.class);
 787             });
 788         }
 789 
 790         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
 791             checkNPE(() -> { // null receiver
 792                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, type)).
 793                     invokeExact((VarHandleTestMethodTypeString) null, "foo");
 794             });
 795             hs.checkWMTEOrCCE(() -> { // receiver reference class
 796                 String x = (String) hs.get(am, methodType(type, Class.class, type)).
 797                     invokeExact(Void.class, "foo");
 798             });
 799             hs.checkWMTEOrCCE(() -> { // value reference class
 800                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, Class.class)).
 801                     invokeExact(recv, Void.class);
 802             });
 803             checkWMTE(() -> { // reciever primitive class
 804                 String x = (String) hs.get(am, methodType(type, int.class, type)).
 805                     invokeExact(0, "foo");
 806             });
 807             // Incorrect return type
 808             hs.checkWMTEOrCCE(() -> { // reference class
 809                 Void r = (Void) hs.get(am, methodType(Void.class, VarHandleTestMethodTypeString.class, type)).
 810                     invokeExact(recv, "foo");
 811             });
 812             checkWMTE(() -> { // primitive class
 813                 boolean x = (boolean) hs.get(am, methodType(boolean.class, VarHandleTestMethodTypeString.class, type)).
 814                     invokeExact(recv, "foo");
 815             });
 816             // Incorrect arity
 817             checkWMTE(() -> { // 0
 818                 String x = (String) hs.get(am, methodType(type)).
 819                     invokeExact();
 820             });
 821             checkWMTE(() -> { // >
 822                 String x = (String) hs.get(am, methodType(type, VarHandleTestMethodTypeString.class, type)).
 823                     invokeExact(recv, "foo", Void.class);
 824             });
 825         }
 826 
 827 
 828     }
 829 
 830 
 831     static void testStaticFieldWrongMethodType(VarHandle vh) throws Throwable {
 832         // Get
 833         // Incorrect return type
 834         checkCCE(() -> { // reference class
 835             Void x = (Void) vh.get();
 836         });
 837         checkWMTE(() -> { // primitive class
 838             boolean x = (boolean) vh.get();
 839         });
 840         // Incorrect arity
 841         checkWMTE(() -> { // >
 842             String x = (String) vh.get(Void.class);

1174                     invokeExact();
1175             });
1176             // Incorrect arity
1177             checkWMTE(() -> { // >
1178                 String x = (String) hs.get(am, methodType(Class.class)).
1179                     invokeExact(Void.class);
1180             });
1181         }
1182 
1183         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1184             hs.checkWMTEOrCCE(() -> { // value reference class
1185                 hs.get(am, methodType(void.class, Class.class)).
1186                     invokeExact(Void.class);
1187             });
1188             // Incorrect arity
1189             checkWMTE(() -> { // 0
1190                 hs.get(am, methodType(void.class)).
1191                     invokeExact();
1192             });
1193             checkWMTE(() -> { // >
1194                 hs.get(am, methodType(void.class, type, Class.class)).
1195                     invokeExact("foo", Void.class);
1196             });
1197         }
1198         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1199             // Incorrect argument types
1200             hs.checkWMTEOrCCE(() -> { // expected reference class
1201                 boolean r = (boolean) hs.get(am, methodType(boolean.class, Class.class, type)).
1202                     invokeExact(Void.class, "foo");
1203             });
1204             hs.checkWMTEOrCCE(() -> { // actual reference class
1205                 boolean r = (boolean) hs.get(am, methodType(boolean.class, type, Class.class)).
1206                     invokeExact("foo", Void.class);
1207             });
1208             // Incorrect arity
1209             checkWMTE(() -> { // 0
1210                 boolean r = (boolean) hs.get(am, methodType(boolean.class)).
1211                     invokeExact();
1212             });
1213             checkWMTE(() -> { // >
1214                 boolean r = (boolean) hs.get(am, methodType(boolean.class, type, type, Class.class)).
1215                     invokeExact("foo", "foo", Void.class);
1216             });
1217         }
1218 
1219         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1220             // Incorrect argument types
1221             hs.checkWMTEOrCCE(() -> { // expected reference class
1222                 String x = (String) hs.get(am, methodType(type, Class.class, type)).
1223                     invokeExact(Void.class, "foo");
1224             });
1225             hs.checkWMTEOrCCE(() -> { // actual reference class
1226                 String x = (String) hs.get(am, methodType(type, type, Class.class)).
1227                     invokeExact("foo", Void.class);
1228             });
1229             // Incorrect return type
1230             hs.checkWMTEOrCCE(() -> { // reference class
1231                 Void r = (Void) hs.get(am, methodType(Void.class, type, type)).
1232                     invokeExact("foo", "foo");
1233             });
1234             checkWMTE(() -> { // primitive class
1235                 boolean x = (boolean) hs.get(am, methodType(boolean.class, type, type)).
1236                     invokeExact("foo", "foo");
1237             });
1238             // Incorrect arity
1239             checkWMTE(() -> { // 0
1240                 String x = (String) hs.get(am, methodType(type)).
1241                     invokeExact();
1242             });
1243             checkWMTE(() -> { // >
1244                 String x = (String) hs.get(am, methodType(type, type, type, Class.class)).
1245                     invokeExact("foo", "foo", Void.class);
1246             });
1247         }
1248 
1249         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
1250             // Incorrect argument types
1251             hs.checkWMTEOrCCE(() -> { // value reference class
1252                 String x = (String) hs.get(am, methodType(type, Class.class)).
1253                     invokeExact(Void.class);
1254             });
1255             // Incorrect return type
1256             hs.checkWMTEOrCCE(() -> { // reference class
1257                 Void r = (Void) hs.get(am, methodType(Void.class, type)).
1258                     invokeExact("foo");
1259             });
1260             checkWMTE(() -> { // primitive class
1261                 boolean x = (boolean) hs.get(am, methodType(boolean.class, type)).
1262                     invokeExact("foo");
1263             });
1264             // Incorrect arity
1265             checkWMTE(() -> { // 0
1266                 String x = (String) hs.get(am, methodType(type)).
1267                     invokeExact();
1268             });
1269             checkWMTE(() -> { // >
1270                 String x = (String) hs.get(am, methodType(type, type, Class.class)).
1271                     invokeExact("foo", Void.class);
1272             });
1273         }
1274 
1275 
1276     }
1277 
1278 
1279     static void testArrayWrongMethodType(VarHandle vh) throws Throwable {
1280         String[] array = new String[10];
1281         Arrays.fill(array, "foo");
1282 
1283         // Get
1284         // Incorrect argument types
1285         checkNPE(() -> { // null array
1286             String x = (String) vh.get(null, 0);
1287         });
1288         checkCCE(() -> { // array reference class
1289             String x = (String) vh.get(Void.class, 0);
1290         });

1848             boolean x = (boolean) vh.getAndSetRelease(array, 0, "foo");
1849         });
1850         // Incorrect arity
1851         checkWMTE(() -> { // 0
1852             String x = (String) vh.getAndSetRelease();
1853         });
1854         checkWMTE(() -> { // >
1855             String x = (String) vh.getAndSetRelease(array, 0, "foo", Void.class);
1856         });
1857 
1858 
1859     }
1860 
1861     static void testArrayWrongMethodType(Handles hs) throws Throwable {
1862         String[] array = new String[10];
1863         Arrays.fill(array, "foo");
1864 
1865         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET)) {
1866             // Incorrect argument types
1867             checkNPE(() -> { // null array
1868                 String x = (String) hs.get(am, methodType(type, String[].class, int.class)).
1869                     invokeExact((String[]) null, 0);
1870             });
1871             hs.checkWMTEOrCCE(() -> { // array reference class
1872                 String x = (String) hs.get(am, methodType(type, Class.class, int.class)).
1873                     invokeExact(Void.class, 0);
1874             });
1875             checkWMTE(() -> { // array primitive class
1876                 String x = (String) hs.get(am, methodType(type, int.class, int.class)).
1877                     invokeExact(0, 0);
1878             });
1879             checkWMTE(() -> { // index reference class
1880                 String x = (String) hs.get(am, methodType(type, String[].class, Class.class)).
1881                     invokeExact(array, Void.class);
1882             });
1883             // Incorrect return type
1884             hs.checkWMTEOrCCE(() -> { // reference class
1885                 Void x = (Void) hs.get(am, methodType(Void.class, String[].class, int.class)).
1886                     invokeExact(array, 0);
1887             });
1888             checkWMTE(() -> { // primitive class
1889                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class)).
1890                     invokeExact(array, 0);
1891             });
1892             // Incorrect arity
1893             checkWMTE(() -> { // 0
1894                 String x = (String) hs.get(am, methodType(type)).
1895                     invokeExact();
1896             });
1897             checkWMTE(() -> { // >
1898                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, Class.class)).
1899                     invokeExact(array, 0, Void.class);
1900             });
1901         }
1902 
1903         for (TestAccessMode am : testAccessModesOfType(TestAccessType.SET)) {
1904             // Incorrect argument types
1905             checkNPE(() -> { // null array
1906                 hs.get(am, methodType(void.class, String[].class, int.class, type)).
1907                     invokeExact((String[]) null, 0, "foo");
1908             });
1909             hs.checkWMTEOrCCE(() -> { // array reference class
1910                 hs.get(am, methodType(void.class, Class.class, int.class, type)).
1911                     invokeExact(Void.class, 0, "foo");
1912             });
1913             hs.checkWMTEOrCCE(() -> { // value reference class
1914                 hs.get(am, methodType(void.class, String[].class, int.class, Class.class)).
1915                     invokeExact(array, 0, Void.class);
1916             });
1917             checkWMTE(() -> { // receiver primitive class
1918                 hs.get(am, methodType(void.class, int.class, int.class, type)).
1919                     invokeExact(0, 0, "foo");
1920             });
1921             checkWMTE(() -> { // index reference class
1922                 hs.get(am, methodType(void.class, String[].class, Class.class, type)).
1923                     invokeExact(array, Void.class, "foo");
1924             });
1925             // Incorrect arity
1926             checkWMTE(() -> { // 0
1927                 hs.get(am, methodType(void.class)).
1928                     invokeExact();
1929             });
1930             checkWMTE(() -> { // >
1931                 hs.get(am, methodType(void.class, String[].class, int.class, Class.class)).
1932                     invokeExact(array, 0, "foo", Void.class);
1933             });
1934         }
1935         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_SET)) {
1936             // Incorrect argument types
1937             checkNPE(() -> { // null receiver
1938                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, type, type)).
1939                     invokeExact((String[]) null, 0, "foo", "foo");
1940             });
1941             hs.checkWMTEOrCCE(() -> { // receiver reference class
1942                 boolean r = (boolean) hs.get(am, methodType(boolean.class, Class.class, int.class, type, type)).
1943                     invokeExact(Void.class, 0, "foo", "foo");
1944             });
1945             hs.checkWMTEOrCCE(() -> { // expected reference class
1946                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, Class.class, type)).
1947                     invokeExact(array, 0, Void.class, "foo");
1948             });
1949             hs.checkWMTEOrCCE(() -> { // actual reference class
1950                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, type, Class.class)).
1951                     invokeExact(array, 0, "foo", Void.class);
1952             });
1953             checkWMTE(() -> { // receiver primitive class
1954                 boolean r = (boolean) hs.get(am, methodType(boolean.class, int.class, int.class, type, type)).
1955                     invokeExact(0, 0, "foo", "foo");
1956             });
1957             checkWMTE(() -> { // index reference class
1958                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, Class.class, type, type)).
1959                     invokeExact(array, Void.class, "foo", "foo");
1960             });
1961             // Incorrect arity
1962             checkWMTE(() -> { // 0
1963                 boolean r = (boolean) hs.get(am, methodType(boolean.class)).
1964                     invokeExact();
1965             });
1966             checkWMTE(() -> { // >
1967                 boolean r = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, type, type, Class.class)).
1968                     invokeExact(array, 0, "foo", "foo", Void.class);
1969             });
1970         }
1971 
1972         for (TestAccessMode am : testAccessModesOfType(TestAccessType.COMPARE_AND_EXCHANGE)) {
1973             // Incorrect argument types
1974             checkNPE(() -> { // null receiver
1975                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, type, type)).
1976                     invokeExact((String[]) null, 0, "foo", "foo");
1977             });
1978             hs.checkWMTEOrCCE(() -> { // array reference class
1979                 String x = (String) hs.get(am, methodType(type, Class.class, int.class, type, type)).
1980                     invokeExact(Void.class, 0, "foo", "foo");
1981             });
1982             hs.checkWMTEOrCCE(() -> { // expected reference class
1983                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, Class.class, type)).
1984                     invokeExact(array, 0, Void.class, "foo");
1985             });
1986             hs.checkWMTEOrCCE(() -> { // actual reference class
1987                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, type, Class.class)).
1988                     invokeExact(array, 0, "foo", Void.class);
1989             });
1990             checkWMTE(() -> { // array primitive class
1991                 String x = (String) hs.get(am, methodType(type, int.class, int.class, type, type)).
1992                     invokeExact(0, 0, "foo", "foo");
1993             });
1994             checkWMTE(() -> { // index reference class
1995                 String x = (String) hs.get(am, methodType(type, String[].class, Class.class, type, type)).
1996                     invokeExact(array, Void.class, "foo", "foo");
1997             });
1998             // Incorrect return type
1999             hs.checkWMTEOrCCE(() -> { // reference class
2000                 Void r = (Void) hs.get(am, methodType(Void.class, String[].class, int.class, type, type)).
2001                     invokeExact(array, 0, "foo", "foo");
2002             });
2003             checkWMTE(() -> { // primitive class
2004                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, type, type)).
2005                     invokeExact(array, 0, "foo", "foo");
2006             });
2007             // Incorrect arity
2008             checkWMTE(() -> { // 0
2009                 String x = (String) hs.get(am, methodType(type)).
2010                     invokeExact();
2011             });
2012             checkWMTE(() -> { // >
2013                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, type, type, Class.class)).
2014                     invokeExact(array, 0, "foo", "foo", Void.class);
2015             });
2016         }
2017 
2018         for (TestAccessMode am : testAccessModesOfType(TestAccessType.GET_AND_SET)) {
2019             // Incorrect argument types
2020             checkNPE(() -> { // null array
2021                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, type)).
2022                     invokeExact((String[]) null, 0, "foo");
2023             });
2024             hs.checkWMTEOrCCE(() -> { // array reference class
2025                 String x = (String) hs.get(am, methodType(type, Class.class, int.class, type)).
2026                     invokeExact(Void.class, 0, "foo");
2027             });
2028             hs.checkWMTEOrCCE(() -> { // value reference class
2029                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, Class.class)).
2030                     invokeExact(array, 0, Void.class);
2031             });
2032             checkWMTE(() -> { // array primitive class
2033                 String x = (String) hs.get(am, methodType(type, int.class, int.class, type)).
2034                     invokeExact(0, 0, "foo");
2035             });
2036             checkWMTE(() -> { // index reference class
2037                 String x = (String) hs.get(am, methodType(type, String[].class, Class.class, type)).
2038                     invokeExact(array, Void.class, "foo");
2039             });
2040             // Incorrect return type
2041             hs.checkWMTEOrCCE(() -> { // reference class
2042                 Void r = (Void) hs.get(am, methodType(Void.class, String[].class, int.class, type)).
2043                     invokeExact(array, 0, "foo");
2044             });
2045             checkWMTE(() -> { // primitive class
2046                 boolean x = (boolean) hs.get(am, methodType(boolean.class, String[].class, int.class, type)).
2047                     invokeExact(array, 0, "foo");
2048             });
2049             // Incorrect arity
2050             checkWMTE(() -> { // 0
2051                 String x = (String) hs.get(am, methodType(type)).
2052                     invokeExact();
2053             });
2054             checkWMTE(() -> { // >
2055                 String x = (String) hs.get(am, methodType(type, String[].class, int.class, type, Class.class)).
2056                     invokeExact(array, 0, "foo", Void.class);
2057             });
2058         }
2059 
2060 
2061     }
2062 }
< prev index next >