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"); } }
public char[][] localVariableTypeNames() { try { StackFrame frame = getStackFrame(); Iterator variables = frame.visibleVariables().iterator(); Vector names = new Vector(); while (variables.hasNext()) { LocalVariable var = (LocalVariable) variables.next(); names.addElement(var.typeName().toCharArray()); } char[][] result = new char[names.size()][]; names.copyInto(result); return result; } catch (AbsentInformationException e) { return null; } }
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. }
/* * This function appears to take an exorbitant amount of time why why why */ public boolean setLocalVariableValue(int i, String name, Value sf) throws IncompatibleThreadStateException, InvalidTypeException, ClassNotLoadedException, AbsentInformationException { StackFrame frame = this.currentThread.frames().get(i); LocalVariable var = frame.visibleVariableByName(name); LOGGER.info("got var: " + var.typeName()); try { frame.setValue(var, sf); LOGGER.info("success setting new variable value"); return true; } catch (java.lang.ClassCastException e) { /* * KNOWN ISSUE: when checking type compatibility the debugger * requests the ClassLoader of the type of the variable. When an * object is loaded via reflection (using the current method) this * will return an ObjectReference. The debugger is expecting a * ClassLoaderReference and apparently the ObjectReference cannot be * cast to a ClassLoaderReference. */ LOGGER.info( "ClassCastException due to type assignments with classes loaded via reflection, work around is to load class again"); } return false; }