Exemplo n.º 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);
 }
 @Override
 public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) {
   super.visitBinaryExpression(expression);
   final PsiExpression rhs = expression.getROperand();
   if (!(rhs != null)) {
     return;
   }
   if (!ComparisonUtils.isComparison(expression)) {
     return;
   }
   if (ComparisonUtils.isEqualityComparison(expression)) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   if (!isCharacter(lhs)) {
     return;
   }
   if (!isCharacter(rhs)) {
     return;
   }
   if (NonNlsUtils.isNonNlsAnnotated(lhs) || NonNlsUtils.isNonNlsAnnotated(rhs)) {
     return;
   }
   registerError(expression);
 }
 @Override
 public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) {
   super.visitBinaryExpression(expression);
   if (!ComparisonUtils.isComparison(expression)) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   final PsiExpression rhs = expression.getROperand();
   if (rhs == null || !isConstantExpression(lhs) || isConstantExpression(rhs)) {
     return;
   }
   registerError(expression);
 }
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiBinaryExpression expression = (PsiBinaryExpression) descriptor.getPsiElement();
   final PsiExpression rhs = expression.getROperand();
   if (rhs == null) {
     return;
   }
   final String flippedComparison =
       ComparisonUtils.getFlippedComparison(expression.getOperationTokenType());
   if (flippedComparison == null) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   final String rhsText = rhs.getText();
   final String lhsText = lhs.getText();
   replaceExpression(expression, rhsText + ' ' + flippedComparison + ' ' + lhsText);
 }
 @Override
 public void visitBinaryExpression(PsiBinaryExpression expression) {
   super.visitBinaryExpression(expression);
   final PsiExpression rhs = expression.getROperand();
   if (rhs == null) {
     return;
   }
   if (!ComparisonUtils.isComparison(expression)) {
     return;
   }
   final PsiExpression lhs = expression.getLOperand();
   if (lhs instanceof PsiMethodCallExpression) {
     final PsiJavaToken sign = expression.getOperationSign();
     if (canBeReplacedByContains(lhs, sign, rhs, false)) {
       registerError(expression, expression);
     }
   } else if (rhs instanceof PsiMethodCallExpression) {
     final PsiJavaToken sign = expression.getOperationSign();
     if (canBeReplacedByContains(rhs, sign, lhs, true)) {
       registerError(expression, expression);
     }
   }
 }