jdk/test/java/lang/StackWalker/NativeMethod.java
author mchung
Thu, 02 Jun 2016 16:39:14 -0700
changeset 38779 a78d46ad59c1
permissions -rw-r--r--
8157892: StackFrame::getFileName returns null when a source file exists for native methods 8157977: getByteCodeIndex method from StackFrame does not return negative number when StackFrame is a native frame Reviewed-by: dfuchs, bchristi
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38779
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     1
/*
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     4
 *
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     8
 *
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    13
 * accompanied this code).
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    14
 *
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    18
 *
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    21
 * questions.
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    22
 */
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    23
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    24
/*
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    25
 * @test
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    26
 * @bug 8157892 8157977
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    27
 * @summary Verify file name, line number and bci of native methods
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    28
 * @run main NativeMethod
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    29
 */
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    30
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    31
import java.lang.StackWalker.Option;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    32
import java.lang.StackWalker.StackFrame;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    33
import java.lang.reflect.Method;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    34
import java.util.List;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    35
import java.util.stream.Collectors;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    36
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    37
public class NativeMethod {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    38
    public static void main(String... args) throws Exception {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    39
        new NativeMethod().test();
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    40
    }
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    41
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    42
    private final StackWalker walker;
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    43
    NativeMethod() {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    44
        this.walker = StackWalker.getInstance(Option.SHOW_REFLECT_FRAMES);
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    45
    }
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    46
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    47
    void test() throws Exception {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    48
        // invoke via reflection that includes native methods
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    49
        Method m = NativeMethod.class.getDeclaredMethod("walk");
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    50
        m.invoke(this);
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    51
    }
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    52
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    53
    void walk() {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    54
        List<StackFrame> nativeFrames = walker.walk(s ->
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    55
            s.filter(StackFrame::isNativeMethod)
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    56
             .collect(Collectors.toList())
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    57
        );
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    58
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    59
        assertTrue(nativeFrames.size() > 0, "native frame not found");
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    60
        for (StackFrame f : nativeFrames) {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    61
            assertTrue(f.getFileName() != null, "source file expected to be found");
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    62
            assertTrue(f.getLineNumber() < 0, "line number expected to be unavailable");
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    63
            assertTrue(f.getByteCodeIndex() < 0, "bci expected to be unavailable");
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    64
        }
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    65
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    66
    }
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    67
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    68
    private static void assertTrue(boolean value, String msg) {
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    69
        if (value != true)
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    70
            throw new AssertionError(msg);
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    71
    }
a78d46ad59c1 8157892: StackFrame::getFileName returns null when a source file exists for native methods
mchung
parents:
diff changeset
    72
}