1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   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 import jdk.experimental.value.MethodHandleBuilder;
  25 import jdk.incubator.mvt.ValueType;
  26 import org.testng.annotations.Test;
  27 
  28 import java.lang.invoke.MethodHandle;
  29 import java.lang.invoke.MethodHandles;
  30 import java.lang.invoke.MethodType;
  31 
  32 import static org.testng.Assert.*;
  33 
  34 /*
  35  * @test
  36  * @modules java.base/jdk.experimental.value
  37  *          jdk.incubator.mvt
  38  * @run testng/othervm -XX:+EnableMVT -Dvalhalla.enablePoolPatches=true WithFieldTest
  39  */
  40 
  41 @Test
  42 public class WithFieldTest {
  43 
  44     static final ValueType<?> VT_Point = ValueType.forClass(Point.class);
  45 
  46     static final ValueType<?> VT_PrivatePoint = ValueType.forClass(PrivatePoint.class);
  47 
  48     static final MethodHandle MH_Point_get_x;
  49 
  50     static final MethodHandle MH_PrivatePoint_get_x;
  51 
  52     static {
  53         try {
  54             MH_Point_get_x = MethodHandles.lookup().findGetter(
  55                     Point.class, "x", int.class);
  56             MH_PrivatePoint_get_x = MethodHandles.lookup().findVirtual(
  57                     PrivatePoint.class, "getX", MethodType.methodType(int.class));
  58         } catch (Exception e) {
  59             throw new InternalError(e);
  60         }
  61     }
  62 
  63 
  64     public void testPrivateLookupPrivateFieldUsingVCC() throws Throwable {
  65         testPrivateAccess(VT_PrivatePoint, VT_PrivatePoint.boxClass(), MethodHandles.lookup(), MH_PrivatePoint_get_x);
  66     }
  67 
  68     public void testPrivateLookupPublicFieldUsingVCC() throws Throwable {
  69         testPrivateAccess(VT_Point, VT_Point.boxClass(), MethodHandles.lookup(), MH_Point_get_x);
  70     }
  71 
  72     public void testPrivateLookupPrivateFieldUsingDVT() throws Throwable {
  73         testPrivateAccess(VT_PrivatePoint, VT_PrivatePoint.valueClass(), MethodHandles.lookup(), MH_PrivatePoint_get_x);
  74     }
  75 
  76     public void testPrivateLookupPublicFieldUsingDVT() throws Throwable {
  77         testPrivateAccess(VT_Point, VT_Point.valueClass(), MethodHandles.lookup(), MH_Point_get_x);
  78     }
  79 
  80 
  81     @Test(expectedExceptions = IllegalAccessError.class)
  82     public void testLookupPrivateFieldUsingVCC() throws Throwable {
  83         testAccess(VT_PrivatePoint, MethodHandles.lookup(), MH_PrivatePoint_get_x);
  84     }
  85 
  86     @Test(expectedExceptions = IllegalAccessError.class)
  87     public void testLookupPublicFieldUsingVCC() throws Throwable {
  88         testAccess(VT_Point, MethodHandles.lookup(), MH_Point_get_x);
  89     }
  90 
  91     @Test(expectedExceptions = IllegalAccessError.class)
  92     public void testLookupPrivateFieldUsingDVT() throws Throwable {
  93         testAccess(VT_PrivatePoint, MethodHandles.lookup(), MH_PrivatePoint_get_x);
  94     }
  95 
  96     @Test(expectedExceptions = IllegalAccessError.class)
  97     public void testLookupPublicFieldUsingDVT() throws Throwable {
  98         testAccess(VT_Point, MethodHandles.lookup(), MH_Point_get_x);
  99     }
 100 
 101 
 102     static void testPrivateAccess(ValueType<?> vt, Class<?> lc, MethodHandles.Lookup l, MethodHandle getter) throws Throwable {
 103         testAccess(vt, MethodHandles.privateLookupIn(lc, l), getter);
 104     }
 105 
 106     static void testAccess(ValueType<?> vt, MethodHandles.Lookup l, MethodHandle getter) throws Throwable {
 107         MethodHandle mh = MethodHandles.collectArguments(vwithfield(l, vt, "x", int.class), 0, vt.defaultValueConstant());
 108 
 109         mh = MethodHandles.filterReturnValue(mh, vt.box());
 110 
 111         mh = MethodHandles.filterReturnValue(mh, getter);
 112 
 113         int actual = (int) mh.invoke(42);
 114         assertEquals(actual, 42);
 115     }
 116 
 117     static MethodHandle vwithfield(MethodHandles.Lookup lookup, ValueType<?> vt, String name, Class<?> type) {
 118         Class<?> dvt = vt.valueClass();
 119         return MethodHandleBuilder.loadCode(
 120                 lookup,
 121                 lookup.lookupClass().toString() + "_wither$" + name,
 122                 MethodType.methodType(dvt, dvt, type),
 123                 C -> C.vload(0).load(1).vwithfield(dvt, name, fieldDescriptor(type)).vreturn());
 124     }
 125 
 126     static String fieldDescriptor(Class<?> type) {
 127         String s = MethodType.methodType(type).toMethodDescriptorString();
 128         return s.substring(2);
 129     }
 130 
 131 }