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