Example #1
0
 protected static void replaceExpressionWithNegatedExpression(
     @NotNull PsiExpression newExpression, @NotNull PsiExpression expression) {
   final Project project = expression.getProject();
   final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
   PsiExpression expressionToReplace = expression;
   final String newExpressionText = newExpression.getText();
   final String expString;
   if (BoolUtils.isNegated(expression)) {
     expressionToReplace = BoolUtils.findNegation(expression);
     expString = newExpressionText;
   } else if (ComparisonUtils.isComparison(newExpression)) {
     final PsiBinaryExpression binaryExpression = (PsiBinaryExpression) newExpression;
     final String negatedComparison =
         ComparisonUtils.getNegatedComparison(binaryExpression.getOperationTokenType());
     final PsiExpression lhs = binaryExpression.getLOperand();
     final PsiExpression rhs = binaryExpression.getROperand();
     assert rhs != null;
     expString = lhs.getText() + negatedComparison + rhs.getText();
   } else {
     if (ParenthesesUtils.getPrecedence(newExpression) > ParenthesesUtils.PREFIX_PRECEDENCE) {
       expString = "!(" + newExpressionText + ')';
     } else {
       expString = '!' + newExpressionText;
     }
   }
   final PsiExpression newCall = factory.createExpressionFromText(expString, expression);
   assert expressionToReplace != null;
   final PsiElement insertedElement = expressionToReplace.replace(newCall);
   final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
   codeStyleManager.reformat(insertedElement);
 }
Example #2
0
 protected static void replaceExpressionWithNegatedExpressionString(
     @NotNull String newExpression, @NotNull PsiExpression expression) {
   final Project project = expression.getProject();
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiElementFactory factory = psiFacade.getElementFactory();
   PsiExpression expressionToReplace = expression;
   final String expString;
   if (BoolUtils.isNegated(expression)) {
     expressionToReplace = BoolUtils.findNegation(expressionToReplace);
     expString = newExpression;
   } else {
     PsiElement parent = expressionToReplace.getParent();
     while (parent instanceof PsiParenthesizedExpression) {
       expressionToReplace = (PsiExpression) parent;
       parent = parent.getParent();
     }
     expString = "!(" + newExpression + ')';
   }
   final PsiExpression newCall = factory.createExpressionFromText(expString, expression);
   assert expressionToReplace != null;
   final PsiElement insertedElement = expressionToReplace.replace(newCall);
   final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
   codeStyleManager.reformat(insertedElement);
 }
Example #3
0
  public void processIntention(PsiElement element) throws IncorrectOperationException {
    final PsiAssertStatement assertStatement = (PsiAssertStatement) element;
    assert assertStatement != null;
    final PsiExpression condition = assertStatement.getAssertCondition();
    final PsiExpression description = assertStatement.getAssertDescription();

    final String negatedConditionString = BoolUtils.getNegatedExpressionText(condition);
    @NonNls final String newStatement;
    if (description == null) {
      newStatement = "if(" + negatedConditionString + "){ throw new IllegalArgumentException();}";
    } else {
      newStatement =
          "if("
              + negatedConditionString
              + "){ throw new IllegalArgumentException("
              + description.getText()
              + ");}";
    }
    replaceStatement(newStatement, assertStatement);
  }