1 /*
   2  * Copyright (c) 2018, 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 
  25 /*
  26  * @test
  27  * @summary test MethodHandle/VarHandle on inline types 
  28  * @compile -XDallowWithFieldOperator Point.java Line.java MutablePath.java MixedValues.java
  29  * @run testng/othervm -XX:+EnableValhalla MethodHandleTest
  30  */
  31 
  32 import java.lang.invoke.*;
  33 import java.lang.reflect.Field;
  34 import java.lang.reflect.Modifier;
  35 import java.util.*;
  36 
  37 import org.testng.annotations.BeforeTest;
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 import static org.testng.Assert.*;
  41 
  42 public class MethodHandleTest {
  43     private static final Point P = Point.makePoint(10, 20);
  44     private static final Line L = Line.makeLine(10, 20, 30, 40);
  45     private static final MutablePath PATH = MutablePath.makePath(10, 20, 30, 40);
  46 
  47     @Test
  48     public static void testPointClass() throws Throwable  {
  49         MethodHandleTest test = new MethodHandleTest("Point", P, "x", "y");
  50         test.run();
  51     }
  52 
  53     @Test
  54     public static void testLineClass() throws Throwable {
  55         MethodHandleTest test = new MethodHandleTest("Line", L, "p1", "p2");
  56         test.run();
  57     }
  58 
  59     @Test
  60     public static void testMutablePath() throws Throwable {
  61         MethodHandleTest test = new MethodHandleTest("MutablePath", PATH, "p1", "p2");
  62         test.run();
  63 
  64         // set the mutable fields
  65         MutablePath path = MutablePath.makePath(1, 2, 3, 44);
  66         Point p = Point.makePoint(100, 200);
  67         test.setValueField("p1", path, p);
  68         test.setValueField("p2", path, p);
  69     }
  70 
  71     @Test
  72     public static void testValueFields() throws Throwable {
  73         MutablePath path = MutablePath.makePath(1, 2, 3, 4);
  74         // p1 and p2 are a non-final field of inline type in a reference
  75         MethodHandleTest test1 = new MethodHandleTest("Point", path.p1, "x", "y");
  76         test1.run();
  77 
  78         MethodHandleTest test2 = new MethodHandleTest("Point", path.p2, "x", "y");
  79         test2.run();
  80     }
  81 
  82     @Test
  83     public static void testMixedValues() throws Throwable {
  84         MixedValues mv = new MixedValues(P, L, PATH, "mixed", "types");
  85         MethodHandleTest test =
  86             new MethodHandleTest("MixedValues", mv, "p", "l", "mutablePath", "list", "nfp");
  87         test.run();
  88 
  89         Point p = Point.makePoint(100, 200);
  90         Line l = Line.makeLine(100, 200, 300, 400);
  91         test.setValueField("p", mv, p);
  92         test.setValueField("nfp", mv, p);
  93         test.setValueField("l", mv, l);
  94         test.setValueField("l", mv, l);
  95         test.setValueField("staticPoint", null, p);
  96         test.setValueField("staticLine", null, l);
  97         // staticLine is a nullable field
  98         test.setValueField("staticLine", null, null);
  99     }
 100 
 101     @Test
 102     public static void testArrayElementSetterAndGetter() throws Throwable {
 103         testArray(Point[].class, P);
 104         testArray(Line[].class, L);
 105         testArray(MutablePath[].class, PATH);
 106     }
 107 
 108     static void testArray(Class<?> c, Object o) throws Throwable {
 109         MethodHandle setter = MethodHandles.arrayElementSetter(c);
 110         MethodHandle getter = MethodHandles.arrayElementGetter(c);
 111         MethodHandle ctor = MethodHandles.arrayConstructor(c);
 112         int size = 5;
 113         Object[] array = (Object[])ctor.invoke(size);
 114         for (int i=0; i < size; i++) {
 115             setter.invoke(array, i, o);
 116         }
 117         for (int i=0; i < size; i++) {
 118             Object v = (Object)getter.invoke(array, i);
 119             assertEquals(v, o);
 120         }
 121 
 122         // set an array element to null
 123         Class<?> elementType = c.getComponentType();
 124         try {
 125             // inline array element is flattenable
 126             Object v = (Object)setter.invoke(array, 0, null);
 127             assertFalse(elementType.isValue(), "should fail to set an inline class array element to null");
 128         } catch (NullPointerException e) {
 129             assertTrue(elementType.isValue(), "should only fail to set an inline class array element to null");
 130         }
 131     }
 132 
 133     private final Class<?> c;
 134     private final Object o;
 135     private final List<String> names;
 136     public MethodHandleTest(String cn, Object o, String... fields) throws Exception {
 137         this.c = Class.forName(cn);
 138         this.o = o;
 139         this.names = List.of(fields);
 140     }
 141 
 142     public void run() throws Throwable {
 143         for (String name : names) {
 144             Field f = c.getDeclaredField(name);
 145             unreflectField(f);
 146             findGetter(f);
 147             varHandle(f);
 148             if (c.isValue())
 149                 ensureImmutable(f);
 150             else
 151                 ensureNullable(f);
 152         }
 153     }
 154 
 155     public List<String> names() {
 156         return names;
 157     }
 158 
 159     void findGetter(Field f) throws Throwable {
 160         MethodHandle mh = MethodHandles.lookup().findGetter(c, f.getName(), f.getType());
 161         Object value = mh.invoke(o);
 162     }
 163 
 164     void varHandle(Field f) throws Throwable {
 165         VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
 166         Object value = vh.get(o);
 167     }
 168 
 169     void unreflectField(Field f) throws Throwable {
 170         MethodHandle mh = MethodHandles.lookup().unreflectGetter(f);
 171         Object value = mh.invoke(o);
 172     }
 173 
 174     /*
 175      * Test setting a field of an inline type to a new value.
 176      * The field must be flattenable but may or may not be flattened.
 177      */
 178     void setValueField(String name, Object obj, Object value) throws Throwable {
 179         Field f = c.getDeclaredField(name);
 180         boolean isStatic = Modifier.isStatic(f.getModifiers());
 181         assertTrue(f.getType().isValue());
 182         assertTrue((isStatic && obj == null) || (!isStatic && obj != null));
 183         Object v = f.get(obj);
 184 
 185         // Field::set
 186         try {
 187             f.set(obj, value);
 188             assertEquals(f.get(obj), value);
 189         } finally {
 190             f.set(obj, v);
 191         }
 192 
 193 
 194         if (isStatic) {
 195             setStaticField(f, value);
 196         } else {
 197             setInstanceField(f, obj, value);
 198         }
 199     }
 200 
 201     private void setInstanceField(Field f, Object obj, Object value) throws Throwable {
 202         Object v = f.get(obj);
 203         // MethodHandle::invoke
 204         try {
 205             MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
 206             mh.invoke(obj, value);
 207             assertEquals(f.get(obj), value);
 208         } finally {
 209             f.set(obj, v);
 210         }
 211         // VarHandle::set
 212         try {
 213             VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
 214             vh.set(obj, value);
 215             assertEquals(f.get(obj), value);
 216         } finally {
 217             f.set(obj, v);
 218         }
 219     }
 220 
 221     private void setStaticField(Field f, Object value) throws Throwable {
 222         Object v = f.get(null);
 223         // MethodHandle::invoke
 224         try {
 225             MethodHandle mh = MethodHandles.lookup().findStaticSetter(c, f.getName(), f.getType());
 226             mh.invoke(f.getType().cast(value));
 227             assertEquals(f.get(null), value);
 228         } finally {
 229             f.set(null, v);
 230         }
 231         // VarHandle::set
 232         try {
 233             VarHandle vh = MethodHandles.lookup().findStaticVarHandle(c, f.getName(), f.getType());
 234             vh.set(f.getType().cast(value));
 235             assertEquals(f.get(null), value);
 236         } finally {
 237             f.set(null, v);
 238         }
 239     }
 240 
 241     /*
 242      * Test setting the given field to null via reflection, method handle
 243      * and var handle.
 244      */
 245     void ensureNullable(Field f) throws Throwable {
 246         assertFalse(Modifier.isStatic(f.getModifiers()));
 247         // flattenable implies non-nullable
 248         boolean canBeNull = !isFlattenable(f);
 249         // test reflection
 250         try {
 251             f.set(o, null);
 252             assertTrue(canBeNull, f + " cannot be set to null");
 253         } catch (NullPointerException e) {
 254             assertFalse(canBeNull, f + " should allow be set to null");
 255         }
 256         // test method handle, i.e. putfield bytecode behavior
 257         try {
 258             MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
 259             mh.invoke(o, null);
 260             assertTrue(canBeNull, f + " cannot be set to null");
 261         } catch (NullPointerException e) {
 262             assertFalse(canBeNull, f + " should allow be set to null");
 263         }
 264         // test var handle
 265         try {
 266             VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
 267             vh.set(o, null);
 268             assertTrue(canBeNull, f + " cannot be set to null");
 269         } catch (NullPointerException e) {
 270             assertFalse(canBeNull, f + " should allow be set to null");
 271         }
 272     }
 273 
 274     void ensureImmutable(Field f) throws Throwable {
 275         assertFalse(Modifier.isStatic(f.getModifiers()));
 276         Object v = f.get(o);
 277         // test reflection
 278         try {
 279             f.set(o, v);
 280             throw new RuntimeException(f + " should be immutable");
 281         } catch (IllegalAccessException e) {}
 282 
 283         // test method handle, i.e. putfield bytecode behavior
 284         try {
 285             MethodHandle mh = MethodHandles.lookup().findSetter(c, f.getName(), f.getType());
 286             mh.invoke(o, v);
 287             throw new RuntimeException(f + " should be immutable");
 288         } catch (IllegalAccessException e) { }
 289         // test var handle
 290         try {
 291             VarHandle vh = MethodHandles.lookup().findVarHandle(c, f.getName(), f.getType());
 292             vh.set(o, v);
 293             throw new RuntimeException(f + " should be immutable");
 294         } catch (UnsupportedOperationException e) {}
 295     }
 296 
 297     boolean isFlattened(Field f) {
 298         return (f.getModifiers() & 0x00008000) == 0x00008000;
 299     }
 300 
 301     boolean isFlattenable(Field f) {
 302         return (f.getModifiers() & 0x00000100) == 0x00000100;
 303     }
 304 
 305 }