Ejemplo n.º 1
0
 private static boolean postfixExpressionsAreEquivalent(
     @NotNull PsiPostfixExpression postfixExp1, @NotNull PsiPostfixExpression postfixExp2) {
   final IElementType tokenType1 = postfixExp1.getOperationTokenType();
   if (!tokenType1.equals(postfixExp2.getOperationTokenType())) {
     return false;
   }
   final PsiExpression operand1 = postfixExp1.getOperand();
   final PsiExpression operand2 = postfixExp2.getOperand();
   return expressionsAreEquivalent(operand1, operand2);
 }
 @Override
 public void visitPostfixExpression(@NotNull PsiPostfixExpression postfixExpression) {
   if (read || written) {
     return;
   }
   super.visitPostfixExpression(postfixExpression);
   final IElementType tokenType = postfixExpression.getOperationTokenType();
   if (!tokenType.equals(JavaTokenType.PLUSPLUS)
       && !tokenType.equals(JavaTokenType.MINUSMINUS)) {
     return;
   }
   final PsiExpression operand = postfixExpression.getOperand();
   if (!(operand instanceof PsiReferenceExpression)) {
     return;
   }
   final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand;
   final PsiElement target = referenceExpression.resolve();
   if (!variable.equals(target)) {
     return;
   }
   written = true;
 }
 public void visitPostfixExpression(PsiPostfixExpression expression) {
   super.visitPostfixExpression(expression);
   checkSetterNeeded(expression.getOperand(), expression.getOperationSign());
 }
 private static void removeParensFromPostfixExpression(
     @NotNull PsiPostfixExpression postfixExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression operand = postfixExpression.getOperand();
   removeParentheses(operand, ignoreClarifyingParentheses);
 }
 @Override
 public void doFix(Project project, ProblemDescriptor descriptor)
     throws IncorrectOperationException {
   final PsiExpression expression = (PsiExpression) descriptor.getPsiElement();
   final PsiType type = expression.getType();
   if (type == null) {
     return;
   }
   final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(type);
   if (unboxedType == null) {
     return;
   }
   final String newExpressionText = buildNewExpressionText(expression, unboxedType);
   final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
   final PsiElementFactory factory = psiFacade.getElementFactory();
   final PsiElement parent = expression.getParent();
   final String expressionText = expression.getText();
   if (parent instanceof PsiPrefixExpression && !unboxedType.equalsToText("boolean")) {
     final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) parent;
     final IElementType tokenType = prefixExpression.getOperationTokenType();
     if (JavaTokenType.PLUSPLUS.equals(tokenType)) {
       replaceExpression(prefixExpression, expressionText + '=' + newExpressionText + "+1");
     } else if (JavaTokenType.MINUSMINUS.equals(tokenType)) {
       replaceExpression(prefixExpression, expressionText + '=' + newExpressionText + "-1");
     } else {
       replaceExpression(
           prefixExpression, prefixExpression.getOperationSign().getText() + newExpressionText);
     }
   } else if (parent instanceof PsiPostfixExpression) {
     final PsiPostfixExpression postfixExpression = (PsiPostfixExpression) parent;
     final IElementType tokenType = postfixExpression.getOperationTokenType();
     final PsiElement grandParent = postfixExpression.getParent();
     if (grandParent instanceof PsiExpressionStatement) {
       if (JavaTokenType.PLUSPLUS.equals(tokenType)) {
         replaceExpression(postfixExpression, expressionText + '=' + newExpressionText + "+1");
       } else if (JavaTokenType.MINUSMINUS.equals(tokenType)) {
         replaceExpression(postfixExpression, expressionText + '=' + newExpressionText + "-1");
       }
     } else {
       final PsiElement element = postfixExpression.replace(postfixExpression.getOperand());
       final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
       if (statement == null) {
         return;
       }
       final PsiStatement newStatement;
       if (JavaTokenType.PLUSPLUS.equals(tokenType)) {
         newStatement =
             factory.createStatementFromText(
                 expressionText + '=' + newExpressionText + "+1;", statement);
       } else {
         newStatement =
             factory.createStatementFromText(
                 expressionText + '=' + newExpressionText + "-1;", statement);
       }
       final PsiElement greatGrandParent = statement.getParent();
       greatGrandParent.addAfter(newStatement, statement);
     }
   } else if (parent instanceof PsiAssignmentExpression) {
     final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) parent;
     final PsiExpression lExpression = assignmentExpression.getLExpression();
     if (expression.equals(lExpression)) {
       final PsiJavaToken operationSign = assignmentExpression.getOperationSign();
       final String operationSignText = operationSign.getText();
       final char sign = operationSignText.charAt(0);
       final PsiExpression rExpression = assignmentExpression.getRExpression();
       if (rExpression == null) {
         return;
       }
       final String text =
           lExpression.getText() + '=' + newExpressionText + sign + rExpression.getText();
       final PsiExpression newExpression =
           factory.createExpressionFromText(text, assignmentExpression);
       assignmentExpression.replace(newExpression);
     } else {
       replaceExpression(expression, newExpressionText);
     }
   } else {
     replaceExpression(expression, newExpressionText);
   }
 }