fix CCE and cleanup

This commit is contained in:
Bas Leijdekkers
2012-09-05 11:54:41 +02:00
parent 9fd0d57386
commit 95b60b7080
4 changed files with 38 additions and 18 deletions
@@ -1974,7 +1974,7 @@ externalizable.without.public.no.arg.constructor.problem.descriptor=Externalizab
make.constructor.public=Make constructor 'public'
string.concatenation.missing.whitespace.display.name=String literal concatenation missing whitespace
string.concatenation.missing.whitespace.problem.descriptor=String literal concatenation missing whitespace #loc
string.concatenation.missing.whitespace.option=Ignore when not both sides are string literals
string.concatenation.missing.whitespace.option=Ignore when one or both sides are not string literals
negated.equality.expression.display.name=Negated equality expression
negated.equality.expression.problem.descriptor=Negating ''{0}'' #loc
negated.equality.expression.quickfix=Remove negation
@@ -21,7 +21,7 @@ import com.intellij.psi.tree.IElementType;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -83,9 +83,11 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
}
private boolean isMissingWhitespace(PsiExpression lhs, PsiExpression rhs) {
if (isLiteral(lhs) && hasStringType(lhs)) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression)lhs;
final String value = (String)literalExpression.getValue();
final boolean lhsIsString = hasStringType(lhs);
final PsiLiteralExpression lhsLiteral = ExpressionUtils.getLiteral(lhs);
final PsiLiteralExpression rhsLiteral = ExpressionUtils.getLiteral(rhs);
if (lhsLiteral != null && lhsIsString) {
final String value = (String)lhsLiteral.getValue();
if (value == null) {
return false;
}
@@ -98,13 +100,13 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
return false;
}
}
else if (ignoreNonStringLiterals || !isLiteral(rhs) || hasStringType(lhs)) {
else if (ignoreNonStringLiterals || rhsLiteral == null || lhsIsString) {
return false;
}
if (isLiteral(rhs) && hasStringType(rhs)) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression)rhs;
final String value = (String)literalExpression.getValue();
if ((value == null) || (value.length() == 0)) {
final boolean rhsIsString = hasStringType(rhs);
if (rhsLiteral != null && rhsIsString) {
final String value = (String)rhsLiteral.getValue();
if ((value == null) || value.isEmpty()) {
return false;
}
final char c = value.charAt(0);
@@ -112,17 +114,12 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
return false;
}
}
else if (ignoreNonStringLiterals || hasStringType(rhs)) {
else if (ignoreNonStringLiterals || rhsIsString) {
return false;
}
return true;
}
private boolean isLiteral(PsiExpression expression) {
expression = ParenthesesUtils.stripParentheses(expression);
return expression instanceof PsiLiteralExpression;
}
private boolean hasStringType(PsiExpression expression) {
final PsiType type = expression.getType();
return (type != null) && type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
@@ -26,14 +26,15 @@ import org.jetbrains.annotations.Nullable;
public class ExpressionUtils {
private ExpressionUtils() {
}
private ExpressionUtils() {}
@Nullable
public static Object computeConstantExpression(
@Nullable PsiExpression expression) {
return computeConstantExpression(expression, false);
}
@Nullable
public static Object computeConstantExpression(
@Nullable PsiExpression expression,
boolean throwConstantEvaluationOverflowException) {
@@ -161,6 +162,27 @@ public class ExpressionUtils {
return false;
}
@Nullable
public static PsiLiteralExpression getLiteral(@Nullable PsiExpression expression) {
expression = ParenthesesUtils.stripParentheses(expression);
if (expression instanceof PsiLiteralExpression) {
return (PsiLiteralExpression)expression;
}
if (!(expression instanceof PsiTypeCastExpression)) {
return null;
}
final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)expression;
final PsiExpression operand = ParenthesesUtils.stripParentheses(typeCastExpression.getOperand());
if (!(operand instanceof PsiTypeCastExpression)) {
return null;
}
return (PsiLiteralExpression)operand;
}
public static boolean isLiteral(@Nullable PsiExpression expression) {
return getLiteral(expression) != null;
}
public static boolean isEmptyStringLiteral(@Nullable PsiExpression expression) {
expression = ParenthesesUtils.stripParentheses(expression);
if (!(expression instanceof PsiLiteralExpression)) {
@@ -7,5 +7,6 @@ class Concatenations {
"FROM table");
System.out.println("no:" + i);
System.out.println("i" + i);
System.out.println("i" + ((String)"j"));
}
}