test/jdk/java/lang/invoke/VarHandles/X-VarHandleTestAccess.java.template
changeset 52914 4fa75d8ad418
parent 49788 5375d426822a
child 53662 26ecda724028
equal deleted inserted replaced
52913:bf2f2560dd53 52914:4fa75d8ad418
     1 /*
     1 /*
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    48 
    48 
    49     final $type$ final_v = $value1$;
    49     final $type$ final_v = $value1$;
    50 
    50 
    51     $type$ v;
    51     $type$ v;
    52 
    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 
    53     VarHandle vhFinalField;
    61     VarHandle vhFinalField;
    54 
    62 
    55     VarHandle vhField;
    63     VarHandle vhField;
    56 
    64 
    57     VarHandle vhStaticField;
    65     VarHandle vhStaticField;
    61     VarHandle vhArray;
    69     VarHandle vhArray;
    62 
    70 
    63 #if[String]
    71 #if[String]
    64     VarHandle vhArrayObject;
    72     VarHandle vhArrayObject;
    65 #end[String]
    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     }
    66 
   113 
    67     @BeforeClass
   114     @BeforeClass
    68     public void setup() throws Exception {
   115     public void setup() throws Exception {
    69         vhFinalField = MethodHandles.lookup().findVarHandle(
   116         vhFinalField = MethodHandles.lookup().findVarHandle(
    70                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
   117                 VarHandleTestAccess$Type$.class, "final_v", $type$.class);
    91         vhs.add(vhField);
   138         vhs.add(vhField);
    92         vhs.add(vhStaticField);
   139         vhs.add(vhStaticField);
    93         vhs.add(vhArray);
   140         vhs.add(vhArray);
    94 
   141 
    95         return vhs.stream().map(tc -> new Object[]{tc}).toArray(Object[][]::new);
   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         }
    96     }
   168     }
    97 
   169 
    98     @Test(dataProvider = "varHandlesProvider")
   170     @Test(dataProvider = "varHandlesProvider")
    99     public void testIsAccessModeSupported(VarHandle vh) {
   171     public void testIsAccessModeSupported(VarHandle vh) {
   100         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));
   172         assertTrue(vh.isAccessModeSupported(VarHandle.AccessMode.GET));