1 /*
   2  * Copyright (c) 2016, 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 package runtime.valhalla.valuetypes;
  24 
  25 import java.lang.invoke.*;
  26 import jdk.experimental.value.*;
  27 
  28 import static jdk.test.lib.Asserts.*;
  29 
  30 /*
  31  * @test VboxUnbox
  32  * @summary Exercise vbox & vunbox bytecodes
  33  * @library /testlibrary /
  34  * @build runtime.valhalla.valuetypes.ValueCapableClass
  35  * @run main/othervm -Xint -noverify runtime.valhalla.valuetypes.VboxUnbox
  36  *
  37  * To dump generated byte-code, add "-Dvalhalla.dumpIsolatedMethodClasses=/tmp/foo"
  38  */
  39 public class VboxUnbox {
  40 
  41     public static void main(String[] args) {
  42         testCorrectBoxing();
  43         testIncorrectBoxing();
  44     }
  45 
  46     public static void testCorrectBoxing() {
  47         Class<?> vcc = ValueCapableClass.class;
  48         Class<?> dvt = ValueType.forClass(vcc).valueClass();
  49 
  50         MethodHandle newDvt = newDvtMh(dvt);
  51         MethodHandle box = boxMh(dvt, vcc);
  52         MethodHandle unbox = unboxMh(vcc, dvt);
  53 
  54         ValueCapableClass newObject = ValueCapableClass.create(3, (short)7, (short)11);
  55         ValueCapableClass newBoxed = null;
  56 
  57         try {
  58             ValueCapableClass boxed = (ValueCapableClass) MethodHandles.filterReturnValue(newDvt, box).invokeExact();
  59             // Assuming MVT1.0, where source has no way of holding an actual value at the language level, so: box(unbox(Object))
  60             newBoxed = (ValueCapableClass) MethodHandles.filterReturnValue(unbox, box).invokeExact(newObject);
  61         }
  62         catch (Throwable t) { fail("Invokation Exception", t); }
  63 
  64         assertTrue(newObject.getX() == newBoxed.getX());
  65         assertTrue(newObject.getY() == newBoxed.getY());
  66         assertTrue(newObject.getZ() == newBoxed.getZ());
  67     }
  68 
  69     public static void testIncorrectBoxing() {
  70         Class<?> vcc = ValueCapableClass.class;
  71         Class<?> dvt = ValueType.forClass(vcc).valueClass();
  72 
  73         MethodHandle newDvt = newDvtMh(dvt);
  74         MethodHandle box = boxMh(dvt, String.class); // Illegal box type
  75         try {
  76             MethodHandles.filterReturnValue(newDvt, box).invoke();
  77             error("Expected ClassCastException");
  78         }
  79         catch (ClassCastException cce) {}
  80         catch (Throwable t) { fail("Invokation Exception", t); }
  81 
  82         MethodHandle unbox = unboxMh(vcc, String.class); // Illegal unbox type
  83         try {
  84             unbox.invoke(ValueCapableClass.create());
  85             error("Expected ClassCastException");
  86         }
  87         catch (ClassCastException cce) {}
  88         catch (Throwable t) { fail("Invokation Exception", t); }
  89     }
  90 
  91     /*
  92        Create new DVT via loading a value array element. Why this workaround:
  93        1) to avoid "ValueType.defaultValueConstant()" which may or may not be implemented with vunbox
  94        2) vnew requires valuefactory attribute, which at the time of writing isn't generated with the DVT
  95        3) vdefault doesn't exist at the time of writing
  96     */
  97     public static MethodHandle newDvtMh(Class<?> dvt) {
  98         return MethodHandleBuilder.loadCode(MethodHandles.lookup(), "newDvt", MethodType.methodType(dvt), CODE->{
  99                 CODE.iconst_1().anewarray(dvt).iconst_0().vaload().vreturn();
 100             });
 101     }
 102 
 103     public static MethodHandle boxMh(Class<?> inClass, Class<?> outClass) {
 104         return MethodHandleBuilder.loadCode(MethodHandles.lookup(), "box", MethodType.methodType(outClass, inClass), CODE->{
 105                 CODE.vload(0).vbox(outClass).areturn();
 106             });
 107     }
 108 
 109     public static MethodHandle unboxMh(Class<?> inClass, Class<?> outClass)  {
 110         return MethodHandleBuilder.loadCode(MethodHandles.lookup(), "box", MethodType.methodType(outClass, inClass), CODE->{
 111                 CODE.aload(0).vunbox(outClass).vreturn();
 112             });
 113     }
 114 
 115     /*
 116        TODO: Current repo state doesn't pick up the common test/lib Asserts
 117        remove this implementation when fixed
 118      */
 119     public static void fail(String msg, Throwable thr) { // Missing Asserts.fail(String, Throwable)
 120         throw new RuntimeException(msg, thr);
 121     }
 122     public static void error(String msg) { // Missing Asserts.error(String)
 123         throw new RuntimeException(msg);
 124     }
 125 }