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 Class<?> clz = Class.forName("java.lang.LiveStackFrameInfo"); 61 Method method = clz.getMethod("getStackWalker"); 62 method.setAccessible(true); 63 extendedWalker = (StackWalker) method.invoke(null); 64 new LocalsAndOperands(extendedWalker, true).test(); 65 66 // no access to local and operands. 67 new LocalsAndOperands(StackWalker.create(), false).test(); 68 } 69 70 private final StackWalker walker; 71 private final boolean extended; 72 LocalsAndOperands(StackWalker walker, boolean extended) { 73 this.walker = walker; 74 this.extended = extended; 75 } 76 77 synchronized void test() throws Exception { 78 int x = 10; 79 char c = 'z'; 80 String hi = "himom"; 81 long l = 1000000L; 82 double d = 3.1415926; 83 84 List<StackWalker.StackFrame> frames = walker.walk(s -> s.collect(Collectors.toList())); 85 if (extended) { 86 for (StackWalker.StackFrame f : frames) { 87 System.out.println("frame: " + f); 88 Object[] locals = (Object[]) getLocals.invoke(f); 89 for (int i = 0; i < locals.length; i++) { 90 System.out.format("local %d: %s type %s%n", i, locals[i], type(locals[i])); 91 } 92 93 Object[] operands = (Object[]) getOperands.invoke(f); 94 for (int i = 0; i < operands.length; i++) { 95 System.out.format("operand %d: %s type %s%n", i, operands[i], type(operands[i])); 96 } 97 98 Object[] monitors = (Object[]) getMonitors.invoke(f); 99 for (int i = 0; i < monitors.length; i++) { 100 System.out.format("monitor %d: %s%n", i, monitors[i]); 101 } 102 } 103 } else { 104 for (StackFrame f : frames) { 105 if (liveStackFrameClass.isInstance(f)) 106 throw new RuntimeException("should not be LiveStackFrame"); 107 } 108 } 109 } 110 111 String type(Object o) throws Exception { 112 if (primitiveValueClass.isInstance(o)) { 113 char c = (char)primitiveType.invoke(o); 114 return String.valueOf(c); 115 } else { 116 return o.getClass().getName(); 117 } 118 } 119 }