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