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