Example #1
0
 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");
   }
 }
 public Value getLocalVariableValue(StackFrame fr, String localName)
     throws AbsentInformationException, IncompatibleThreadStateException {
   Value ret = null;
   LocalVariable var = fr.visibleVariableByName(localName);
   if (var != null) {
     ret = fr.getValue(var);
   }
   return ret;
 }
 public StringReference getLocalVariableValueAsString(StackFrame fr, String localName)
     throws AbsentInformationException, IncompatibleThreadStateException {
   Value val = null;
   StringReference ret = null;
   LocalVariable var = fr.visibleVariableByName(localName);
   if (var != null) {
     val = fr.getValue(var);
     if (val instanceof StringReference) {
       ret = (StringReference) fr.getValue(var);
     } else {
       LOGGER.warn(
           "getLocalVariableValueAsString called with non-String Object: "
               + var.getClass().getName());
     }
   } else {
     LOGGER.warn("LocalVariable with name: " + localName + " was not found");
   }
   return ret;
 }
 public Value getLocalVariableValue(ThreadReference tr, int frameIdx, String localName)
     throws AbsentInformationException, IncompatibleThreadStateException {
   StackFrame fr = tr.frame(frameIdx);
   Value ret = null;
   LocalVariable var = fr.visibleVariableByName(localName);
   if (var != null) {
     ret = fr.getValue(var);
   }
   return ret;
 }
  public boolean run(String codeSnippetClassName) {
    ClassType codeSnippetClass;
    ObjectReference codeSnippet;
    Method method;
    List arguments;
    ObjectReference codeSnippetRunner;
    try {
      // Get the code snippet class
      List classes = jdiVM.classesByName(codeSnippetClassName);
      while (classes.size() == 0) {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        classes = jdiVM.classesByName(codeSnippetClassName);
        if (classes.size() == 0) {
          // workaround bug in Standard VM
          Iterator iterator = this.jdiVM.allClasses().iterator();
          while (iterator.hasNext()) {
            ReferenceType type = (ReferenceType) iterator.next();
            if (type.name().equals(codeSnippetClassName)) {
              classes = new ArrayList(1);
              classes.add(type);
              break;
            }
          }
        }
      }
      codeSnippetClass = (ClassType) classes.get(0);

      // Create a new code snippet
      Method constructor = (Method) codeSnippetClass.methodsByName("<init>").get(0);
      codeSnippet =
          codeSnippetClass.newInstance(
              jdiThread, constructor, new ArrayList(), ClassType.INVOKE_SINGLE_THREADED);

      // Install local variables and "this" into generated fields
      StackFrame stackFrame = getStackFrame();
      try {
        Iterator variables = stackFrame.visibleVariables().iterator();
        while (variables.hasNext()) {
          LocalVariable jdiVariable = (LocalVariable) variables.next();
          Value value = stackFrame.getValue(jdiVariable);
          Field field =
              codeSnippetClass.fieldByName(new String(LOCAL_VAR_PREFIX) + jdiVariable.name());
          codeSnippet.setValue(field, value);
        }
      } catch (AbsentInformationException e) {
        // No variables
      }
      Field delegateThis = codeSnippetClass.fieldByName(new String(DELEGATE_THIS));
      ObjectReference thisObject;
      if (delegateThis != null && ((thisObject = stackFrame.thisObject()) != null)) {
        codeSnippet.setValue(delegateThis, thisObject);
      }

      // Get the code snippet runner
      ClassType codeSnippetRunnerClass =
          (ClassType) jdiVM.classesByName(CODE_SNIPPET_RUNNER_CLASS_NAME).get(0);
      Field theRunner = codeSnippetRunnerClass.fieldByName(THE_RUNNER_FIELD);
      codeSnippetRunner = (ObjectReference) codeSnippetRunnerClass.getValue(theRunner);

      // Get the method 'runCodeSnippet' and its arguments
      method = (Method) codeSnippetRunnerClass.methodsByName(RUN_CODE_SNIPPET_METHOD).get(0);
      arguments = new ArrayList();
      arguments.add(codeSnippet);
    } catch (ClassNotLoadedException e) {
      e.printStackTrace();
      return false;
    } catch (IncompatibleThreadStateException e) {
      e.printStackTrace();
      return false;
    } catch (InvalidTypeException e) {
      e.printStackTrace();
      return false;
    } catch (InvocationException e) {
      e.printStackTrace();
      return false;
    }

    try {
      // Invoke runCodeSnippet(CodeSnippet)
      codeSnippetRunner.invokeMethod(
          jdiThread, method, arguments, ClassType.INVOKE_SINGLE_THREADED);

      // Retrieve values of local variables and put them back in the stack frame
      StackFrame stackFrame = getStackFrame();
      try {
        Iterator variables = stackFrame.visibleVariables().iterator();
        while (variables.hasNext()) {
          LocalVariable jdiVariable = (LocalVariable) variables.next();
          Field field =
              codeSnippetClass.fieldByName(new String(LOCAL_VAR_PREFIX) + jdiVariable.name());
          Value value = codeSnippet.getValue(field);
          stackFrame.setValue(jdiVariable, value);
        }
      } catch (AbsentInformationException e) {
        // No variables
      }
    } catch (ClassNotLoadedException e) {
      e.printStackTrace();
    } catch (IncompatibleThreadStateException e) {
      e.printStackTrace();
    } catch (InvalidTypeException e) {
      e.printStackTrace();
    } catch (InvocationException e) {
      e.printStackTrace();
    }
    return true;
  }