Path: blob/master/test/jdk/java/lang/StackWalker/Basic.java
41149 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8140450 817389826* @summary Basic test for the StackWalker::walk method27* @run testng Basic28*/2930import java.lang.StackWalker.StackFrame;31import java.lang.invoke.MethodType;32import java.util.List;33import java.util.Map;34import java.util.stream.Collectors;35import java.util.stream.Stream;36import static java.lang.StackWalker.Option.*;3738import org.testng.annotations.DataProvider;39import org.testng.annotations.Test;40import static org.testng.Assert.*;4142public class Basic {43private static boolean verbose = false;4445@DataProvider(name = "stackDepths")46public static Object[][] stackDepths() {47return new Object[][] {48{ new int[] { 12 }, new int[] { 4, 8, 12} },49{ new int[] { 18 }, new int[] { 8, 16, 20} },50{ new int[] { 32 }, new int[] { 16, 32, 64} },51};52}5354/**55* For a stack of a given depth, it creates a StackWalker with an estimate.56* Test walking different number of frames57*/58@Test(dataProvider = "stackDepths")59public static void test(int[] depth, int[] estimates) {60Basic test = new Basic(depth[0]);61for (int estimate : estimates) {62test.walk(estimate);63}64}6566@Test67public static void testWalkFromConstructor() throws Exception {68System.out.println("testWalkFromConstructor:");69List<String> found = ((ConstructorNewInstance)ConstructorNewInstance.class.getMethod("create")70.invoke(null)).collectedFrames();71assertEquals(List.of(ConstructorNewInstance.class.getName()+"::<init>",72ConstructorNewInstance.class.getName()+"::create",73Basic.class.getName()+"::testWalkFromConstructor"),74found);75}7677@Test78public static void testMethodSignature() throws Exception {79List<StackFrame> frames = new StackBuilder(16, 16).build();80Map<String, MethodType> methodTypes = StackBuilder.methodTypes();81for (StackFrame f : frames) {82MethodType type = methodTypes.get(f.getMethodName());83if (type != null) {84System.out.format("%s.%s %s%n", f.getClassName(), f.getMethodName(),85f.getDescriptor());8687String descriptor = f.getDescriptor();88if (!descriptor.equals(type.toMethodDescriptorString())) {89throw new RuntimeException("Expected: " + type.toMethodDescriptorString()90+ " got: " + f.getDescriptor());91}9293if (!f.getMethodType().equals(type)) {94throw new RuntimeException("Expected: " + type95+ " got: " + f.getMethodType());96}9798// verify descriptor returned by getDescriptor() before and after99// getMethodType() is called100if (!descriptor.equals(f.getDescriptor())) {101throw new RuntimeException("Mismatched: " + descriptor102+ " got: " + f.getDescriptor());103}104}105}106}107108private final int depth;109Basic(int depth) {110this.depth = depth;111}112113/** For TestNG */114public Basic() {115depth = 0;116}117118/*119* Setup a stack builder with the expected stack depth120* Walk the stack and count the frames.121*/122void walk(int estimate) {123int limit = Math.min(depth, 16);124List<StackFrame> frames = new StackBuilder(depth, limit).build();125System.out.format("depth=%d estimate=%d expected=%d walked=%d%n",126depth, estimate, limit, frames.size());127assertEquals(limit, frames.size());128}129130static class ConstructorNewInstance {131static final StackWalker walker =132StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);133List<String> testFramesOrReflectionFrames;134public ConstructorNewInstance() {135testFramesOrReflectionFrames = walker.walk(this::parse);136}137public List<String> collectedFrames() {138return testFramesOrReflectionFrames;139}140public boolean accept(StackFrame f) {141// Frames whose class names don't contain "."142// are our own test frames. These are the ones143// we expect.144// Frames whose class names contain ".reflect."145// are reflection frames. None should be present,146// since they are supposed to be filtered by147// by StackWalker. If we find any, we want to fail.148if (!f.getClassName().contains(".")149|| f.getClassName().contains(".reflect.")) {150System.out.println(" " + f);151return true;152}153// Filter out all other frames (in particular154// those from the test framework) in order to155// have predictable results.156return false;157}158public String frame(StackFrame f) {159return f.getClassName() + "::" + f.getMethodName();160}161List<String> parse(Stream<StackFrame> s) {162return s.filter(this::accept)163.map(this::frame)164.collect(Collectors.toList());165}166public static ConstructorNewInstance create() throws Exception {167return ConstructorNewInstance.class.getConstructor().newInstance();168}169}170171static class StackBuilder {172private final int stackDepth;173private final int limit;174private int depth = 0;175private List<StackFrame> result;176StackBuilder(int stackDepth, int limit) {177this.stackDepth = stackDepth; // build method;178this.limit = limit;179}180List<StackFrame> build() {181trace("build");182m1();183return result;184}185void m1() {186trace("m1");187m2();188}189List m2() {190trace("m2");191m3();192return null;193}194int m3() {195trace("m3");196m4(null);197return 0;198}199void m4(Object o) {200trace("m4");201int remaining = stackDepth-depth-1;202if (remaining >= 4) {203m1();204} else {205filler(remaining);206}207}208void filler(int i) {209trace("filler");210if (i == 0)211walk();212else213filler(--i);214}215216void walk() {217StackWalker walker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE);218result = walker.walk(s -> s.limit(limit).collect(Collectors.toList()));219}220void trace(String methodname) {221++depth;222if (verbose)223System.out.format("%2d: %s%n", depth, methodname);224}225226static Map<String, MethodType> methodTypes() throws Exception {227return Map.of("m1", MethodType.methodType(void.class),228"m2", MethodType.methodType(List.class),229"m3", MethodType.methodType(int.class),230"m4", MethodType.methodType(void.class, Object.class));231}232}233234}235236237