Path: blob/master/test/jdk/java/lang/StackWalker/HiddenFrames.java
41149 views
/*1* Copyright (c) 2015, 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 802096826* @summary Basic test for hidden frames27* @run main HiddenFrames28*/2930import java.lang.StackWalker.Option;31import java.lang.StackWalker.StackFrame;32import java.lang.reflect.Method;33import java.util.ArrayList;34import java.util.List;35import java.util.stream.Stream;3637public class HiddenFrames {38public static void main(String... args) throws Exception {39new HiddenFrames().test();40new HiddenFrames(Option.SHOW_REFLECT_FRAMES).test();41new HiddenFrames(Option.SHOW_HIDDEN_FRAMES).test();42}4344private final Option option;45private final StackWalker walker;46private final List<StackFrame> lambdas = new ArrayList<>();47private final List<StackFrame> reflects = new ArrayList<>();4849HiddenFrames() {50this.option = null;51this.walker = StackWalker.getInstance();52}53HiddenFrames(Option option) {54this.option = option;55this.walker = StackWalker.getInstance(option);56}5758void test() throws Exception {59walk();60walkFromReflection();61}6263void walk() {64Stream.of(0).forEach(i -> walker.walk(s ->65{66s.forEach(this::checkFrame);67return null;68}));6970// only check hidden frames but not reflection frames71// walk is not invoked via reflection72if (option == null && !lambdas.isEmpty()) {73throw new RuntimeException("Hidden frames are shown");74}7576if (option == Option.SHOW_HIDDEN_FRAMES && lambdas.isEmpty()) {77throw new RuntimeException("No hidden Lambda frame");78}79}8081void walkFromReflection() throws Exception {82Method m = HiddenFrames.class.getDeclaredMethod("walk");83m.invoke(this);8485if (option == null && !lambdas.isEmpty()) {86throw new RuntimeException("Hidden frames are shown");87}8889if (option == Option.SHOW_HIDDEN_FRAMES && lambdas.isEmpty()) {90throw new RuntimeException("No hidden Lambda frame");91}9293if (option != null && reflects.isEmpty()) {94throw new RuntimeException("No reflection frame");95}96}9798void checkFrame(StackFrame frame) {99String cn = frame.getClassName();100if (cn.startsWith("java.lang.reflect.") || cn.startsWith("jdk.internal.reflect.")) {101reflects.add(frame);102}103if (cn.contains("$$Lambda$")) {104lambdas.add(frame);105}106}107}108109110