1 /*
   2  * Copyright (c) 2017, 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 reflection on value types
  28  * @compile -XDallowWithFieldOperator Point.java Line.java NonFlattenValue.java
  29  * @run main/othervm -XX:+EnableValhalla Reflection
  30  */
  31 
  32 import java.lang.reflect.*;
  33 import java.util.Arrays;
  34 import java.util.stream.Collector;
  35 import java.util.stream.Collectors;
  36 
  37 public class Reflection {
  38     public static void main(String... args) throws Exception {
  39         testPointClass();
  40         testLineClass();
  41         testNonFlattenValue();
  42     }
  43 
  44     static void testPointClass() throws Exception  {
  45         Point o = Point.makePoint(10, 20);
  46         Reflection test = new Reflection("Point", o);
  47         test.newInstance();
  48         test.constructor();
  49         test.accessFieldX(o.x);
  50         test.setAccessible();
  51         test.trySetAccessible();
  52         test.staticField();
  53     }
  54 
  55     static void testLineClass() throws Exception {
  56         Line l = Line.makeLine(10, 20, 30, 40);
  57         Reflection test = new Reflection("Line", l);
  58         test.checkField("p1", Point.class.asValueType());
  59         test.checkField("p2", Point.class.asValueType());
  60         test.checkMethod("p1", Point.class.asValueType());
  61         test.checkMethod("p2", Point.class.asValueType());
  62     }
  63 
  64     static void testNonFlattenValue() throws Exception {
  65         NonFlattenValue nfv = NonFlattenValue.make(10, 20);
  66         Reflection test = new Reflection("NonFlattenValue", nfv);
  67         test.checkField("nfp", Point.class.asBoxType());
  68         test.checkMethod("point", Point.class.asBoxType());
  69         test.checkMethod("pointValue", Point.class.asValueType());
  70         test.checkMethod("has", void.class, Point.class.asValueType(), Point.class.asBoxType());
  71     }
  72 
  73     private final Class<?> c;
  74     private final Constructor<?> ctor;
  75     private final Object o;
  76     Reflection(String cn, Object o) throws Exception {
  77         this.c = Class.forName(cn);
  78         if (!c.isValue()) {
  79             throw new RuntimeException(cn + " is not a value class");
  80         }
  81 
  82         this.ctor = c.getDeclaredConstructor();
  83         this.o = o;
  84 
  85         // TODO: what should Object::getClass return?
  86         // assertEquals(o.getClass(), c.asValueType());
  87 
  88         // test the box type and value type
  89         testBoxAndValueType(this.c);
  90         // test array of Q-type
  91         // TODO: array of L-type support
  92         testArrayOfQType();
  93     }
  94 
  95     private static void testBoxAndValueType(Class<?> c) {
  96         Class<?> box = c.asBoxType();
  97         Class<?> val = c.asValueType();
  98         assertTrue(val != null);
  99         assertEquals(box.getTypeName(), c.getTypeName());
 100         assertEquals(val.getTypeName(), c.getTypeName() + "/val");
 101         assertEquals(box, c);
 102         assertEquals(val.asBoxType(), box);
 103         assertEquals(box.asValueType(), val);
 104     }
 105 
 106     void testArrayOfQType() {
 107         Class<?> elementType = c.asValueType();
 108         Object array = Array.newInstance(elementType, 1);
 109         Class<?> arrayType = array.getClass();
 110         assertTrue(arrayType.isArray());
 111         Class<?> componentType = arrayType.getComponentType();
 112         assertTrue(componentType.isValue());
 113         assertEquals(componentType, elementType);
 114         // Array is a reference type
 115         assertEquals(arrayType.asBoxType(), arrayType);
 116     }
 117 
 118     void accessFieldX(int x) throws Exception {
 119         Field field = c.getField("x");
 120         if (field.getInt(o) != x) {
 121             throw new RuntimeException("Unexpected Point.x value: " +  field.getInt(o));
 122         }
 123 
 124         try {
 125             field.setInt(o, 100);
 126             throw new RuntimeException("IllegalAccessException not thrown");
 127         } catch (IllegalAccessException e) {}
 128     }
 129 
 130     void newInstance() throws Exception {
 131         try {
 132             Object o = c.newInstance();
 133             throw new RuntimeException("newInstance expected to be unsupported on value class");
 134         } catch (IllegalAccessException e) {}
 135     }
 136 
 137     void constructor() throws Exception {
 138         try {
 139             ctor.newInstance();
 140             throw new RuntimeException("IllegalAccessException not thrown");
 141         } catch (IllegalAccessException e) { }
 142     }
 143 
 144     void setAccessible() throws Exception {
 145         try {
 146             ctor.setAccessible(true);
 147             throw new RuntimeException("InaccessibleObjectException not thrown");
 148         } catch (InaccessibleObjectException e) { e.printStackTrace(); }
 149         Field field = c.getField("x");
 150         try {
 151             field.setAccessible(true);
 152             throw new RuntimeException("InaccessibleObjectException not thrown");
 153         } catch (InaccessibleObjectException e) { e.printStackTrace(); }
 154     }
 155 
 156     void trySetAccessible() throws Exception {
 157         if (ctor.trySetAccessible()) {
 158             throw new RuntimeException("trySetAccessible should not succeed");
 159         }
 160         Field field = c.getField("x");
 161         if (field.trySetAccessible()) {
 162             throw new RuntimeException("trySetAccessible should not succeed");
 163         }
 164     }
 165 
 166     void staticField() throws Exception {
 167         Field f = c.getDeclaredField("STATIC_FIELD");
 168         if (f.trySetAccessible()) {
 169             throw new RuntimeException("trySetAccessible should not succeed");
 170         }
 171         try {
 172             f.setAccessible(true);
 173             throw new RuntimeException("IllegalAccessException not thrown");
 174         } catch (InaccessibleObjectException e) { }
 175     }
 176 
 177     void checkField(String name, Class<?> type) throws Exception {
 178         Field f = c.getDeclaredField(name);
 179         System.out.format("Field %s::%s of type %s = %s%n",
 180                           f.getDeclaringClass().getTypeName(), f.getName(),
 181                           f.getType().getTypeName(), f.get(o));
 182         assertEquals(f.getType(), type);
 183     }
 184 
 185     void checkMethod(String name, Class<?> returnType, Class<?>... params) throws Exception {
 186         Method m = c.getDeclaredMethod(name, params);
 187 
 188         String paramDesc = (params == null || params.length == 0) ? "" :
 189             Arrays.stream(params).map(Class::getTypeName).collect(Collectors.joining(", "));
 190         System.out.format("Method %s::%s(%s)%s%n",
 191                           m.getDeclaringClass().getTypeName(), m.getName(),
 192                           paramDesc, returnType.getTypeName());
 193     }
 194     
 195     static void assertEquals(Object o1, Object o2) {
 196         if (o1 == o2 || o1.equals(o2))
 197             return;
 198 
 199         throw new AssertionError(o1 + " != " + o2);
 200     }
 201 
 202     static void assertTrue(boolean value) {
 203         if (!value)
 204             throw new AssertionError("expected true");
 205 
 206     }
 207 }