1 /*
   2  * Copyright (c) 2011, 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  *
  28  * @summary converted from VM Testbase vm/mlvm/meth/func/java/throwException.
  29  * VM Testbase keywords: [feature_mlvm, quarantine]
  30  * VM Testbase comments: 8079714
  31  * VM Testbase readme:
  32  * DESCRIPTION
  33  *     The test creates a sequence of MHs (see vm/mlvm/mh/func/sequences test for details)
  34  *     and throws an exception from the latest test of this sequence and verifies that
  35  *     the exception is passed correctly.
  36  *
  37  * @library /vmTestbase
  38  *          /test/lib
  39  * @run driver jdk.test.lib.FileInstaller . .
  40  *
  41  * @comment build test class and indify classes
  42  * @build vm.mlvm.meth.func.java.throwException.Test
  43  * @run driver vm.mlvm.share.IndifiedClassesBuilder
  44  *
  45  * @run main/othervm vm.mlvm.meth.func.java.throwException.Test
  46  */
  47 
  48 package vm.mlvm.meth.func.java.throwException;
  49 
  50 import java.lang.invoke.MethodHandle;
  51 import java.lang.invoke.MethodHandles;
  52 import java.lang.invoke.MethodType;
  53 
  54 import vm.mlvm.meth.share.Argument;
  55 import vm.mlvm.meth.share.MHTransformationGen;
  56 import vm.mlvm.meth.share.RandomArgumentGen;
  57 import vm.mlvm.meth.share.RandomArgumentsGen;
  58 import vm.mlvm.meth.share.transform.v2.MHMacroTF;
  59 import vm.mlvm.share.MlvmTest;
  60 
  61 public class Test extends MlvmTest {
  62 
  63     public static void main(String[] args) { MlvmTest.launch(args); }
  64 
  65     public static class Example {
  66         private Throwable t;
  67 
  68         public Example(Throwable t) {
  69             this.t = t;
  70         }
  71 
  72         public Object m0(int i, String s, Float f) {
  73             RuntimeException re = new RuntimeException("Good luck!");
  74             re.initCause(this.t);
  75             throw re;
  76         }
  77     }
  78 
  79     @Override
  80     public boolean run() throws Throwable {
  81 
  82         final RuntimeException requiredException = new RuntimeException("test");
  83         final Example e = new Example(requiredException);
  84 
  85         final MethodHandle mh = MethodHandles.lookup().findVirtual(
  86                 Example.class, "m0",
  87                 MethodType.methodType(Object.class, int.class, String.class, Float.class));
  88 
  89         Argument[] finalArgs = RandomArgumentsGen.createRandomArgs(true, mh.type());
  90         Argument retVal = RandomArgumentGen.next(mh.type().returnType());
  91         retVal.setPreserved(true);
  92 
  93         MHMacroTF seq = MHTransformationGen.createSequence(retVal, e, mh, finalArgs);
  94 
  95         try {
  96             MHTransformationGen.callSequence(seq, false);
  97             getLog().complain("Did not catch a required exception!");
  98             return false;
  99         } catch ( Throwable t ) {
 100             while ( t != null && t instanceof Exception ) {
 101                 t = t.getCause();
 102                 if ( t.equals(requiredException) ) {
 103                     getLog().display("Got a proper exception:");
 104                     t.printStackTrace(getLog().getOutStream());
 105                     return true;
 106                 }
 107             }
 108 
 109             getLog().complain("Got wrong exception!");
 110             t.printStackTrace(getLog().getOutStream());
 111             return false;
 112         }
 113     }
 114 }