diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index 2b65e19a769d..dfcef2f582c9 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -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 diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/StringConcatenationMissingWhitespaceInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/StringConcatenationMissingWhitespaceInspection.java index 7f58b3ef4d0e..f873a10f43bb 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/StringConcatenationMissingWhitespaceInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/StringConcatenationMissingWhitespaceInspection.java @@ -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); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ExpressionUtils.java b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ExpressionUtils.java index 5489da1d27c9..62f13b74e790 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ExpressionUtils.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/psiutils/ExpressionUtils.java @@ -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)) { diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/string_concatenation_missing_whitespace/Concatenations.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/string_concatenation_missing_whitespace/Concatenations.java index 22f32e5eb3f4..c3f495333a7f 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/string_concatenation_missing_whitespace/Concatenations.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/string_concatenation_missing_whitespace/Concatenations.java @@ -7,5 +7,6 @@ class Concatenations { "FROM table"); System.out.println("no:" + i); System.out.println("i" + i); + System.out.println("i" + ((String)"j")); } } \ No newline at end of file