@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!ExpressionUtils.hasStringType(lhs)) { return; } final PsiExpression rhs = expression.getROperand(); if (rhs == null || !ExpressionUtils.hasStringType(rhs)) { return; } if (ExpressionUtils.isNullLiteral(lhs) || ExpressionUtils.isNullLiteral(rhs)) { return; } final PsiJavaToken sign = expression.getOperationSign(); registerError(sign); }
@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(); PsiReplacementUtil.replaceExpression(expression, rhsText + ' ' + flippedComparison + ' ' + lhsText); }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); final PsiExpression rhs = expression.getROperand(); if (!ComparisonUtils.isComparison(expression) || ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!isCharacter(lhs) || !isCharacter(rhs)) { return; } if (NonNlsUtils.isNonNlsAnnotated(lhs) || NonNlsUtils.isNonNlsAnnotated(rhs)) { return; } registerError(expression); }
@Override public void visitBinaryExpression( @NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (expression.getROperand() == null) { return; } if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); final PsiExpression rhs = expression.getROperand(); if (isModTwo(lhs) && hasValue(rhs, 1)) { registerError(expression, expression); } if (isModTwo(rhs) && hasValue(lhs, 1)) { registerError(expression, expression); } }
private static boolean isModTwo(PsiExpression exp) { if (!(exp instanceof PsiBinaryExpression)) { return false; } final PsiBinaryExpression binary = (PsiBinaryExpression)exp; final IElementType tokenType = binary.getOperationTokenType(); if (!JavaTokenType.PERC.equals(tokenType)) { return false; } final PsiExpression rhs = binary.getROperand(); final PsiExpression lhs = binary.getLOperand(); if (rhs == null) { return false; } return hasValue(rhs, 2) || hasValue(lhs, 2); }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); final PsiType lhsType = lhs.getType(); final PsiExpression rhs = expression.getROperand(); if (rhs == null) { return; } final PsiType rhsType = rhs.getType(); if (PsiType.SHORT.equals(lhsType) && PsiType.CHAR.equals(rhsType)) { registerError(expression); } else if (PsiType.CHAR.equals(lhsType) && PsiType.SHORT.equals(rhsType)) { registerError(expression); } }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); final PsiExpression rhs = expression.getROperand(); if (rhs == null) { return; } if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!TypeUtils.hasFloatingPointType(lhs) && !TypeUtils.hasFloatingPointType(rhs)) { return; } if (isInfinityOrZero(lhs) || isInfinityOrZero(rhs)) { return; } registerError(expression); }
public String getTextForElement(PsiElement element) { String operatorText = ""; String flippedOperatorText = ""; final PsiBinaryExpression expression = (PsiBinaryExpression)element; if (expression != null) { final PsiJavaToken sign = expression.getOperationSign(); operatorText = sign.getText(); flippedOperatorText = ComparisonUtils.getFlippedComparison(sign.getTokenType()); } if (operatorText.equals(flippedOperatorText)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message( "flip.comparison.intention.name", operatorText, flippedOperatorText); } }
public String getTextForElement(PsiElement element) { String operatorText = ""; String negatedOperatorText = ""; final PsiBinaryExpression exp = (PsiBinaryExpression)element; if (exp != null) { final PsiJavaToken sign = exp.getOperationSign(); operatorText = sign.getText(); negatedOperatorText = ComparisonUtils.getNegatedComparison(sign.getTokenType()); } if (operatorText.equals(negatedOperatorText)) { return IntentionPowerPackBundle.message( "negate.comparison.intention.name", operatorText); } else { return IntentionPowerPackBundle.message( "negate.comparison.intention.name1", operatorText, negatedOperatorText); } }
@Override public void visitBinaryExpression(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!ExpressionUtils.hasStringType(lhs)) { return; } final PsiExpression rhs = expression.getROperand(); if (rhs == null || !ExpressionUtils.hasStringType(rhs)) { return; } final String lhsText = lhs.getText(); if (PsiKeyword.NULL.equals(lhsText)) { return; } final String rhsText = rhs.getText(); if (PsiKeyword.NULL.equals(rhsText)) { return; } final PsiJavaToken sign = expression.getOperationSign(); registerError(sign); }
@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(@NotNull PsiBinaryExpression expression) { super.visitBinaryExpression(expression); final PsiExpression rhs = expression.getROperand(); if (rhs == null) { return; } if (!ComparisonUtils.isEqualityComparison(expression)) { return; } final PsiExpression lhs = expression.getLOperand(); if (!TypeUtils.hasFloatingPointType(lhs) && !TypeUtils.hasFloatingPointType(rhs)) { return; } if (ExpressionUtils.isZero(lhs) || ExpressionUtils.isZero(rhs)) { return; } registerError(expression); }
public String getTextForElement(PsiElement element) { String operatorText = ""; String flippedOperatorText = ""; final PsiBinaryExpression expression = (PsiBinaryExpression)element; if (expression != null) { final PsiJavaToken sign = expression.getOperationSign(); operatorText = sign.getText(); flippedOperatorText = ComparisonUtils.getFlippedComparison(sign); } if (operatorText.equals(flippedOperatorText)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message( "flip.comparison.intention.name", operatorText, flippedOperatorText); } }
public String getTextForElement(PsiElement element) { String operatorText = ""; String flippedOperatorText = ""; final PsiBinaryExpression expression = (PsiBinaryExpression) element; if(expression != null) { final PsiJavaToken sign = expression.getOperationSign(); operatorText = sign.getText(); flippedOperatorText = ComparisonUtils.getFlippedComparison(sign.getTokenType()); } if(operatorText.equals(flippedOperatorText)) { return IntentionPowerPackBundle.message("flip.smth.intention.name", operatorText); } else { return IntentionPowerPackBundle.message("flip.comparison.intention.name", operatorText, flippedOperatorText); } }
public PsiBinaryExpressionPattern left(@NotNull final ElementPattern pattern) { return with(new PatternCondition<PsiBinaryExpression>("left") { public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) { return pattern.accepts(psiBinaryExpression.getLOperand(), context); } }); }
public PsiBinaryExpressionPattern right(@NotNull final ElementPattern pattern) { return with(new PatternCondition<PsiBinaryExpression>("right") { public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) { return pattern.accepts(psiBinaryExpression.getROperand(), context); } }); }
public PsiBinaryExpressionPattern operation(final ElementPattern pattern) { return with(new PatternCondition<PsiBinaryExpression>("operation") { public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) { return pattern.accepts(psiBinaryExpression.getOperationSign(), context); } }); }
@Override public void visitElement(PsiElement element) { if (!myRefExprsInVisit.isEmpty() && myRefExprsInVisit.peek() == element) { myRefExprsInVisit.pop(); myRefExprsInVisit.push(null); } else if (element instanceof PsiBinaryExpression) { //implement smart traversing to avoid stack overflow if (!myBinaryExpressions.isEmpty() && myBinaryExpressions.peek() == element) { return; } PsiElement child = element.getFirstChild(); while (child != null) { if (child instanceof PsiBinaryExpression) { myBinaryExpressions.push((PsiBinaryExpression)child); } child.accept(this); child = child.getNextSibling(); if (child == null) { child = myBinaryExpressions.isEmpty() ? null : myBinaryExpressions.pop(); if (child != null) child = child.getFirstChild(); } } } else { element.acceptChildren(this); } }
@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 (!isConstantExpression(rhs) || isConstantExpression(lhs)) { 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 protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); if (!(element instanceof PsiBinaryExpression)) { return; } final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)element; final PsiExpression lhs = ParenthesesUtils.stripParentheses(binaryExpression.getLOperand()); final PsiExpression rhs = ParenthesesUtils.stripParentheses(binaryExpression.getROperand()); if (!(lhs instanceof PsiBinaryExpression) || !(rhs instanceof PsiBinaryExpression)) { return; } final PsiBinaryExpression lBinaryExpression = (PsiBinaryExpression)lhs; final PsiBinaryExpression rBinaryExpression = (PsiBinaryExpression)rhs; final PsiExpression llhs = ParenthesesUtils.stripParentheses(lBinaryExpression.getLOperand()); final PsiExpression lrhs = ParenthesesUtils.stripParentheses(rBinaryExpression.getLOperand()); if (llhs == null || lrhs == null) { return; } final PsiExpression thenExpression = ParenthesesUtils.stripParentheses(lBinaryExpression.getROperand()); final PsiExpression elseExpression = ParenthesesUtils.stripParentheses(rBinaryExpression.getROperand()); if (thenExpression == null || elseExpression == null) { return; } if (BoolUtils.isNegation(llhs) ) { PsiReplacementUtil.replaceExpression(binaryExpression, getText(lrhs) + '?' + getText(elseExpression) + ':' + getText(thenExpression)); } else { PsiReplacementUtil.replaceExpression(binaryExpression, getText(llhs) + '?' + getText(thenExpression) + ':' + getText(elseExpression)); } }
@Override public void visitBinaryExpression(PsiBinaryExpression expression) { super.visitBinaryExpression(expression); final IElementType tokenType = expression.getOperationTokenType(); if (!JavaTokenType.OROR.equals(tokenType)) { return; } final PsiExpression lhs = ParenthesesUtils.stripParentheses(expression.getLOperand()); final PsiExpression rhs = ParenthesesUtils.stripParentheses(expression.getROperand()); if (!(lhs instanceof PsiBinaryExpression) || !(rhs instanceof PsiBinaryExpression)) { return; } final PsiBinaryExpression lBinaryExpression = (PsiBinaryExpression)lhs; final PsiBinaryExpression rBinaryExpression = (PsiBinaryExpression)rhs; final IElementType lTokenType = lBinaryExpression.getOperationTokenType(); final IElementType rTokenType = rBinaryExpression.getOperationTokenType(); if (!JavaTokenType.ANDAND.equals(lTokenType) || !JavaTokenType.ANDAND.equals(rTokenType)) { return; } final PsiExpression expression1 = ParenthesesUtils.stripParentheses(lBinaryExpression.getLOperand()); final PsiExpression expression2 = ParenthesesUtils.stripParentheses(rBinaryExpression.getLOperand()); if (expression1 == null || expression2 == null || ParenthesesUtils.stripParentheses(lBinaryExpression.getROperand()) == null || ParenthesesUtils.stripParentheses(rBinaryExpression.getROperand()) == null) { return; } if (EquivalenceChecker.expressionsAreEquivalent(BoolUtils.getNegated(expression1), expression2) && !SideEffectChecker.mayHaveSideEffects(expression2)) { registerError(expression); } else if (EquivalenceChecker.expressionsAreEquivalent(expression1, BoolUtils.getNegated(expression2)) && !SideEffectChecker.mayHaveSideEffects(expression1)) { registerError(expression); } }
public boolean satisfiedBy(PsiElement element) { if (!(element instanceof PsiBinaryExpression)) { return false; } final PsiBinaryExpression expression = (PsiBinaryExpression)element; final PsiExpression rhs = expression.getROperand(); if (rhs == null) { return false; } if (!ComparisonUtils.isComparison((PsiExpression)element)) { return false; } return !ErrorUtil.containsError(element); }
public PsiBinaryExpressionPattern left(@NotNull final ElementPattern pattern) { return with(new PatternCondition<PsiBinaryExpression>("left") { public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) { return pattern.getCondition().accepts(psiBinaryExpression.getLOperand(), context); } }); }
public PsiBinaryExpressionPattern right(@NotNull final ElementPattern pattern) { return with(new PatternCondition<PsiBinaryExpression>("right") { public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) { return pattern.getCondition().accepts(psiBinaryExpression.getROperand(), context); } }); }
public PsiBinaryExpressionPattern operation(final ElementPattern pattern) { return with(new PatternCondition<PsiBinaryExpression>("operation") { public boolean accepts(@NotNull final PsiBinaryExpression psiBinaryExpression, final ProcessingContext context) { return pattern.getCondition().accepts(psiBinaryExpression.getOperationSign(), context); } }); }