private void printVar(OutputSink out, LocalVariable var, StackFrame frame) { out.print(" " + var.name()); if (var.isVisible(frame)) { Value val = frame.getValue(var); out.println(" = " + val.toString()); } else { out.println(" is not in scope"); } }
private void commandLocals() throws NoSessionException { ThreadReference current = context.getCurrentThread(); if (current == null) { env.failure("No default thread specified: " + "use the \"thread\" command first."); return; } StackFrame frame; try { frame = context.getCurrentFrame(current); if (frame == null) { env.failure("Thread has not yet created any stack frames."); return; } } catch (VMNotInterruptedException e) { env.failure("Target VM must be in interrupted state."); return; } List<LocalVariable> vars; try { vars = frame.visibleVariables(); if (vars == null || vars.size() == 0) { env.failure("No local variables"); return; } } catch (AbsentInformationException e) { env.failure( "Local variable information not available." + " Compile with -g to generate variable information"); return; } OutputSink out = env.getOutputSink(); out.println("Method arguments:"); for (LocalVariable var : vars) { if (var.isArgument()) { printVar(out, var, frame); } } out.println("Local variables:"); for (LocalVariable var : vars) { if (!var.isArgument()) { printVar(out, var, frame); } } out.show(); return; }
void compareTwoDifferentVars(LocalVariable lv1, LocalVariable lv2) { if (!lv1.equals(lv2)) { println(" Success: difference of local vars detected"); } else { failure(" Failure: difference of local vars is NOT detected"); } if (lv1.hashCode() != lv2.hashCode()) { println(" Success: hashCode's of different local vars differ"); } else { failure(" Failure: hashCode's of different local vars are equal"); } if (lv1.compareTo(lv2) != 0) { println(" Success: compareTo() is correct for different local vars"); } else { failure(" Failure: compareTo() is NOT correct for different local vars"); } }
void compareTwoEqualVars(LocalVariable lv1, LocalVariable lv2) { if (lv1.equals(lv2)) { println(" Success: equality of local vars detected"); } else { failure(" Failure: equality of local vars is NOT detected"); } if (lv1.hashCode() == lv2.hashCode()) { println(" Success: hashCode's of equal local vars are equal"); } else { failure(" Failure: hashCode's of equal local vars differ"); } if (lv1.compareTo(lv2) == 0) { println(" Success: compareTo() is correct for equal local vars"); } else { failure(" Failure: compareTo() is NOT correct for equal local vars"); } }
void printVariable(LocalVariable lv, int index) throws Exception { if (lv == null) { println(" Var name: null"); return; } String tyname = lv.typeName(); println( " Var: " + lv.name() + ", index: " + index + ", type: " + tyname + ", Signature: " + lv.type().signature()); // Sorry, there is no way to take local variable slot numbers using JDI! // It is because method LocalVariableImpl.slot() is private. }
protected void runTests() throws Exception { /* * Get to the top of main() * to determine targetClass and mainThread */ BreakpointEvent bpe = startToMain("GetLocalVariables2Targ"); targetClass = bpe.location().declaringType(); mainThread = bpe.thread(); EventRequestManager erm = vm().eventRequestManager(); bpe = resumeTo("GetLocalVariables2Targ", "bar", "(I)Z"); /* * Inspect the stack frame for main(), not bar()... */ StackFrame frame = bpe.thread().frame(1); List localVars = frame.visibleVariables(); System.out.println(" Visible variables at this point are: "); for (Iterator it = localVars.iterator(); it.hasNext(); ) { LocalVariable lv = (LocalVariable) it.next(); System.out.print(lv.name()); System.out.print(" typeName: "); System.out.print(lv.typeName()); System.out.print(" signature: "); System.out.print(lv.type().signature()); System.out.print(" primitive type: "); System.out.println(lv.type().name()); if ("command".equals(lv.name())) { failure("Failure: LocalVariable \"command\" should not be visible at this point."); if (lv.isVisible(frame)) { System.out.println("Failure: \"command.isvisible(frame)\" returned true."); } } } /* * resume the target listening for events */ listenUntilVMDisconnect(); /* * deal with results of test * if anything has called failure("foo") testFailed will be true */ if (!testFailed) { println("GetLocalVariables2Test: passed"); } else { throw new Exception("GetLocalVariables2Test: failed"); } }