public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      try {
        PsiBinaryExpression binaryExpression = (PsiBinaryExpression) descriptor.getPsiElement();
        IElementType opSign = binaryExpression.getOperationSign().getTokenType();
        PsiExpression lExpr = binaryExpression.getLOperand();
        PsiExpression rExpr = binaryExpression.getROperand();
        if (rExpr == null) return;

        PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
        PsiMethodCallExpression equalsCall =
            (PsiMethodCallExpression) factory.createExpressionFromText("a.equals(b)", null);

        equalsCall.getMethodExpression().getQualifierExpression().replace(lExpr);
        equalsCall.getArgumentList().getExpressions()[0].replace(rExpr);

        PsiExpression result = (PsiExpression) binaryExpression.replace(equalsCall);

        if (opSign == JavaTokenType.NE) {
          PsiPrefixExpression negation =
              (PsiPrefixExpression) factory.createExpressionFromText("!a", null);
          negation.getOperand().replace(result);
          result.replace(negation);
        }
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
Ejemplo n.º 2
0
 @Nullable
 private static String getExprText(PsiExpression expr, PsiLocalVariable localVariable) {
   final String exprText = expr != null ? expr.getText() : null;
   if (localVariable != null) {
     final PsiExpression initializer = localVariable.getInitializer();
     return initializer != null ? initializer.getText() : exprText;
   } else {
     return exprText;
   }
 }
 private static boolean isNullLiteral(PsiExpression expr) {
   return expr instanceof PsiLiteralExpression && "null".equals(expr.getText());
 }