EA-49286 (CCE: SimplifiableIfStatementInspection.buildNegatedExpressionText)

This commit is contained in:
Bas Leijdekkers
2013-08-30 12:13:56 +02:00
parent 7bbb306308
commit 7d71ded718
4 changed files with 47 additions and 37 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2012 Bas Leijdekkers
* Copyright 2006-2013 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -165,16 +165,16 @@ public class SimplifiableIfStatementInspection extends BaseInspection {
final StringBuilder builder = new StringBuilder();
if (ParenthesesUtils.getPrecedence(expression) > precedence) {
builder.append('(');
getPresentableText(expression, builder);
appendPresentableText(expression, builder);
builder.append(')');
}
else {
getPresentableText(expression, builder);
appendPresentableText(expression, builder);
}
return builder.toString();
}
private static void getPresentableText(@Nullable PsiElement element, StringBuilder builder) {
private static void appendPresentableText(@Nullable PsiElement element, StringBuilder builder) {
if (element == null) {
return;
}
@@ -196,7 +196,7 @@ public class SimplifiableIfStatementInspection extends BaseInspection {
}
else {
for (PsiElement child : children) {
getPresentableText(child, builder);
appendPresentableText(child, builder);
}
}
}
@@ -220,40 +220,42 @@ public class SimplifiableIfStatementInspection extends BaseInspection {
}
if (ParenthesesUtils.getPrecedence(negated) > precedence) {
result.append('(');
getPresentableText(negated, result);
appendPresentableText(negated, result);
result.append(')');
}
else {
getPresentableText(negated, result);
appendPresentableText(negated, result);
}
}
else if (ComparisonUtils.isComparison(expression)) {
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)expression;
final IElementType tokenType = binaryExpression.getOperationTokenType();
final String negatedComparison = ComparisonUtils.getNegatedComparison(tokenType);
final PsiExpression lhs = binaryExpression.getLOperand();
final PsiExpression rhs = binaryExpression.getROperand();
if (ParenthesesUtils.getPrecedence(expression) > precedence) {
result.append('(');
getPresentableText(lhs, result);
result.append(negatedComparison);
getPresentableText(rhs, result);
result.append(')');
}
else {
getPresentableText(lhs, result);
result.append(negatedComparison);
getPresentableText(rhs, result);
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
final String negatedComparison = ComparisonUtils.getNegatedComparison(polyadicExpression.getOperationTokenType());
final PsiExpression[] operands = polyadicExpression.getOperands();
final boolean isEven = (operands.length & 1) != 1;
for (int i = 0, length = operands.length; i < length; i++) {
final PsiExpression operand = operands[i];
if (i > 0) {
if (isEven && (i & 1) != 1) {
final PsiJavaToken token = polyadicExpression.getTokenBeforeOperand(operand);
if (token != null) {
result.append(token.getText());
}
}
else {
result.append(negatedComparison);
}
}
appendPresentableText(operand, result);
}
}
else if (ParenthesesUtils.getPrecedence(expression) > ParenthesesUtils.PREFIX_PRECEDENCE) {
result.append("!(");
getPresentableText(expression, result);
appendPresentableText(expression, result);
result.append(')');
}
else {
result.append('!');
getPresentableText(expression, result);
appendPresentableText(expression, result);
}
return result.toString();
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,18 +29,13 @@ import java.util.Set;
public class ComparisonUtils {
private ComparisonUtils() {
super();
}
private ComparisonUtils() {}
private static final Set<IElementType> s_comparisonTokens =
new HashSet<IElementType>(6);
private static final Set<IElementType> s_comparisonTokens = new HashSet<IElementType>(6);
private static final Map<IElementType, String> s_swappedComparisons =
new HashMap<IElementType, String>(6);
private static final Map<IElementType, String> s_swappedComparisons = new HashMap<IElementType, String>(6);
private static final Map<IElementType, String> s_invertedComparisons =
new HashMap<IElementType, String>(6);
private static final Map<IElementType, String> s_invertedComparisons = new HashMap<IElementType, String>(6);
static {
s_comparisonTokens.add(JavaTokenType.EQEQ);
@@ -69,8 +64,8 @@ public class ComparisonUtils {
if (!(expression instanceof PsiPolyadicExpression)) {
return false;
}
final PsiPolyadicExpression binaryExpression = (PsiPolyadicExpression)expression;
final IElementType tokenType = binaryExpression.getOperationTokenType();
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)expression;
final IElementType tokenType = polyadicExpression.getOperationTokenType();
return isComparisonOperation(tokenType);
}
@@ -99,4 +99,10 @@ public class SimplifiableIfStatement {
}
}
boolean m(boolean b1, boolean b2, boolean b3, boolean b4, boolean i) {
if (b1 == b2 == b3 == b4) {
return false;
}
return i = true;
}
}
@@ -76,4 +76,11 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If statement may be replaced with &amp;&amp; or || expression</problem_class>
<description>&lt;code&gt;if&lt;/code&gt; statement can be replaced with 'return (a || b) &amp;&amp; (c || d);' #loc</description>
</problem>
<problem>
<file>SimplifiableIfStatement.java</file>
<line>103</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement may be replaced with &amp;&amp; or || expression</problem_class>
<description>&lt;code&gt;if&lt;/code&gt; statement can be replaced with 'return b1!=b2==b3!=b4 &amp;amp;&amp;amp; (i = true);' #loc</description>
</problem>
</problems>