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 Sanity test for locals and operands
  28  * @run main LocalsAndOperands
  29  */
  30 
  31 import java.lang.StackWalker.StackFrame;
  32 import java.lang.reflect.*;
  33 import java.util.List;
  34 import java.util.stream.Collectors;
  35 
  36 public class LocalsAndOperands {
  37     static Class<?> liveStackFrameClass;
  38     static Class<?> primitiveValueClass;
  39     static StackWalker extendedWalker;
  40     static Method getLocals;
  41     static Method getOperands;
  42     static Method getMonitors;
  43     static Method primitiveType;
  44     public static void main(String... args) throws Exception {
  45         liveStackFrameClass = Class.forName("java.lang.LiveStackFrame");
  46         primitiveValueClass = Class.forName("java.lang.LiveStackFrame$PrimitiveValue");
  47 
  48         getLocals = liveStackFrameClass.getDeclaredMethod("getLocals");
  49         getLocals.setAccessible(true);
  50 
  51         getOperands = liveStackFrameClass.getDeclaredMethod("getStack");
  52         getOperands.setAccessible(true);
  53 
  54         getMonitors = liveStackFrameClass.getDeclaredMethod("getMonitors");
  55         getMonitors.setAccessible(true);
  56 
  57         primitiveType = primitiveValueClass.getDeclaredMethod("type");
  58         primitiveType.setAccessible(true);
  59 
  60         Method method = liveStackFrameClass.getMethod("getStackWalker");
  61         method.setAccessible(true);
  62         extendedWalker = (StackWalker) method.invoke(null);
  63         new LocalsAndOperands(extendedWalker, true).test();
  64 
  65         // no access to local and operands.
  66         new LocalsAndOperands(StackWalker.getInstance(), false).test();
  67     }
  68 
  69     private final StackWalker walker;
  70     private final boolean extended;
  71     LocalsAndOperands(StackWalker walker, boolean extended) {
  72         this.walker = walker;
  73         this.extended = extended;
  74     }
  75 
  76     synchronized void test() throws Exception {
  77         int x = 10;
  78         char c = 'z';
  79         String hi = "himom";
  80         long l = 1000000L;
  81         double d =  3.1415926;
  82 
  83         List<StackWalker.StackFrame> frames = walker.walk(s -> s.collect(Collectors.toList()));
  84         if (extended) {
  85             for (StackWalker.StackFrame f : frames) {
  86                 System.out.println("frame: " + f);
  87                 Object[] locals = (Object[]) getLocals.invoke(f);
  88                 for (int i = 0; i < locals.length; i++) {
  89                     System.out.format("local %d: %s type %s%n", i, locals[i], type(locals[i]));
  90                 }
  91 
  92                 Object[] operands = (Object[]) getOperands.invoke(f);
  93                 for (int i = 0; i < operands.length; i++) {
  94                     System.out.format("operand %d: %s type %s%n", i, operands[i], type(operands[i]));
  95                 }
  96 
  97                 Object[] monitors = (Object[]) getMonitors.invoke(f);
  98                 for (int i = 0; i < monitors.length; i++) {
  99                     System.out.format("monitor %d: %s%n", i, monitors[i]);
 100                 }
 101             }
 102         } else {
 103             for (StackFrame f : frames) {
 104                 if (liveStackFrameClass.isInstance(f))
 105                     throw new RuntimeException("should not be LiveStackFrame");
 106             }
 107         }
 108     }
 109 
 110     String type(Object o) throws Exception {
 111         if (primitiveValueClass.isInstance(o)) {
 112             char c = (char)primitiveType.invoke(o);
 113             return String.valueOf(c);
 114         } else {
 115             return o.getClass().getName();
 116         }
 117     }
 118 }