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 package test.java.lang.StackWalker;
32 
33 import java.lang.StackWalker.Option;
34 import java.lang.StackWalker.StackFrame;
35 import java.lang.reflect.Method;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.stream.Stream;
39 import org.testng.annotations.Test;
40 
41 public class HiddenFrames {
42     // Android-changed: Add @Test annotation.
43     // public static void main(String... args) throws Exception {
44     @Test
main()45     public static void main() throws Exception {
46         new HiddenFrames().test();
47         new HiddenFrames(Option.SHOW_REFLECT_FRAMES).test();
48         new HiddenFrames(Option.SHOW_HIDDEN_FRAMES).test();
49     }
50 
51     private final Option option;
52     private final StackWalker walker;
53     private final List<StackFrame> lambdas = new ArrayList<>();
54     private final List<StackFrame> reflects = new ArrayList<>();
55 
HiddenFrames()56     HiddenFrames() {
57         this.option = null;
58         this.walker = StackWalker.getInstance();
59     }
HiddenFrames(Option option)60     HiddenFrames(Option option) {
61         this.option = option;
62         this.walker = StackWalker.getInstance(option);
63     }
64 
test()65     void test() throws Exception {
66         walk();
67         walkFromReflection();
68     }
69 
walk()70     void walk() {
71        Stream.of(0).forEach(i -> walker.walk(s ->
72        {
73            s.forEach(this::checkFrame);
74            return null;
75        }));
76 
77         // only check hidden frames but not reflection frames
78         // walk is not invoked via reflection
79         if (option == null && !lambdas.isEmpty()) {
80             throw new RuntimeException("Hidden frames are shown");
81         }
82 
83         if (option == Option.SHOW_HIDDEN_FRAMES && lambdas.isEmpty()) {
84             // Android-removed: Android desugars lambdas during build-time, not in runtime.
85             // throw new RuntimeException("No hidden Lambda frame");
86         }
87     }
88 
walkFromReflection()89     void walkFromReflection() throws Exception {
90         Method m = HiddenFrames.class.getDeclaredMethod("walk");
91         m.invoke(this);
92 
93         if (option == null && !lambdas.isEmpty()) {
94             throw new RuntimeException("Hidden frames are shown");
95         }
96 
97         if (option == Option.SHOW_HIDDEN_FRAMES && lambdas.isEmpty()) {
98             // Android-removed: Android desugars lambdas during build-time, not in runtime.
99             // throw new RuntimeException("No hidden Lambda frame");
100         }
101 
102         if (option != null && reflects.isEmpty()) {
103             throw new RuntimeException("No reflection frame");
104         }
105     }
106 
checkFrame(StackFrame frame)107     void checkFrame(StackFrame frame) {
108         String cn = frame.getClassName();
109         if (cn.startsWith("java.lang.reflect.") || cn.startsWith("jdk.internal.reflect.")) {
110             reflects.add(frame);
111         }
112         if (cn.contains("$$Lambda$")) {
113             lambdas.add(frame);
114         }
115     }
116 }
117