This commit is contained in:
Bas Leijdekkers
2012-10-22 15:06:21 +02:00
parent 519585b6f4
commit a21e84f0d8
14 changed files with 146 additions and 260 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -21,8 +21,8 @@ import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.FormatUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
public class MalformedFormatStringInspection extends BaseInspection {
@@ -30,8 +30,7 @@ public class MalformedFormatStringInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"malformed.format.string.display.name");
return InspectionGadgetsBundle.message("malformed.format.string.display.name");
}
@Override
@@ -39,21 +38,17 @@ public class MalformedFormatStringInspection extends BaseInspection {
public String buildErrorString(Object... infos) {
final Object value = infos[0];
if (value instanceof Exception) {
return InspectionGadgetsBundle.message(
"malformed.format.string.problem.descriptor.malformed");
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.malformed");
}
final Validator[] validators = (Validator[])value;
final int argumentCount = ((Integer)infos[1]).intValue();
if (validators.length < argumentCount) {
return InspectionGadgetsBundle.message(
"malformed.format.string.problem.descriptor.too.many.arguments");
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.many.arguments");
}
if (validators.length > argumentCount) {
return InspectionGadgetsBundle.message(
"malformed.format.string.problem.descriptor.too.few.arguments");
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.too.few.arguments");
}
return InspectionGadgetsBundle.message(
"malformed.format.string.problem.descriptor.arguments.do.not.match.type");
return InspectionGadgetsBundle.message("malformed.format.string.problem.descriptor.arguments.do.not.match.type");
}
@Override
@@ -66,12 +61,10 @@ public class MalformedFormatStringInspection extends BaseInspection {
return new MalformedFormatStringVisitor();
}
private static class MalformedFormatStringVisitor
extends BaseInspectionVisitor {
private static class MalformedFormatStringVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
@NotNull PsiMethodCallExpression expression) {
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!FormatUtils.isFormatCall(expression)) {
return;
@@ -87,30 +80,28 @@ public class MalformedFormatStringInspection extends BaseInspection {
return;
}
final int formatArgumentIndex;
if ("java.util.Locale".equals(type.getCanonicalText())
&& arguments.length > 1) {
if ("java.util.Locale".equals(type.getCanonicalText()) && arguments.length > 1) {
formatArgumentIndex = 1;
}
else {
formatArgumentIndex = 0;
}
final PsiExpression formatArgument = arguments[formatArgumentIndex];
if (!TypeUtils.expressionHasType(formatArgument,
CommonClassNames.JAVA_LANG_STRING)) {
if (!ExpressionUtils.hasStringType(formatArgument)) {
return;
}
if (!PsiUtil.isConstantExpression(formatArgument)) {
return;
}
final PsiType formatType = formatArgument.getType();
final String value =
(String)ConstantExpressionUtil.computeCastTo(
formatArgument, formatType);
if (formatType == null) {
return;
}
final String value = (String)ConstantExpressionUtil.computeCastTo(formatArgument, formatType);
if (value == null) {
return;
}
final int argumentCount =
arguments.length - (formatArgumentIndex + 1);
final int argumentCount = arguments.length - (formatArgumentIndex + 1);
final Validator[] validators;
try {
validators = FormatDecode.decode(value, argumentCount);
@@ -121,24 +112,23 @@ public class MalformedFormatStringInspection extends BaseInspection {
}
if (validators.length != argumentCount) {
if (argumentCount == 1) {
final PsiExpression argument =
arguments[formatArgumentIndex + 1];
final PsiExpression argument = arguments[formatArgumentIndex + 1];
final PsiType argumentType = argument.getType();
if (argumentType instanceof PsiArrayType) {
return;
}
}
registerError(formatArgument, validators,
Integer.valueOf(argumentCount));
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
return;
}
for (int i = 0; i < validators.length; i++) {
final Validator validator = validators[i];
final PsiType argumentType =
arguments[i + formatArgumentIndex + 1].getType();
final PsiType argumentType = arguments[i + formatArgumentIndex + 1].getType();
if (argumentType == null) {
continue;
}
if (!validator.valid(argumentType)) {
registerError(formatArgument, validators,
Integer.valueOf(argumentCount));
registerError(formatArgument, validators, Integer.valueOf(argumentCount));
return;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -21,8 +21,8 @@ import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.MethodCallUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Pattern;
@@ -33,21 +33,17 @@ public class MalformedRegexInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"malformed.regular.expression.display.name");
return InspectionGadgetsBundle.message("malformed.regular.expression.display.name");
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
if (infos.length == 0) {
return InspectionGadgetsBundle.message(
"malformed.regular.expression.problem.descriptor1");
return InspectionGadgetsBundle.message("malformed.regular.expression.problem.descriptor1");
}
else {
return InspectionGadgetsBundle.message(
"malformed.regular.expression.problem.descriptor2",
infos[0]);
return InspectionGadgetsBundle.message("malformed.regular.expression.problem.descriptor2", infos[0]);
}
}
@@ -64,8 +60,7 @@ public class MalformedRegexInspection extends BaseInspection {
private static class MalformedRegexVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
@NotNull PsiMethodCallExpression expression) {
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
@@ -76,16 +71,14 @@ public class MalformedRegexInspection extends BaseInspection {
return;
}
final PsiExpression argument = arguments[0];
if (!TypeUtils.expressionHasType(argument,
CommonClassNames.JAVA_LANG_STRING)) {
if (!ExpressionUtils.hasStringType(argument)) {
return;
}
if (!PsiUtil.isConstantExpression(argument)) {
return;
}
final PsiType regexType = argument.getType();
final String value = (String)
ConstantExpressionUtil.computeCastTo(argument, regexType);
final String value = (String)ConstantExpressionUtil.computeCastTo(argument, regexType);
if (value == null) {
return;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -21,7 +21,7 @@ import com.intellij.psi.util.PsiUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -47,15 +47,13 @@ public class MalformedXPathInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"malformed.xpath.expression.display.name");
return InspectionGadgetsBundle.message("malformed.xpath.expression.display.name");
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"malformed.xpath.expression.problem.description");
return InspectionGadgetsBundle.message("malformed.xpath.expression.problem.description");
}
@Override
@@ -71,8 +69,7 @@ public class MalformedXPathInspection extends BaseInspection {
private static class MalformedXPathVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
@NotNull PsiMethodCallExpression expression) {
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
@@ -80,17 +77,17 @@ public class MalformedXPathInspection extends BaseInspection {
return;
}
final PsiExpression xpathArgument = arguments[0];
if (!TypeUtils.expressionHasType(xpathArgument,
CommonClassNames.JAVA_LANG_STRING)) {
if (!ExpressionUtils.hasStringType(xpathArgument)) {
return;
}
if (!PsiUtil.isConstantExpression(xpathArgument)) {
return;
}
final PsiType type = xpathArgument.getType();
final String value =
(String)ConstantExpressionUtil.computeCastTo(xpathArgument,
type);
if (type == null) {
return;
}
final String value = (String)ConstantExpressionUtil.computeCastTo(xpathArgument, type);
if (value == null) {
return;
}
@@ -108,10 +105,8 @@ public class MalformedXPathInspection extends BaseInspection {
}
}
private static boolean callTakesXPathExpression(
PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
private static boolean callTakesXPathExpression(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final String name = methodExpression.getReferenceName();
if (!xpathMethodNames.contains(name)) {
return false;
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2011 Bas Leijdekkers
* Copyright 2010-2012 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,35 +29,31 @@ import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class StringConcatenationInMessageFormatCallInspection
extends BaseInspection {
public class StringConcatenationInMessageFormatCallInspection extends BaseInspection {
@Nls
@NotNull
@Override
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"string.concatenation.in.message.format.call.display.name");
return InspectionGadgetsBundle.message("string.concatenation.in.message.format.call.display.name");
}
@NotNull
@Override
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"string.concatenation.in.message.format.call.problem.descriptor");
return InspectionGadgetsBundle.message("string.concatenation.in.message.format.call.problem.descriptor");
}
@Override
protected InspectionGadgetsFix buildFix(Object... infos) {
final PsiReferenceExpression referenceExpression =
(PsiReferenceExpression)infos[0];
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)infos[0];
final String referenceName = referenceExpression.getReferenceName();
return new StringConcatenationInFormatCallFix(referenceName);
}
private static class StringConcatenationInFormatCallFix
extends InspectionGadgetsFix {
private static class StringConcatenationInFormatCallFix extends InspectionGadgetsFix {
private final String variableName;
@@ -67,19 +63,16 @@ public class StringConcatenationInMessageFormatCallInspection
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"string.concatenation.in.format.call.quickfix", variableName);
return InspectionGadgetsBundle.message("string.concatenation.in.format.call.quickfix", variableName);
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiBinaryExpression)) {
return;
}
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)element;
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)element;
final PsiElement parent = binaryExpression.getParent();
if (!(parent instanceof PsiExpressionList)) {
return;
@@ -96,8 +89,7 @@ public class StringConcatenationInMessageFormatCallInspection
final Object constant =
ExpressionUtils.computeConstantExpression(lhs);
if (constant instanceof String) {
final PsiExpression newExpression =
addParameter(lhs, parameter);
final PsiExpression newExpression = addParameter(lhs, parameter);
if (newExpression == null) {
expressionList.addAfter(lhs, binaryExpression);
}
@@ -111,17 +103,15 @@ public class StringConcatenationInMessageFormatCallInspection
binaryExpression.delete();
}
private static PsiExpression addParameter(PsiExpression expression,
int parameterNumber) {
@Nullable
private static PsiExpression addParameter(PsiExpression expression, int parameterNumber) {
if (expression instanceof PsiBinaryExpression) {
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)expression;
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)expression;
final PsiExpression rhs = binaryExpression.getROperand();
if (rhs == null) {
return null;
}
final PsiExpression newExpression =
addParameter(rhs, parameterNumber);
final PsiExpression newExpression = addParameter(rhs, parameterNumber);
if (newExpression == null) {
return null;
}
@@ -129,17 +119,14 @@ public class StringConcatenationInMessageFormatCallInspection
return expression;
}
else if (expression instanceof PsiLiteralExpression) {
final PsiLiteralExpression literalExpression =
(PsiLiteralExpression)expression;
final PsiLiteralExpression literalExpression = (PsiLiteralExpression)expression;
final Object value = literalExpression.getValue();
if (!(value instanceof String)) {
return null;
}
final Project project = expression.getProject();
final PsiElementFactory factory =
JavaPsiFacade.getElementFactory(project);
return factory.createExpressionFromText("\"" + value + '{' +
parameterNumber + "}\"", null);
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
return factory.createExpressionFromText("\"" + value + '{' + parameterNumber + "}\"", null);
}
else {
return null;
@@ -152,19 +139,14 @@ public class StringConcatenationInMessageFormatCallInspection
return new StringConcatenationInMessageFormatCallVisitor();
}
private static class StringConcatenationInMessageFormatCallVisitor
extends BaseInspectionVisitor {
private static class StringConcatenationInMessageFormatCallVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
PsiMethodCallExpression expression) {
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
if (!isMessageFormatCall(methodExpression)) {
if (!isMessageFormatCall(expression)) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
@@ -176,8 +158,7 @@ public class StringConcatenationInMessageFormatCallInspection
return;
}
final int formatArgumentIndex;
if ("java.util.Locale".equals(type.getCanonicalText())
&& arguments.length > 1) {
if ("java.util.Locale".equals(type.getCanonicalText()) && arguments.length > 1) {
formatArgumentIndex = 1;
}
else {
@@ -185,8 +166,7 @@ public class StringConcatenationInMessageFormatCallInspection
}
final PsiExpression formatArgument = arguments[formatArgumentIndex];
final PsiType formatArgumentType = formatArgument.getType();
if (formatArgumentType == null ||
!formatArgumentType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
if (formatArgumentType == null || !formatArgumentType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
return;
}
if (!(formatArgument instanceof PsiBinaryExpression)) {
@@ -195,9 +175,7 @@ public class StringConcatenationInMessageFormatCallInspection
if (PsiUtil.isConstantExpression(formatArgument)) {
return;
}
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)formatArgument;
final PsiBinaryExpression binaryExpression = (PsiBinaryExpression)formatArgument;
final PsiExpression lhs = binaryExpression.getLOperand();
final PsiType lhsType = lhs.getType();
if (lhsType == null || !lhsType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
@@ -210,27 +188,23 @@ public class StringConcatenationInMessageFormatCallInspection
registerError(formatArgument, rhs);
}
private static boolean isMessageFormatCall(
PsiReferenceExpression methodExpression) {
@NonNls final String referenceName =
methodExpression.getReferenceName();
private static boolean isMessageFormatCall(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
@NonNls final String referenceName = methodExpression.getReferenceName();
if (!"format".equals(referenceName)) {
return false;
}
final PsiExpression qualifierExpression =
methodExpression.getQualifierExpression();
final PsiExpression qualifierExpression = methodExpression.getQualifierExpression();
if (!(qualifierExpression instanceof PsiReferenceExpression)) {
return false;
}
final PsiReferenceExpression referenceExpression =
(PsiReferenceExpression)qualifierExpression;
final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)qualifierExpression;
final PsiElement target = referenceExpression.resolve();
if (!(target instanceof PsiClass)) {
return false;
}
final PsiClass aClass = (PsiClass)target;
return InheritanceUtil.isInheritor(aClass,
"java.text.MessageFormat");
return InheritanceUtil.isInheritor(aClass, "java.text.MessageFormat");
}
}
}
@@ -65,7 +65,7 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
super.visitPolyadicExpression(expression);
final IElementType tokenType = expression.getOperationTokenType();
if (!JavaTokenType.PLUS.equals(tokenType) || !hasStringType(expression)) {
if (!JavaTokenType.PLUS.equals(tokenType) || !ExpressionUtils.hasStringType(expression)) {
return;
}
final PsiExpression[] operands = expression.getOperands();
@@ -83,7 +83,7 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
}
private boolean isMissingWhitespace(PsiExpression lhs, PsiExpression rhs) {
final boolean lhsIsString = hasStringType(lhs);
final boolean lhsIsString = ExpressionUtils.hasStringType(lhs);
final PsiLiteralExpression lhsLiteral = ExpressionUtils.getLiteral(lhs);
final PsiLiteralExpression rhsLiteral = ExpressionUtils.getLiteral(rhs);
if (lhsLiteral != null && lhsIsString) {
@@ -103,7 +103,7 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
else if (ignoreNonStringLiterals || rhsLiteral == null || lhsIsString) {
return false;
}
final boolean rhsIsString = hasStringType(rhs);
final boolean rhsIsString = ExpressionUtils.hasStringType(rhs);
if (rhsLiteral != null && rhsIsString) {
final String value = (String)rhsLiteral.getValue();
if ((value == null) || value.isEmpty()) {
@@ -119,10 +119,5 @@ public class StringConcatenationMissingWhitespaceInspection extends BaseInspecti
}
return true;
}
private boolean hasStringType(PsiExpression expression) {
final PsiType type = expression.getType();
return (type != null) && type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -22,7 +22,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.fixes.EqualityToEqualsFix;
import com.siyeh.ig.psiutils.ComparisonUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;
public class StringEqualityInspection extends BaseInspection {
@@ -58,11 +58,11 @@ public class StringEqualityInspection extends BaseInspection {
return;
}
final PsiExpression lhs = expression.getLOperand();
if (!isStringType(lhs)) {
if (!ExpressionUtils.hasStringType(lhs)) {
return;
}
final PsiExpression rhs = expression.getROperand();
if (rhs == null || !isStringType(rhs)) {
if (rhs == null || !ExpressionUtils.hasStringType(rhs)) {
return;
}
final String lhsText = lhs.getText();
@@ -76,16 +76,5 @@ public class StringEqualityInspection extends BaseInspection {
final PsiJavaToken sign = expression.getOperationSign();
registerError(sign);
}
private static boolean isStringType(PsiExpression expression) {
if (expression == null) {
return false;
}
final PsiType lhsType = expression.getType();
if (lhsType == null) {
return false;
}
return TypeUtils.isJavaLangString(lhsType);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -23,8 +23,9 @@ import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ComparisonUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CharacterComparisonInspection extends BaseInspection {
@@ -70,9 +71,9 @@ public class CharacterComparisonInspection extends BaseInspection {
registerError(expression);
}
private static boolean isCharacter(PsiExpression expression) {
return TypeUtils.expressionHasType(expression, PsiKeyword.CHAR) ||
TypeUtils.expressionHasType(expression, CommonClassNames.JAVA_LANG_CHARACTER);
private static boolean isCharacter(@Nullable PsiExpression expression) {
return ExpressionUtils.hasType(expression, PsiKeyword.CHAR) ||
ExpressionUtils.hasType(expression, CommonClassNames.JAVA_LANG_CHARACTER);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -21,24 +21,21 @@ import com.intellij.psi.PsiNewExpression;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.TypeUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;
public class SimpleDateFormatWithoutLocaleInspection
extends BaseInspection {
public class SimpleDateFormatWithoutLocaleInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"instantiating.simpledateformat.without.locale.display.name");
return InspectionGadgetsBundle.message("instantiating.simpledateformat.without.locale.display.name");
}
@Override
@NotNull
public String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"instantiating.simpledateformat.without.locale.problem.descriptor");
return InspectionGadgetsBundle.message("instantiating.simpledateformat.without.locale.problem.descriptor");
}
@Override
@@ -46,23 +43,21 @@ public class SimpleDateFormatWithoutLocaleInspection
return new SimpleDateFormatWithoutLocaleVisitor();
}
private static class SimpleDateFormatWithoutLocaleVisitor
extends BaseInspectionVisitor {
private static class SimpleDateFormatWithoutLocaleVisitor extends BaseInspectionVisitor {
@Override
public void visitNewExpression(@NotNull PsiNewExpression expression) {
super.visitNewExpression(expression);
if (!TypeUtils.expressionHasType(expression,
"java.text.SimpleDateFormat")) {
if (!ExpressionUtils.hasType(expression, "java.text.SimpleDateFormat")) {
return;
}
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
return;
}
final PsiExpression[] args = argumentList.getExpressions();
for (PsiExpression arg : args) {
if (TypeUtils.expressionHasType(arg, "java.util.Locale")) {
final PsiExpression[] arguments = argumentList.getExpressions();
for (PsiExpression argument : arguments) {
if (ExpressionUtils.hasType(argument, "java.util.Locale")) {
return;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -23,8 +23,8 @@ import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.MethodCallUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
public class BigDecimalEqualsInspection extends BaseInspection {
@@ -32,15 +32,13 @@ public class BigDecimalEqualsInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"big.decimal.equals.display.name");
return InspectionGadgetsBundle.message("big.decimal.equals.display.name");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"big.decimal.equals.problem.descriptor");
return InspectionGadgetsBundle.message("big.decimal.equals.problem.descriptor");
}
@Override
@@ -51,20 +49,15 @@ public class BigDecimalEqualsInspection extends BaseInspection {
private static class BigDecimalEqualsFix extends InspectionGadgetsFix {
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"big.decimal.equals.replace.quickfix");
return InspectionGadgetsBundle.message("big.decimal.equals.replace.quickfix");
}
@Override
public void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
final PsiIdentifier name =
(PsiIdentifier)descriptor.getPsiElement();
final PsiReferenceExpression expression =
(PsiReferenceExpression)name.getParent();
public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiIdentifier name = (PsiIdentifier)descriptor.getPsiElement();
final PsiReferenceExpression expression = (PsiReferenceExpression)name.getParent();
assert expression != null;
final PsiMethodCallExpression call =
(PsiMethodCallExpression)expression.getParent();
final PsiMethodCallExpression call = (PsiMethodCallExpression)expression.getParent();
final PsiExpression qualifier = expression.getQualifierExpression();
if (qualifier == null) {
return;
@@ -74,8 +67,7 @@ public class BigDecimalEqualsInspection extends BaseInspection {
final PsiExpressionList argumentList = call.getArgumentList();
final PsiExpression[] args = argumentList.getExpressions();
final String argText = args[0].getText();
replaceExpression(call,
qualifierText + ".compareTo(" + argText + ")==0");
replaceExpression(call, qualifierText + ".compareTo(" + argText + ")==0");
}
}
@@ -87,27 +79,23 @@ public class BigDecimalEqualsInspection extends BaseInspection {
private static class BigDecimalEqualsVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
@NotNull PsiMethodCallExpression expression) {
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
if (!MethodCallUtils.isEqualsCall(expression)) {
return;
}
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiExpressionList argumentList = expression.getArgumentList();
final PsiExpression[] arguments = argumentList.getExpressions();
if (arguments.length == 0) {
return;
}
final PsiExpression arg = arguments[0];
if (!TypeUtils.expressionHasType(arg, "java.math.BigDecimal")) {
if (!ExpressionUtils.hasType(arg, "java.math.BigDecimal")) {
return;
}
final PsiExpression qualifier =
methodExpression.getQualifierExpression();
if (!TypeUtils.expressionHasType(qualifier, "java.math.BigDecimal"
)) {
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (!ExpressionUtils.hasType(qualifier, "java.math.BigDecimal")) {
return;
}
final PsiElement context = expression.getParent();
@@ -28,6 +28,7 @@ import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ComparisonUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
@@ -39,30 +40,25 @@ public class CharUsedInArithmeticContextInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"char.used.in.arithmetic.context.display.name");
return InspectionGadgetsBundle.message("char.used.in.arithmetic.context.display.name");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"char.used.in.arithmetic.context.problem.descriptor");
return InspectionGadgetsBundle.message("char.used.in.arithmetic.context.problem.descriptor");
}
@NotNull
@Override
protected InspectionGadgetsFix[] buildFixes(Object... infos) {
final List<InspectionGadgetsFix> result =
new ArrayList<InspectionGadgetsFix>();
final List<InspectionGadgetsFix> result = new ArrayList<InspectionGadgetsFix>();
final PsiElement expression = (PsiElement)infos[0];
PsiElement parent = expression.getParent();
if (parent instanceof PsiExpression) {
final PsiExpression binaryExpression =
(PsiExpression)parent;
final PsiExpression binaryExpression = (PsiExpression)parent;
final PsiType type = binaryExpression.getType();
if (type instanceof PsiPrimitiveType &&
!type.equals(PsiType.CHAR)) {
if (type instanceof PsiPrimitiveType && !type.equals(PsiType.CHAR)) {
final String typeText = type.getCanonicalText();
result.add(new CharUsedInArithmeticContentCastFix(typeText));
}
@@ -71,8 +67,7 @@ public class CharUsedInArithmeticContextInspection extends BaseInspection {
return result.toArray(new InspectionGadgetsFix[result.size()]);
}
while (parent instanceof PsiPolyadicExpression) {
if (TypeUtils.expressionHasType((PsiExpression)parent,
CommonClassNames.JAVA_LANG_STRING)) {
if (ExpressionUtils.hasStringType((PsiExpression)parent)) {
result.add(new CharUsedInArithmeticContentFix());
break;
}
@@ -82,36 +77,30 @@ public class CharUsedInArithmeticContextInspection extends BaseInspection {
return result.toArray(new InspectionGadgetsFix[result.size()]);
}
private static class CharUsedInArithmeticContentFix
extends InspectionGadgetsFix {
private static class CharUsedInArithmeticContentFix extends InspectionGadgetsFix {
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"char.used.in.arithmetic.context.quickfix");
return InspectionGadgetsBundle.message("char.used.in.arithmetic.context.quickfix");
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiLiteralExpression)) {
return;
}
final PsiLiteralExpression literalExpression =
(PsiLiteralExpression)element;
final PsiLiteralExpression literalExpression = (PsiLiteralExpression)element;
final Object literal = literalExpression.getValue();
if (!(literal instanceof Character)) {
return;
}
final String escaped = StringUtil.escapeStringCharacters(
literal.toString());
final String escaped = StringUtil.escapeStringCharacters(literal.toString());
replaceExpression(literalExpression, '\"' + escaped + '"');
}
}
private static class CharUsedInArithmeticContentCastFix
extends InspectionGadgetsFix {
private static class CharUsedInArithmeticContentCastFix extends InspectionGadgetsFix {
private final String typeText;
@@ -121,21 +110,18 @@ public class CharUsedInArithmeticContextInspection extends BaseInspection {
@NotNull
public String getName() {
return InspectionGadgetsBundle.message(
"char.used.in.arithmetic.context.cast.quickfix", typeText);
return InspectionGadgetsBundle.message("char.used.in.arithmetic.context.cast.quickfix", typeText);
}
@Override
protected void doFix(Project project, ProblemDescriptor descriptor)
throws IncorrectOperationException {
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PsiExpression)) {
return;
}
final PsiExpression expression = (PsiExpression)element;
final String expressionText = expression.getText();
replaceExpression(expression,
'(' + typeText + ')' + expressionText);
replaceExpression(expression, '(' + typeText + ')' + expressionText);
}
}
@@ -144,8 +130,8 @@ public class CharUsedInArithmeticContextInspection extends BaseInspection {
return new CharUsedInArithmeticContextVisitor();
}
private static class CharUsedInArithmeticContextVisitor
extends BaseInspectionVisitor {
private static class CharUsedInArithmeticContextVisitor extends BaseInspectionVisitor {
@Override
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
super.visitPolyadicExpression(expression);
@@ -158,11 +144,8 @@ public class CharUsedInArithmeticContextInspection extends BaseInspection {
for (int i = 1; i < operands.length; i++) {
final PsiExpression operand = operands[i];
final PsiType rightType = operand.getType();
final PsiType expressionType =
TypeConversionUtil.calcTypeForBinaryExpression(
leftType, rightType, tokenType, true);
if (expressionType == null ||
expressionType.equalsToText(CommonClassNames.JAVA_LANG_STRING)) {
final PsiType expressionType = TypeConversionUtil.calcTypeForBinaryExpression(leftType, rightType, tokenType, true);
if (TypeUtils.isJavaLangString(expressionType)) {
return;
}
if (PsiType.CHAR.equals(rightType)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2012 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.
@@ -26,7 +26,6 @@ import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -182,8 +181,7 @@ public class PointlessArithmeticExpressionInspection
if (!arithmeticTokens.contains(expression.getOperationTokenType())) {
return;
}
if (TypeUtils.expressionHasType(expression,
CommonClassNames.JAVA_LANG_STRING)) {
if (ExpressionUtils.hasStringType(expression)) {
return;
}
final PsiExpression lhs = expression.getLOperand();
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2010 Bas Leijdekkers
* Copyright 2007-2012 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,8 +22,8 @@ import com.intellij.psi.PsiReferenceExpression;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.MethodUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
public class EqualsHashCodeCalledOnUrlInspection extends BaseInspection {
@@ -31,15 +31,13 @@ public class EqualsHashCodeCalledOnUrlInspection extends BaseInspection {
@Override
@NotNull
public String getDisplayName() {
return InspectionGadgetsBundle.message(
"equals.hashcode.called.on.url.display.name");
return InspectionGadgetsBundle.message("equals.hashcode.called.on.url.display.name");
}
@Override
@NotNull
protected String buildErrorString(Object... infos) {
return InspectionGadgetsBundle.message(
"equals.hashcode.called.on.url.problem.descriptor");
return InspectionGadgetsBundle.message("equals.hashcode.called.on.url.problem.descriptor");
}
@Override
@@ -47,22 +45,17 @@ public class EqualsHashCodeCalledOnUrlInspection extends BaseInspection {
return new EqualsHashCodeCalledOnUrlVisitor();
}
private static class EqualsHashCodeCalledOnUrlVisitor
extends BaseInspectionVisitor {
private static class EqualsHashCodeCalledOnUrlVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
public void visitMethodCallExpression(PsiMethodCallExpression expression) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
final PsiMethod method = expression.resolveMethod();
if (!MethodUtils.isEquals(method) &&
!MethodUtils.isHashCode(method)) {
if (!MethodUtils.isEquals(method) && !MethodUtils.isHashCode(method)) {
return;
}
final PsiExpression qualifier =
methodExpression.getQualifierExpression();
if (!TypeUtils.expressionHasType(qualifier, "java.net.URL")) {
final PsiExpression qualifier = methodExpression.getQualifierExpression();
if (!ExpressionUtils.hasType(qualifier, "java.net.URL")) {
return;
}
registerMethodCallError(expression);
@@ -79,7 +79,7 @@ public class TrivialStringConcatenationInspection extends BaseInspection {
final StringBuilder text = new StringBuilder();
for (PsiExpression operand : operands) {
if (operandToReplace != null && !replaced) {
if (TypeUtils.expressionHasType(operand, CommonClassNames.JAVA_LANG_STRING)) {
if (ExpressionUtils.hasStringType(operand)) {
seenString = true;
}
if (text.length() > 0) {
@@ -123,7 +123,7 @@ public class TrivialStringConcatenationInspection extends BaseInspection {
return "String.valueOf((Object)null)";
}
}
if (seenString || TypeUtils.expressionHasType(operandToReplace, CommonClassNames.JAVA_LANG_STRING)) {
if (seenString || ExpressionUtils.hasStringType(operandToReplace)) {
return operandToReplace.getText();
}
return "String.valueOf(" + operandToReplace.getText() + ')';
@@ -169,7 +169,7 @@ public class TrivialStringConcatenationInspection extends BaseInspection {
@Override
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
super.visitPolyadicExpression(expression);
if (!TypeUtils.expressionHasType(expression, CommonClassNames.JAVA_LANG_STRING)) {
if (!ExpressionUtils.hasStringType(expression)) {
return;
}
final PsiExpression[] operands = expression.getOperands();
@@ -30,14 +30,6 @@ public class TypeUtils {
private TypeUtils() {
}
public static boolean expressionHasType(@Nullable PsiExpression expression, @NonNls @NotNull String typeName) {
if (expression == null) {
return false;
}
final PsiType type = expression.getType();
return typeEquals(typeName, type);
}
public static boolean typeEquals(@NonNls @NotNull String typeName, @Nullable PsiType targetType) {
return targetType != null && targetType.equalsToText(typeName);
}