Beispiel #1
0
  @Override
  public Value evaluate(final EvaluationContext evaluationContext) throws EvaluateException {
    DebugProcess process = evaluationContext.getDebugProcess();

    ClassLoaderReference classLoader;
    try {
      classLoader = getClassLoader(evaluationContext, process);
    } catch (Exception e) {
      throw new EvaluateException("Error creating evaluation class loader: " + e, e);
    }

    String version = ((VirtualMachineProxyImpl) process.getVirtualMachineProxy()).version();
    JavaSdkVersion sdkVersion = JdkVersionUtil.getVersion(version);
    Collection<OutputFileObject> classes =
        compile(sdkVersion != null ? sdkVersion.getDescription() : null);

    try {
      defineClasses(classes, evaluationContext, process, classLoader);
    } catch (Exception e) {
      throw new EvaluateException("Error during classes definition " + e, e);
    }

    try {
      // invoke base evaluator on call code
      final Project project =
          ApplicationManager.getApplication()
              .runReadAction(
                  new Computable<Project>() {
                    @Override
                    public Project compute() {
                      return myPsiContext.getProject();
                    }
                  });
      ExpressionEvaluator evaluator =
          DebuggerInvocationUtil.commitAndRunReadAction(
              project,
              new EvaluatingComputable<ExpressionEvaluator>() {
                @Override
                public ExpressionEvaluator compute() throws EvaluateException {
                  final TextWithImports callCode = getCallCode();
                  PsiElement copyContext = myData.getAnchor();
                  final CodeFragmentFactory factory =
                      DebuggerUtilsEx.findAppropriateCodeFragmentFactory(callCode, copyContext);
                  return factory
                      .getEvaluatorBuilder()
                      .build(
                          factory.createCodeFragment(callCode, copyContext, project),
                          ContextUtil.getSourcePosition(evaluationContext));
                }
              });
      ((EvaluationContextImpl) evaluationContext).setClassLoader(classLoader);
      return evaluator.evaluate(evaluationContext);
    } catch (Exception e) {
      throw new EvaluateException("Error during generated code invocation " + e, e);
    }
  }
  private void runAction(final EvaluationContextImpl context, LocatableEvent event) {
    if (LOG_ENABLED || LOG_EXPRESSION_ENABLED) {
      final StringBuilder buf = StringBuilderSpinAllocator.alloc();
      try {
        if (LOG_ENABLED) {
          buf.append(getEventMessage(event));
          buf.append("\n");
        }
        final DebugProcessImpl debugProcess = context.getDebugProcess();
        final TextWithImports expressionToEvaluate = getLogMessage();
        if (LOG_EXPRESSION_ENABLED
            && expressionToEvaluate != null
            && !"".equals(expressionToEvaluate.getText())) {
          if (!debugProcess.isAttached()) {
            return;
          }

          try {
            ExpressionEvaluator evaluator =
                DebuggerInvocationUtil.commitAndRunReadAction(
                    getProject(),
                    new EvaluatingComputable<ExpressionEvaluator>() {
                      public ExpressionEvaluator compute() throws EvaluateException {
                        return EvaluatorBuilderImpl.build(
                            expressionToEvaluate,
                            ContextUtil.getContextElement(context),
                            ContextUtil.getSourcePosition(context));
                      }
                    });
            final Value eval = evaluator.evaluate(context);
            final String result =
                eval instanceof VoidValue ? "void" : DebuggerUtils.getValueAsString(context, eval);
            buf.append(result);
          } catch (EvaluateException e) {
            buf.append(DebuggerBundle.message("error.unable.to.evaluate.expression"));
            buf.append(" \"");
            buf.append(expressionToEvaluate);
            buf.append("\"");
            buf.append(" : ");
            buf.append(e.getMessage());
          }
          buf.append("\n");
        }
        if (buf.length() > 0) {
          debugProcess.printToConsole(buf.toString());
        }
      } finally {
        StringBuilderSpinAllocator.dispose(buf);
      }
    }
  }