コード例 #1
0
 private static boolean conditionalExpressionsAreEquivalent(
     @NotNull GrConditionalExpression condExp1, @NotNull GrConditionalExpression condExp2) {
   final GrExpression condition1 = condExp1.getCondition();
   final GrExpression condition2 = condExp2.getCondition();
   final GrExpression thenExpression1 = condExp1.getThenBranch();
   final GrExpression thenExpression2 = condExp2.getThenBranch();
   final GrExpression elseExpression1 = condExp1.getElseBranch();
   final GrExpression elseExpression2 = condExp2.getElseBranch();
   return expressionsAreEquivalent(condition1, condition2)
       && expressionsAreEquivalent(thenExpression1, thenExpression2)
       && expressionsAreEquivalent(elseExpression1, elseExpression2);
 }
  @Override
  protected void processIntention(@NotNull PsiElement element, Project project, Editor editor)
      throws IncorrectOperationException {
    if (!(element instanceof GrConditionalExpression)) {
      throw new IncorrectOperationException("Not invoked on a conditional");
    }
    GroovyPsiElementFactory groovyPsiElementFactory = GroovyPsiElementFactory.getInstance(project);
    GrConditionalExpression condExp = (GrConditionalExpression) element;
    GrExpression thenBranch = condExp.getThenBranch();
    GrExpression elseBranch = condExp.getElseBranch();

    Object thenVal = GroovyConstantExpressionEvaluator.evaluate(thenBranch);
    if (Boolean.TRUE.equals(thenVal) && elseBranch != null) {
      // aaa ? true : bbb -> aaa || bbb
      GrExpression conditionExp = condExp.getCondition();

      String conditionExpText = getStringToPutIntoOrExpression(conditionExp);
      String elseExpText = getStringToPutIntoOrExpression(elseBranch);
      String newExp = conditionExpText + "||" + elseExpText;
      int caretOffset = conditionExpText.length() + 2; // after "||"

      GrExpression expressionFromText =
          groovyPsiElementFactory.createExpressionFromText(newExp, condExp.getContext());

      expressionFromText = (GrExpression) condExp.replace(expressionFromText);

      editor
          .getCaretModel()
          .moveToOffset(expressionFromText.getTextOffset() + caretOffset); // just past "||"
      return;
    }

    Object elseVal = GroovyConstantExpressionEvaluator.evaluate(elseBranch);
    if (Boolean.FALSE.equals(elseVal) && thenBranch != null) {
      // aaa ? bbb : false -> aaa && bbb
      GrExpression conditionExp = condExp.getCondition();

      String conditionExpText = getStringToPutIntoAndExpression(conditionExp);
      String thenExpText = getStringToPutIntoAndExpression(thenBranch);

      String newExp = conditionExpText + "&&" + thenExpText;
      int caretOffset = conditionExpText.length() + 2; // after "&&"
      GrExpression expressionFromText =
          groovyPsiElementFactory.createExpressionFromText(newExp, condExp.getContext());

      expressionFromText = (GrExpression) condExp.replace(expressionFromText);

      editor
          .getCaretModel()
          .moveToOffset(expressionFromText.getTextOffset() + caretOffset); // just past "&&"
    }
  }
コード例 #3
0
 public void visitConditionalExpression(GrConditionalExpression grConditionalExpression) {
   super.visitConditionalExpression(grConditionalExpression);
   final GrExpression condition = grConditionalExpression.getCondition();
   if (!BoolUtils.isNegation(condition)) {
     return;
   }
   registerError(grConditionalExpression);
 }
  private static String calculateReplacementExpression(GrConditionalExpression exp) {
    final GrExpression thenExpression = exp.getThenBranch();
    final GrExpression elseExpression = exp.getElseBranch();
    final GrExpression condition = exp.getCondition();

    if (isFalse(thenExpression) && isTrue(elseExpression)) {
      return BoolUtils.getNegatedExpressionText(condition);
    } else {
      return condition.getText();
    }
  }
コード例 #5
0
  @Override
  public void visitConditionalExpression(GrConditionalExpression expression) {
    GrExpression condition = expression.getCondition();
    GrExpression thenBranch = expression.getThenBranch();
    GrExpression elseBranch = expression.getElseBranch();

    condition.accept(this);
    InstructionImpl conditionEnd = myHead;
    List<GotoInstruction> negations = collectAndRemoveAllPendingNegations(expression);

    if (thenBranch != null) {
      thenBranch.accept(this);
      handlePossibleReturn(thenBranch);
      addPendingEdge(expression, myHead);
    }

    if (elseBranch != null) {
      InstructionImpl head = reduceAllNegationsIntoInstruction(expression, negations);
      myHead = head != null ? head : conditionEnd;
      elseBranch.accept(this);
      handlePossibleReturn(elseBranch);
    }
  }
    @Override
    public void visitConditionalExpression(GrConditionalExpression exp) {
      super.visitConditionalExpression(exp);
      final GrExpression condition = exp.getCondition();
      final PsiType type = condition.getType();
      if (type == null || !(PsiType.BOOLEAN.isAssignableFrom(type))) {
        return;
      }

      if (ErrorUtil.containsError(exp)) return;

      final GrExpression thenExpression = exp.getThenBranch();
      if (thenExpression == null) {
        return;
      }
      final GrExpression elseExpression = exp.getElseBranch();
      if (elseExpression == null) {
        return;
      }
      if ((isFalse(thenExpression) && isTrue(elseExpression))
          || (isTrue(thenExpression) && isFalse(elseExpression))) {
        registerError(exp);
      }
    }