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 package vm.mlvm.indy.func.java.verifyStackTrace;
  25 
  26 import java.dyn.CallSite;
  27 import java.dyn.InvokeDynamic;
  28 import java.dyn.Linkage;
  29 import java.dyn.MethodHandles;
  30 import java.dyn.MethodType;
  31 
  32 import vm.mlvm.share.MlvmTest;
  33 
  34 public class Test extends MlvmTest {
  35 
  36     private static final String METHOD_NAME = "runFunky";
  37     private static final int MAX_FRAME = 10;
  38 
  39     public Test() {}
  40 
  41     public static CallSite bootstrap(Class<?> c, String name, MethodType mt) {
  42         getLog().trace(0, "Class " + c + "; method name = " + name + "; method type = " + mt);
  43 
  44         boolean found = false;
  45         StackTraceElement trace[] = new Throwable().getStackTrace();
  46         for ( int i = 1; i < MAX_FRAME; i++ ) {
  47             StackTraceElement stackFrame = trace[3];
  48             getLog().trace(0, "Caller " + i + " stack frame: " + stackFrame);
  49             if ( stackFrame.getMethodName().equals(METHOD_NAME) ) {
  50                 found = true;
  51                 break;
  52             }
  53         }
  54 
  55         if ( ! found )
  56             throw new RuntimeException("Can't find caller method name (" + METHOD_NAME + ") in a bootstrap method stack");
  57 
  58         return new CallSite(MethodHandles.publicLookup().findStatic(Test.class, "target", mt));
  59     }
  60 
  61     public static Throwable target(Object o, String s, int i) {
  62         getLog().trace(0, "Target called! Object = " + o + "; string = " + s + "; int = " + i);
  63         return new Throwable();
  64     }
  65 
  66     public boolean runFunky() throws Throwable {
  67         Throwable t = (Throwable) InvokeDynamic.greet(new Object(), "world", 123);
  68 
  69         StackTraceElement stackFrame = t.getStackTrace()[1];
  70         if ( ! stackFrame.getMethodName().equals(METHOD_NAME) )
  71             throw new RuntimeException("Wrong method name in a bootstrap method: " + stackFrame);
  72 
  73         return true;
  74     }
  75 
  76     public boolean run() throws Throwable { return runFunky(); }
  77 
  78     public static void main(String[] args) { MlvmTest.launch(args); }
  79 }