/*
 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package vm.mlvm.indy.func.java.verifyStackTrace;

import java.dyn.CallSite;
import java.dyn.InvokeDynamic;
import java.dyn.Linkage;
import java.dyn.MethodHandles;
import java.dyn.MethodType;

import vm.mlvm.share.MlvmTest;

public class Test extends MlvmTest {

    private static final String METHOD_NAME = "runFunky";
    private static final int MAX_FRAME = 10;

    public Test() {}

    public static CallSite bootstrap(Class<?> c, String name, MethodType mt) {
        getLog().trace(0, "Class " + c + "; method name = " + name + "; method type = " + mt);

        boolean found = false;
        StackTraceElement trace[] = new Throwable().getStackTrace();
        for ( int i = 1; i < MAX_FRAME; i++ ) {
            StackTraceElement stackFrame = trace[3];
            getLog().trace(0, "Caller " + i + " stack frame: " + stackFrame);
            if ( stackFrame.getMethodName().equals(METHOD_NAME) ) {
                found = true;
                break;
            }
        }

        if ( ! found )
            throw new RuntimeException("Can't find caller method name (" + METHOD_NAME + ") in a bootstrap method stack");

        return new CallSite(MethodHandles.publicLookup().findStatic(Test.class, "target", mt));
    }

    public static Throwable target(Object o, String s, int i) {
        getLog().trace(0, "Target called! Object = " + o + "; string = " + s + "; int = " + i);
        return new Throwable();
    }

    public boolean runFunky() throws Throwable {
        Throwable t = (Throwable) InvokeDynamic.greet(new Object(), "world", 123);

        StackTraceElement stackFrame = t.getStackTrace()[1];
        if ( ! stackFrame.getMethodName().equals(METHOD_NAME) )
            throw new RuntimeException("Wrong method name in a bootstrap method: " + stackFrame);

        return true;
    }

    public boolean run() throws Throwable { return runFunky(); }

    public static void main(String[] args) { MlvmTest.launch(args); }
}
