/**
   * If the specified configuration element is activated under the current input.
   *
   * @param input The input object.
   * @param configuration The configuration element that defines the activation element.
   * @return true if it is activated.
   */
  private boolean isElementActivated(Object input, final IConfigurationElement configuration) {
    IConfigurationElement[] children = configuration.getChildren("activation"); // $NON-NLS-1$
    if (children == null || children.length == 0) return true;
    children = children[0].getChildren();
    if (children == null || children.length == 0) return true;
    final IConfigurationElement config = children[0];
    final EvaluationContext context = new EvaluationContext(null, input);
    context.addVariable("input", input); // $NON-NLS-1$
    context.setAllowPluginActivation(true);
    final boolean[] result = new boolean[1];
    SafeRunner.run(
        new SafeRunnable() {
          @Override
          public void handleException(Throwable e) {
            // Ignore exception
          }

          @Override
          public void run() throws Exception {
            Expression expression = ExpressionConverter.getDefault().perform(config);
            EvaluationResult evaluate = expression.evaluate(context);
            if (evaluate == EvaluationResult.TRUE) {
              result[0] = true;
            }
          }
        });
    return result[0];
  }
  protected void renameFooToFooBar(final XtextEditor contextEditor) throws Exception {
    contextEditor.getEditorSite().getPage().activate(contextEditor);
    waitForDisplay();
    IXtextDocument document = contextEditor.getDocument();
    final int offset = document.get().indexOf("foo");
    contextEditor.selectAndReveal(offset, 3);

    EvaluationContext evaluationContext = new EvaluationContext(null, new Object());
    evaluationContext.addVariable(ISources.ACTIVE_EDITOR_NAME, contextEditor);
    ExecutionEvent executionEvent = new ExecutionEvent(null, newHashMap(), null, evaluationContext);
    renameElementHandler.execute(executionEvent);
    //		syncUtil.totalSync(refactoringPreferences.isSaveAllBeforeRefactoring());
    //		IRenameElementContext context = document.readOnly(new IUnitOfWork<IRenameElementContext,
    // XtextResource>() {
    //			public IRenameElementContext exec(XtextResource state) throws Exception {
    //				EObject target = eObjectAtOffsetHelper.resolveElementAt(state, offset);
    //				return renameContextFactory.createRenameElementContext(target, contextEditor, new
    // TextSelection(offset,
    //						3), state);
    //			}
    //		});
    //		controller.initialize(context);
    //		waitForDisplay();
    //		controller.startRefactoring(RefactoringType.LINKED_EDITING);
    //		waitForDisplay();
    pressKeys(contextEditor, "fooBar\n");
    waitForDisplay();
    waitForReconciler(fooEditor);
    waitForReconciler(barEditor);
    waitForDisplay();
  }
  public boolean matches(IJavaProject javaProject) {
    if (fStatus != null) {
      return fStatus.booleanValue();
    }

    IConfigurationElement[] children =
        fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT);
    if (children.length == 1) {
      try {
        ExpressionConverter parser = ExpressionConverter.getDefault();
        Expression expression = parser.perform(children[0]);
        EvaluationContext evalContext = new EvaluationContext(null, javaProject);
        evalContext.addVariable("project", javaProject); // $NON-NLS-1$
        evalContext.addVariable(
            "sourceLevel", javaProject.getOption(JavaCore.COMPILER_SOURCE, true)); // $NON-NLS-1$
        return expression.evaluate(evalContext) == EvaluationResult.TRUE;
      } catch (CoreException e) {
        JavaPlugin.log(e);
      }
      return false;
    }
    fStatus = Boolean.FALSE;
    return false;
  }
Beispiel #4
0
  @SuppressWarnings("unchecked")
  public Object execute(ExecutionEvent event) throws ExecutionException {
    final MessageConsole b3Console = BeeLangConsoleUtils.getBeeLangConsole();
    final PrintStream b3ConsoleOutputStream = BeeLangConsoleUtils.getConsoleOutputStream(b3Console);
    try {
      b3ConsoleOutputStream.println("Running the main function...");
      EvaluationContext ctx = (EvaluationContext) event.getApplicationContext();
      List<ContentOutlineNode> nodes = (List<ContentOutlineNode>) ctx.getDefaultVariable();
      ContentOutlineNode node = nodes.get(0);
      Object result =
          node.getEObjectHandle()
              .readOnly(
                  new IUnitOfWork<Object, EObject>() {
                    // @Override
                    public Object exec(EObject state) throws Exception {
                      B3BuildEngine engine =
                          new B3BuildEngine(new ExecuteHandlerModule(b3ConsoleOutputStream));
                      try {
                        engine.defineBeeModel((BeeModel) state);
                      } catch (Throwable e) {
                        PrintStream b3ConsoleErrorStream =
                            BeeLangConsoleUtils.getConsoleErrorStream(b3Console);
                        try {
                          e.printStackTrace();
                          b3ConsoleErrorStream.println(
                              "Loading failed with error: "
                                  + e.getClass().getName().toString()
                                  + " : "
                                  + e.getMessage());
                          if (e.getCause() != null) {
                            b3ConsoleErrorStream.println("Caused by: " + e.getCause().getMessage());
                            return null;
                          }

                        } finally {
                          b3ConsoleErrorStream.close();
                        }
                      }
                      // get the resolution scope (in case a resolution is to be performed)
                      //
                      SharedScope resolutionScope = null;
                      // If resolving, run a resolution
                      if (isPerformResolve()) {
                        resolutionScope =
                            engine.getInjector().getInstance(B3BuildConstants.KEY_RESOLUTION_SCOPE);
                        resolutionScope.enter(); // !remember to call exit()

                        IStatus status = engine.resolveAllUnits();
                        if (!status.isOK()) {
                          PrintStream b3ConsoleErrorStream =
                              BeeLangConsoleUtils.getConsoleErrorStream(b3Console);
                          try {
                            // TODO: Better error reporting on failed resolution
                            b3ConsoleErrorStream.println("Resolution Failed with non OK status :");
                            b3ConsoleErrorStream.println(status.toString());

                          } finally {
                            b3ConsoleErrorStream.close();
                            // if(resolutionScope != null) // in case the error has to do with
                            // resolution scope
                            // resolutionScope.exit();
                            // resolutionScope = null;
                          }
                        }
                      }
                      // find a function called main (use the first found) and call it with a
                      // List<Object> argv
                      IFunction main = null;
                      for (IFunction f : ((BeeModel) state).getFunctions()) {
                        if ("main".equals(f.getName())) {
                          main = f;
                        }
                      }

                      if (main == null) return null;
                      final List<Object> argv = new ArrayList<Object>();
                      argv.add(engine);
                      try {
                        engine.callFunction(
                            // return engine.getContext().callFunction(
                            "main", new Object[] {argv}, new Type[] {List.class});
                      } catch (B3BackendException exprException) {
                        exprException.printStackTrace();
                        BExpression expr = exprException.getExpression();
                        int lineNumber = BeeLangUtils.getLineNumber(expr);

                        PrintStream b3ConsoleErrorStream =
                            BeeLangConsoleUtils.getConsoleErrorStream(b3Console);
                        try {
                          b3ConsoleErrorStream.println(exprException.getMessage());
                          b3ConsoleErrorStream.println(
                              "        at "
                                  + BeeLangUtils.closestNamedElement(expr)
                                  + "("
                                  + exprException.getLocationString()
                                  + ":"
                                  + lineNumber
                                  + ").");
                          if (exprException.getCause() != null) {
                            b3ConsoleErrorStream.println(
                                "Caused by: "
                                    + ((exprException.getCause().getMessage() == null)
                                        ? exprException.getCause().getClass().getName()
                                        : exprException.getCause().getMessage()));
                          }

                        } finally {
                          b3ConsoleErrorStream.close();
                        }

                      } catch (Throwable e) {
                        // Just print some errors
                        e.printStackTrace();
                        PrintStream b3ConsoleErrorStream =
                            BeeLangConsoleUtils.getConsoleErrorStream(b3Console);
                        try {
                          b3ConsoleErrorStream.println(e.getMessage());
                        } finally {
                          b3ConsoleErrorStream.close();
                        }
                      } finally {
                        if (resolutionScope != null) resolutionScope.exit();
                        resolutionScope = null;
                      }
                      return null;
                    }
                  });
      b3ConsoleOutputStream.println("Result = " + (result == null ? "null" : result.toString()));
      return null;
    } finally {
      b3ConsoleOutputStream.close();
    }
  }
Beispiel #5
0
 private void mockSelection(ISelection selection) {
   EvaluationContext context = new EvaluationContext(null, new Object());
   context.addVariable(ISources.ACTIVE_MENU_SELECTION_NAME, selection);
   when(handlerService.getCurrentState()).thenReturn(context);
 }