1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8020968 27 * @summary Basic test for hidden frames 28 * @run main HiddenFrames 29 */ 30 31 import java.lang.StackWalker.Option; 32 import java.lang.StackWalker.StackFrame; 33 import java.lang.reflect.Method; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 37 import java.util.stream.Stream; 38 39 public class HiddenFrames { 40 public static void main(String... args) throws Exception { 41 new HiddenFrames().test(); 42 new HiddenFrames(Option.SHOW_REFLECT_FRAMES).test(); 43 new HiddenFrames(Option.SHOW_HIDDEN_FRAMES).test(); 44 } 45 46 private final Option option; 47 private final StackWalker walker; 48 private final List<StackFrame> lambdas = new ArrayList<>(); 49 private final List<StackFrame> reflects = new ArrayList<>(); 50 51 HiddenFrames() { 52 this.option = null; 53 this.walker = StackWalker.create(); 54 } 55 HiddenFrames(Option option) { 56 this.option = option; 57 this.walker = StackWalker.create(option); 58 } 59 60 void test() throws Exception { 61 walk(); 62 walkFromReflection(); 63 } 64 65 void walk() { 66 Stream.of(0).forEach(i -> walker.walk(s -> 67 { 68 s.forEach(this::checkFrame); 69 return null; 70 })); 71 72 // only check hidden frames but not reflection frames 73 // walk is not invoked via reflection 74 if (option == null && !lambdas.isEmpty()) { 75 throw new RuntimeException("Hidden frames are shown"); 76 } 77 78 if (option == Option.SHOW_HIDDEN_FRAMES && lambdas.isEmpty()) { 79 throw new RuntimeException("No hidden Lambda frame"); 80 } 81 } 82 83 void walkFromReflection() throws Exception { 84 Method m = HiddenFrames.class.getDeclaredMethod("walk"); 85 m.invoke(this); 86 87 if (option == null && !lambdas.isEmpty()) { 88 throw new RuntimeException("Hidden frames are shown"); 89 } 90 91 if (option == Option.SHOW_HIDDEN_FRAMES && lambdas.isEmpty()) { 92 throw new RuntimeException("No hidden Lambda frame"); 93 } 94 95 if (option != null && reflects.isEmpty()) { 96 throw new RuntimeException("No reflection frame"); 97 } 98 } 99 100 void checkFrame(StackFrame frame) { 101 String cn = frame.getClassName(); 102 if (cn.startsWith("java.lang.reflect.") || cn.startsWith("sun.reflect.")) { 103 reflects.add(frame); 104 } 105 if (cn.contains("$$Lambda$")) { 106 lambdas.add(frame); 107 } 108 } 109 }