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 } | 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 // check for non-null locals in LocalsAndOperands.test() 92 if (f.getClassName().equals("LocalsAndOperands") && 93 f.getMethodName().equals("test")) { 94 if (locals[i] == null) { 95 throw new RuntimeException("kept-alive locals should not be null"); 96 } 97 } 98 } 99 100 Object[] operands = (Object[]) getOperands.invoke(f); 101 for (int i = 0; i < operands.length; i++) { 102 System.out.format(" operand %d: %s type %s%n", i, operands[i], 103 type(operands[i])); 104 } 105 106 Object[] monitors = (Object[]) getMonitors.invoke(f); 107 for (int i = 0; i < monitors.length; i++) { 108 System.out.format(" monitor %d: %s%n", i, monitors[i]); 109 } 110 } 111 } else { 112 for (StackFrame f : frames) { 113 if (liveStackFrameClass.isInstance(f)) { 114 throw new RuntimeException("should not be LiveStackFrame"); 115 } 116 } 117 } 118 // Use local variables so they stay alive 119 System.out.println("Stayin' alive: "+x+" "+c+" "+hi+" "+l+" "+d); 120 } 121 122 String type(Object o) throws Exception { 123 if (o == null) { 124 return "null"; 125 } else if (primitiveValueClass.isInstance(o)) { 126 char c = (char)primitiveType.invoke(o); 127 return String.valueOf(c); 128 } else { 129 return o.getClass().getName(); 130 } 131 } 132 } |