test/jdk/java/lang/StackWalker/TestBCI.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:
37819
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     1
/*
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     4
 *
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     8
 *
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    13
 * accompanied this code).
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    14
 *
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    18
 *
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    21
 * questions.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    22
 */
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    23
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    24
/*
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    25
 * @test
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    26
 * @bug 8140450
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    27
 * @summary Basic test for the StackWalker::getByteCodeIndex method
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    28
 * @modules jdk.jdeps/com.sun.tools.classfile
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    29
 * @run main TestBCI
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    30
 */
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    31
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    32
import com.sun.tools.classfile.Attribute;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    33
import com.sun.tools.classfile.ClassFile;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    34
import com.sun.tools.classfile.Code_attribute;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    35
import com.sun.tools.classfile.ConstantPoolException;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    36
import com.sun.tools.classfile.Descriptor;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    37
import com.sun.tools.classfile.LineNumberTable_attribute;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    38
import com.sun.tools.classfile.Method;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    39
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    40
import java.lang.StackWalker.StackFrame;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    41
import java.io.IOException;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    42
import java.io.InputStream;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    43
import java.util.Arrays;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    44
import java.util.Comparator;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    45
import java.util.HashMap;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    46
import java.util.Map;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    47
import java.util.Optional;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    48
import java.util.SortedSet;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    49
import java.util.TreeSet;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    50
import java.util.function.Function;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    51
import java.util.stream.Collectors;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    52
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    53
import static java.lang.StackWalker.Option.RETAIN_CLASS_REFERENCE;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    54
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    55
public class TestBCI {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    56
    public static void main(String... args) throws Exception {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    57
        TestBCI test = new TestBCI(Walker.class);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    58
        System.out.println("Line number table:");
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    59
        test.methods.values().stream()
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    60
            .sorted(Comparator.comparing(MethodInfo::name).reversed())
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    61
            .forEach(System.out::println);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    62
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    63
        // walk the stack
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    64
        test.walk();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    65
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    66
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    67
    private final Map<String, MethodInfo> methods;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    68
    private final Class<?> clazz;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    69
    TestBCI(Class<?> c) throws ConstantPoolException, IOException {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    70
        Map<String, MethodInfo> methods;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    71
        String filename = c.getName().replace('.', '/') + ".class";
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    72
        try (InputStream in = c.getResourceAsStream(filename)) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    73
            ClassFile cf = ClassFile.read(in);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    74
            methods = Arrays.stream(cf.methods)
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    75
                .map(m -> new MethodInfo(cf, m))
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    76
                .collect(Collectors.toMap(MethodInfo::name, Function.identity()));
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    77
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    78
        this.clazz = c;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    79
        this.methods = methods;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    80
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    81
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    82
    void walk() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    83
        Walker walker = new Walker();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    84
        walker.m1();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    85
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    86
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    87
    void verify(StackFrame frame) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    88
        if (frame.getDeclaringClass() != clazz)
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    89
            return;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    90
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    91
        int bci = frame.getByteCodeIndex();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    92
        int lineNumber = frame.getLineNumber();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    93
        System.out.format("%s.%s bci %d (%s:%d)%n",
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    94
                          frame.getClassName(), frame.getMethodName(), bci,
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    95
                          frame.getFileName(), lineNumber);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    96
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    97
        MethodInfo method = methods.get(frame.getMethodName());
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    98
        SortedSet<Integer> values = method.findLineNumbers(bci).get();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
    99
        if (!values.contains(lineNumber)) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   100
            throw new RuntimeException("line number for bci: " + bci + " "
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   101
                + lineNumber + " not matched line number table: " + values);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   102
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   103
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   104
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   105
    /*
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   106
     * BCIs in the execution stack when StackWalker::forEach is invoked
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   107
     * will cover BCI range in the line number table.
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   108
     */
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   109
    class Walker {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   110
        final StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   111
        void m1() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   112
            int i = (int)Math.random()+2;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   113
            m2(i*2);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   114
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   115
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   116
        void m2(int i) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   117
            i++;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   118
            m3(i);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   119
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   120
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   121
        void m3(int i) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   122
            i++; m4(i++);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   123
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   124
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   125
        int m4(int i) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   126
            walker.forEach(TestBCI.this::verify);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   127
            return i;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   128
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   129
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   130
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   131
    static class MethodInfo {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   132
        final Method method;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   133
        final String name;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   134
        final String paramTypes;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   135
        final String returnType;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   136
        final Map<Integer, SortedSet<Integer>> bciToLineNumbers = new HashMap<>();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   137
        MethodInfo(ClassFile cf, Method m) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   138
            this.method = m;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   139
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   140
            String name;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   141
            String paramTypes;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   142
            String returnType;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   143
            LineNumberTable_attribute.Entry[] lineNumberTable;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   144
            try {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   145
                // method name
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   146
                name = m.getName(cf.constant_pool);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   147
                // signature
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   148
                paramTypes = m.descriptor.getParameterTypes(cf.constant_pool);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   149
                returnType = m.descriptor.getReturnType(cf.constant_pool);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   150
                Code_attribute codeAttr = (Code_attribute)
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   151
                    m.attributes.get(Attribute.Code);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   152
                lineNumberTable = ((LineNumberTable_attribute)
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   153
                    codeAttr.attributes.get(Attribute.LineNumberTable)).line_number_table;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   154
            } catch (ConstantPoolException|Descriptor.InvalidDescriptor e) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   155
                throw new RuntimeException(e);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   156
            }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   157
            this.name = name;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   158
            this.paramTypes = paramTypes;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   159
            this.returnType = returnType;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   160
            Arrays.stream(lineNumberTable).forEach(entry ->
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   161
                bciToLineNumbers.computeIfAbsent(entry.start_pc, _n -> new TreeSet<>())
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   162
                    .add(entry.line_number));
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   163
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   164
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   165
        String name() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   166
            return name;
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   167
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   168
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   169
        Optional<SortedSet<Integer>> findLineNumbers(int value) {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   170
            return bciToLineNumbers.entrySet().stream()
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   171
                    .sorted(Map.Entry.comparingByKey(Comparator.reverseOrder()))
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   172
                    .filter(e -> e.getKey().intValue() <= value)
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   173
                    .map(Map.Entry::getValue)
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   174
                    .findFirst();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   175
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   176
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   177
        @Override
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   178
        public String toString() {
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   179
            StringBuilder sb = new StringBuilder();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   180
            sb.append(name);
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   181
            sb.append(paramTypes).append(returnType).append(" ");
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   182
            bciToLineNumbers.entrySet().stream()
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   183
                .sorted(Map.Entry.comparingByKey())
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   184
                .forEach(entry -> sb.append("bci:").append(entry.getKey()).append(" ")
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   185
                                    .append(entry.getValue()).append(" "));
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   186
            return sb.toString();
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   187
        }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   188
    }
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   189
8a2559d6fe5b 8153912: Reconsider StackFrame::getFileName and StackFrame::getLineNumber
mchung
parents:
diff changeset
   190
}