test/jdk/java/lang/StackWalker/DumpStackTest.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * accompanied this code).
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * questions.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
 * @test
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    26
 * @bug 8143214
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    27
 * @summary Verify outputs of Thread.dumpStack() and Throwable.printStackTrace().
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
 *          This test should also been run against jdk9 successfully except of
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
 *          VM option MemberNameInStackFrame.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
 * @run main/othervm DumpStackTest
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
import java.lang.invoke.MethodHandle;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
import java.lang.invoke.MethodHandles;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
import java.lang.invoke.MethodType;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
import java.lang.reflect.Method;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
import java.util.Arrays;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
import java.util.function.Consumer;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
public class DumpStackTest {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
    public static void main(String args[]) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
        test();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
        testThread();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
        testLambda();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
        testMethodInvoke();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
        testMethodHandle();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    49
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    50
    static class CallFrame {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    51
        final String classname;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    52
        final String methodname;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    53
        CallFrame(Class<?> c, String methodname) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    54
            this(c.getName(), methodname);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    55
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    56
        CallFrame(String classname, String methodname) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    57
            this.classname = classname;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    58
            this.methodname = methodname;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    59
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    60
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    61
        String getClassName() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    62
            return classname;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    63
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    64
        String getMethodName() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    65
            return methodname;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    66
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    67
        String getFileName() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    68
            int i = classname.lastIndexOf('.');
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    69
            int j = classname.lastIndexOf('$');
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    70
            String name = classname.substring(i+1, j >= 0 ? j : classname.length());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    71
            return name + ".java";
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    72
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    73
        @Override
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    74
        public String toString() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    75
            return classname + "." + methodname + "(" + getFileName() + ")";
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    76
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    77
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    78
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    79
    static void test() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    80
        CallFrame[] callStack = new CallFrame[] {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    81
                new CallFrame(Thread.class, "getStackTrace"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    82
                new CallFrame(DumpStackTest.class, "test"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    83
                new CallFrame(DumpStackTest.class, "main"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    84
                // if invoked from jtreg
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
    85
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke0"), // non-public class
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
    86
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
    87
                new CallFrame("jdk.internal.reflect.DelegatingMethodAccessorImpl", "invoke"),
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    88
                new CallFrame(Method.class, "invoke"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    89
                new CallFrame(Thread.class, "run"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    90
        };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    91
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    92
        assertStackTrace(Thread.currentThread().getStackTrace(), callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    93
        getStackTrace(callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    94
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    95
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    96
    static void getStackTrace(CallFrame[] callStack) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    97
        // this method is the top of the stack
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    98
        callStack[0] = new CallFrame(DumpStackTest.class, "getStackTrace");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    99
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   100
        try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   101
            throw new RuntimeException();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   102
        } catch(RuntimeException ex) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   103
            assertStackTrace(ex.getStackTrace(), callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   104
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   105
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   106
    static void testThread() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   107
        Thread t1 = new Thread() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   108
            public void run() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   109
                c();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   110
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   111
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   112
            void c() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   113
                CallFrame[] callStack = new CallFrame[] {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   114
                        new CallFrame(Thread.class, "getStackTrace"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   115
                        new CallFrame(this.getClass(), "c"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   116
                        new CallFrame(this.getClass(), "run")
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   117
                };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   118
                assertStackTrace(Thread.currentThread().getStackTrace(), callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   119
                DumpStackTest.getStackTrace(callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   120
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   121
        };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   122
        t1.start();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   123
        try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   124
            t1.join();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   125
        } catch(InterruptedException e) {}
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   126
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   127
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   128
    static void testLambda() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   129
        Consumer<Void> c = (x) -> consumeLambda();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   130
        c.accept(null);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   131
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   132
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   133
    static void consumeLambda() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   134
        CallFrame[] callStack = new CallFrame[]{
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   135
                new CallFrame(Thread.class, "getStackTrace"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   136
                new CallFrame(DumpStackTest.class, "consumeLambda"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   137
                new CallFrame(DumpStackTest.class, "lambda$testLambda$0"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   138
                new CallFrame(DumpStackTest.class, "testLambda"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   139
                new CallFrame(DumpStackTest.class, "main"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   140
                // if invoked from jtreg
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   141
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke0"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   142
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   143
                new CallFrame("jdk.internal.reflect.DelegatingMethodAccessorImpl", "invoke"),
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   144
                new CallFrame(Method.class, "invoke"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   145
                new CallFrame(Thread.class, "run")
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   146
        };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   147
        assertStackTrace(Thread.currentThread().getStackTrace(), callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   148
        DumpStackTest.getStackTrace(callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   149
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   150
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   151
    static void testMethodInvoke() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   152
        try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   153
            Method m = DumpStackTest.class.getDeclaredMethod("methodInvoke");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   154
            m.invoke(null);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   155
        } catch(Exception e) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   156
            throw new RuntimeException(e);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   157
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   158
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   159
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   160
    static void methodInvoke() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   161
        CallFrame[] callStack = new CallFrame[] {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   162
                new CallFrame(Thread.class, "getStackTrace"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   163
                new CallFrame(DumpStackTest.class, "methodInvoke"),
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   164
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke0"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   165
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   166
                new CallFrame("jdk.internal.reflect.DelegatingMethodAccessorImpl", "invoke"),
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   167
                new CallFrame(Method.class, "invoke"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   168
                new CallFrame(DumpStackTest.class, "testMethodInvoke"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   169
                new CallFrame(DumpStackTest.class, "main"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   170
                // if invoked from jtreg
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   171
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke0"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   172
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   173
                new CallFrame("jdk.internal.reflect.DelegatingMethodAccessorImpl", "invoke"),
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   174
                new CallFrame(Method.class, "invoke"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   175
                new CallFrame(Thread.class, "run")
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   176
        };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   177
        assertStackTrace(Thread.currentThread().getStackTrace(), callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   178
        DumpStackTest.getStackTrace(callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   179
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   180
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   181
    static void testMethodHandle() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   182
        MethodHandles.Lookup lookup = MethodHandles.lookup();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   183
        try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   184
            MethodHandle handle = lookup.findStatic(DumpStackTest.class, "methodHandle",
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   185
                                                    MethodType.methodType(void.class));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   186
            handle.invoke();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   187
        } catch(Throwable t) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   188
            throw new RuntimeException(t);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   189
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   190
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   191
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   192
    static void methodHandle() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   193
        CallFrame[] callStack = new CallFrame[]{
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   194
                new CallFrame(Thread.class, "getStackTrace"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   195
                new CallFrame(DumpStackTest.class, "methodHandle"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   196
                new CallFrame(DumpStackTest.class, "testMethodHandle"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   197
                new CallFrame(DumpStackTest.class, "main"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   198
                // if invoked from jtreg
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   199
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke0"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   200
                new CallFrame("jdk.internal.reflect.NativeMethodAccessorImpl", "invoke"),
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 34372
diff changeset
   201
                new CallFrame("jdk.internal.reflect.DelegatingMethodAccessorImpl", "invoke"),
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   202
                new CallFrame(Method.class, "invoke"),
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   203
                new CallFrame(Thread.class, "run")
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   204
        };
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   205
        assertStackTrace(Thread.currentThread().getStackTrace(), callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   206
        DumpStackTest.getStackTrace(callStack);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   207
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   208
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   209
    static void assertStackTrace(StackTraceElement[] actual, CallFrame[] expected) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   210
        System.out.println("--- Actual ---");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   211
        Arrays.stream(actual).forEach(e -> System.out.println(e));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   212
        System.out.println("--- Expected ---");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   213
        Arrays.stream(expected).forEach(e -> System.out.println(e));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   214
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   215
        for (int i = 0, j = 0; i < actual.length; i++) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   216
            // filter test framework classes
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   217
            if (actual[i].getClassName().startsWith("com.sun.javatest.regtest"))
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   218
                continue;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   219
            assertEquals(actual[i], expected[j++], i);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   220
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   221
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   222
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   223
    static void assertEquals(StackTraceElement actual, CallFrame expected, int idx) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   224
        if (!actual.getClassName().equals(expected.getClassName()) ||
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   225
                !actual.getFileName().equals(expected.getFileName()) ||
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   226
                !actual.getMethodName().equals(expected.getMethodName())) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   227
            throw new RuntimeException("StackTraceElements mismatch at index " + idx +
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   228
                ". Expected [" + expected + "], but get [" + actual + "]");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   229
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   230
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
   231
}