diff options
author | Tor Norbye <tnorbye@google.com> | 2013-04-24 10:43:41 -0700 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2013-04-24 10:43:41 -0700 |
commit | b17587c84879dd2ea42495f1fbdadbc806b9475b (patch) | |
tree | b4d9014f69cb8289627ddc75339a6b0b3fe1bc5e /plugins/InspectionGadgets | |
parent | b569bc6aa78f6eacf72e8b90622d300e1a9db25f (diff) | |
download | idea-b17587c84879dd2ea42495f1fbdadbc806b9475b.tar.gz |
Snapshot e242282deb41c328afbe971fc167e47ddfb26df9 from master branch of git://git.jetbrains.org/idea/community.git
Change-Id: Ifdc1818cde7b63f6d7bf42801f18c7f1557b8d85
Diffstat (limited to 'plugins/InspectionGadgets')
15 files changed, 477 insertions, 80 deletions
diff --git a/plugins/InspectionGadgets/src/META-INF/plugin.xml b/plugins/InspectionGadgets/src/META-INF/plugin.xml index 1b3efecf24ca..2e8c2d98e469 100644 --- a/plugins/InspectionGadgets/src/META-INF/plugin.xml +++ b/plugins/InspectionGadgets/src/META-INF/plugin.xml @@ -2297,6 +2297,10 @@ key="unnecessary.call.to.string.valueof.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.style.UnnecessaryCallToStringValueOfInspection"/> + <localInspection language="JAVA" shortName="UnnecessaryToStringCall" bundle="com.siyeh.InspectionGadgetsBundle" + key="unnecessary.tostring.call.display.name" groupBundle="messages.InspectionsBundle" + groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING" + implementationClass="com.siyeh.ig.style.UnnecessaryToStringCallInspection"/> <localInspection language="JAVA" suppressId="RedundantNoArgConstructor" shortName="UnnecessaryConstructor" bundle="com.siyeh.InspectionGadgetsBundle" key="unnecessary.constructor.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING" diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index df0905eac73c..8120f291f364 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1682,7 +1682,9 @@ synchronization.on.method.parameter.problem.descriptor=Synchronization on method too.broad.catch.quickfix=Add ''catch'' clause for ''{0}'' too.broad.catch.option=&Only warn on RuntimeException, Exception, Error or Throwable unnecessary.call.to.string.valueof.display.name=Unnecessary call to 'String.valueOf()' +unnecessary.tostring.call.display.name=Unnecessary call to '.toString()' unnecessary.call.to.string.valueof.problem.descriptor=<code>#ref</code> can be simplified to ''{0}'' #loc +unnecessary.tostring.call.problem.descriptor=Unnecessary <code>#ref()</code> call #loc unnecessary.call.to.string.valueof.quickfix=Replace with ''{0}'' throwable.result.of.method.call.ignored.display.name=Throwable result of method call ignored throwable.result.of.method.call.ignored.problem.descriptor=Result of <code>#ref()</code> not thrown #loc diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/jdk/VarargParameterInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/jdk/VarargParameterInspection.java index f011d6dd84c2..f93166cfd2b3 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/jdk/VarargParameterInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/jdk/VarargParameterInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 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. @@ -15,13 +15,13 @@ */ package com.siyeh.ig.jdk; +import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.codeStyle.CodeStyleManager; import com.intellij.psi.codeStyle.JavaCodeStyleManager; import com.intellij.psi.search.searches.ReferencesSearch; -import com.intellij.util.IncorrectOperationException; import com.intellij.util.Query; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -44,15 +44,13 @@ public class VarargParameterInspection extends BaseInspection { @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "variable.argument.method.display.name"); + return InspectionGadgetsBundle.message("variable.argument.method.display.name"); } @Override @NotNull public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "variable.argument.method.problem.descriptor"); + return InspectionGadgetsBundle.message("variable.argument.method.problem.descriptor"); } @Override @@ -65,27 +63,29 @@ public class VarargParameterInspection extends BaseInspection { @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "variable.argument.method.quickfix"); + return InspectionGadgetsBundle.message("variable.argument.method.quickfix"); } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { + protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PsiMethod method = (PsiMethod)element.getParent(); final PsiParameterList parameterList = method.getParameterList(); final PsiParameter[] parameters = parameterList.getParameters(); - final PsiParameter lastParameter = - parameters[parameters.length - 1]; + final PsiParameter lastParameter = parameters[parameters.length - 1]; if (!lastParameter.isVarArgs()) { return; } - final PsiEllipsisType type = - (PsiEllipsisType)lastParameter.getType(); + final PsiEllipsisType type = (PsiEllipsisType)lastParameter.getType(); final Query<PsiReference> query = ReferencesSearch.search(method); final PsiType componentType = type.getComponentType(); - final String typeText = componentType.getCanonicalText(); + final String typeText; + if (componentType instanceof PsiClassType) { + final PsiClassType classType = (PsiClassType)componentType; + typeText = classType.rawType().getCanonicalText(); + } else { + typeText = componentType.getCanonicalText(); + } final Collection<PsiReference> references = query.findAll(); for (PsiReference reference : references) { modifyCalls(reference, typeText, parameters.length - 1); @@ -93,59 +93,48 @@ public class VarargParameterInspection extends BaseInspection { final PsiType arrayType = type.toArrayType(); final PsiManager psiManager = lastParameter.getManager(); final PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory(); - final PsiTypeElement newTypeElement = - factory.createTypeElement(arrayType); - final PsiTypeElement typeElement = - lastParameter.getTypeElement(); + final PsiTypeElement newTypeElement = factory.createTypeElement(arrayType); + final PsiTypeElement typeElement = lastParameter.getTypeElement(); + if (typeElement == null) { + return; + } + final PsiAnnotation annotation = AnnotationUtil.findAnnotation(method, "java.lang.SafeVarargs"); + if (annotation != null) { + annotation.delete(); + } typeElement.replace(newTypeElement); } - public static void modifyCalls(PsiReference reference, - String arrayTypeText, - int indexOfFirstVarargArgument) - throws IncorrectOperationException { - final PsiReferenceExpression referenceExpression = - (PsiReferenceExpression)reference.getElement(); - final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)referenceExpression.getParent(); - final PsiExpressionList argumentList = - methodCallExpression.getArgumentList(); + public static void modifyCalls(PsiReference reference, String arrayTypeText, int indexOfFirstVarargArgument) { + final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)reference.getElement(); + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)referenceExpression.getParent(); + final PsiExpressionList argumentList = methodCallExpression.getArgumentList(); final PsiExpression[] arguments = argumentList.getExpressions(); @NonNls final StringBuilder builder = new StringBuilder("new "); builder.append(arrayTypeText); builder.append("[]{"); if (arguments.length > indexOfFirstVarargArgument) { - final PsiExpression firstArgument = - arguments[indexOfFirstVarargArgument]; + final PsiExpression firstArgument = arguments[indexOfFirstVarargArgument]; final String firstArgumentText = firstArgument.getText(); builder.append(firstArgumentText); - for (int i = indexOfFirstVarargArgument + 1; - i < arguments.length; i++) { - builder.append(','); - builder.append(arguments[i].getText()); + for (int i = indexOfFirstVarargArgument + 1; i < arguments.length; i++) { + builder.append(',').append(arguments[i].getText()); } } builder.append('}'); final Project project = referenceExpression.getProject(); - final PsiElementFactory factory = - JavaPsiFacade.getElementFactory(project); - final PsiExpression arrayExpression = - factory.createExpressionFromText(builder.toString(), - referenceExpression); + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + final PsiExpression arrayExpression = factory.createExpressionFromText(builder.toString(), referenceExpression); if (arguments.length > indexOfFirstVarargArgument) { - final PsiExpression firstArgument = - arguments[indexOfFirstVarargArgument]; - argumentList.deleteChildRange(firstArgument, - arguments[arguments.length - 1]); + final PsiExpression firstArgument = arguments[indexOfFirstVarargArgument]; + argumentList.deleteChildRange(firstArgument, arguments[arguments.length - 1]); argumentList.add(arrayExpression); } else { argumentList.add(arrayExpression); } - final CodeStyleManager codeStyleManager = - CodeStyleManager.getInstance(project); - final JavaCodeStyleManager javaCodeStyleManager = - JavaCodeStyleManager.getInstance(project); + final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); + final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); javaCodeStyleManager.shortenClassReferences(argumentList); codeStyleManager.reformat(argumentList); } @@ -165,8 +154,7 @@ public class VarargParameterInspection extends BaseInspection { return; } final PsiParameter[] parameters = parameterList.getParameters(); - final PsiParameter lastParameter = - parameters[parameters.length - 1]; + final PsiParameter lastParameter = parameters[parameters.length - 1]; if (lastParameter.isVarArgs()) { registerMethodError(method); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java index 4c75ce15866d..45fdfb5146b9 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryCallToStringValueOfInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2012 Bas Leijdekkers + * Copyright 2008-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. @@ -104,32 +104,7 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection { if (!"valueOf".equals(referenceName)) { return; } - final PsiElement parent = expression.getParent(); - if (!(parent instanceof PsiPolyadicExpression)) { - return; - } - final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent; - final PsiType type = polyadicExpression.getType(); - if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) { - return; - } - final PsiExpression[] operands = polyadicExpression.getOperands(); - int index = -1; - for (int i = 0, length = operands.length; i < length; i++) { - final PsiExpression operand = operands[i]; - if (expression.equals(operand)) { - index = i; - } - } - if (index > 0) { - if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) { - return; - } - } else if (operands.length > 1) { - if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) { - return; - } - } else { + if (isCallToStringValueOfNecessary(expression)) { return; } final PsiExpressionList argumentList = expression.getArgumentList(); @@ -160,5 +135,86 @@ public class UnnecessaryCallToStringValueOfInspection extends BaseInspection { } registerError(expression, calculateReplacementText(argument)); } + + private boolean isCallToStringValueOfNecessary(PsiMethodCallExpression expression) { + final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression); + if (parent instanceof PsiPolyadicExpression) { + final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent; + final PsiType type = polyadicExpression.getType(); + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) { + return true; + } + final PsiExpression[] operands = polyadicExpression.getOperands(); + int index = -1; + for (int i = 0, length = operands.length; i < length; i++) { + final PsiExpression operand = operands[i]; + if (expression.equals(operand)) { + index = i; + } + } + if (index > 0) { + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) { + return true; + } + } else if (operands.length > 1) { + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) { + return true; + } + } else { + return true; + } + } else if (parent instanceof PsiExpressionList) { + final PsiExpressionList expressionList = (PsiExpressionList)parent; + final PsiElement grandParent = expressionList.getParent(); + if (!(grandParent instanceof PsiMethodCallExpression)) { + return true; + } + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent; + final PsiReferenceExpression methodExpression1 = methodCallExpression.getMethodExpression(); + final String name = methodExpression1.getReferenceName(); + final PsiExpression[] expressions = expressionList.getExpressions(); + if ("insert".equals(name)) { + if (expressions.length < 2 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[1]))) { + return true; + } + if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) { + return true; + } + + } else if ("append".equals(name)) { + if (expressions.length < 1 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[0]))) { + return true; + } + if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) { + return true; + } + } else if ("print".equals(name) || "println".equals(name)) { + if (!isCallToMethodIn(methodCallExpression, "java.io.PrintStream", "java.io.PrintWriter")) { + return true; + } + } + } else { + return true; + } + return false; + } + + private boolean isCallToMethodIn(PsiMethodCallExpression methodCallExpression, String... classNames) { + final PsiMethod method = methodCallExpression.resolveMethod(); + if (method == null) { + return false; + } + final PsiClass containingClass = method.getContainingClass(); + if (containingClass == null) { + return false; + } + final String qualifiedName = containingClass.getQualifiedName(); + for (String className : classNames) { + if (className.equals(qualifiedName)) { + return true; + } + } + return false; + } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryToStringCallInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryToStringCallInspection.java new file mode 100644 index 000000000000..d7cad4ef4f13 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryToStringCallInspection.java @@ -0,0 +1,200 @@ +/* + * Copyright 2008-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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.style; + +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.util.IncorrectOperationException; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.psiutils.ParenthesesUtils; +import com.siyeh.ig.psiutils.TypeUtils; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class UnnecessaryToStringCallInspection extends BaseInspection { + + @Override + @Nls + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message("unnecessary.tostring.call.display.name"); + } + + @Override + @NotNull + protected String buildErrorString(Object... infos) { + final String text = (String)infos[0]; + return InspectionGadgetsBundle.message("unnecessary.tostring.call.problem.descriptor", text); + } + + @Override + @Nullable + protected InspectionGadgetsFix buildFix(Object... infos) { + final String text = (String)infos[0]; + return new UnnecessaryCallToStringValueOfFix(text); + } + + public static String calculateReplacementText(PsiExpression expression) { + if (expression == null) { + return "this"; + } + return expression.getText(); + } + + private static class UnnecessaryCallToStringValueOfFix extends InspectionGadgetsFix { + + private final String replacementText; + + UnnecessaryCallToStringValueOfFix(String replacementText) { + this.replacementText = replacementText; + } + + @NotNull + public String getName() { + return InspectionGadgetsBundle.message("unnecessary.call.to.string.valueof.quickfix", replacementText); + } + + @Override + protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)descriptor.getPsiElement().getParent().getParent(); + final PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (qualifier == null) { + replaceExpression(methodCallExpression, "this"); + } else { + methodCallExpression.replace(qualifier); + } + } + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new UnnecessaryCallToStringValueOfVisitor(); + } + + private static class UnnecessaryCallToStringValueOfVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethodCallExpression(PsiMethodCallExpression expression) { + super.visitMethodCallExpression(expression); + final PsiReferenceExpression methodExpression = expression.getMethodExpression(); + final String referenceName = methodExpression.getReferenceName(); + if (!"toString".equals(referenceName)) { + return; + } + if (isToStringCallNecessary(expression)) { + return; + } + final PsiExpressionList argumentList = expression.getArgumentList(); + final PsiExpression[] arguments = argumentList.getExpressions(); + if (arguments.length != 0) { + return; + } + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (qualifier != null && qualifier.getType() instanceof PsiArrayType) { + // do not warn on nonsensical code + return; + } + registerMethodCallError(expression, calculateReplacementText(qualifier)); + } + + private boolean isToStringCallNecessary(PsiMethodCallExpression expression) { + final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(expression); + if (parent instanceof PsiPolyadicExpression) { + final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)parent; + final PsiType type = polyadicExpression.getType(); + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, type)) { + return true; + } + final PsiExpression[] operands = polyadicExpression.getOperands(); + int index = -1; + for (int i = 0, length = operands.length; i < length; i++) { + final PsiExpression operand = operands[i]; + if (expression.equals(operand)) { + index = i; + } + } + if (index > 0) { + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index - 1].getType())) { + return true; + } + } else if (operands.length > 1) { + if (!TypeUtils.typeEquals(CommonClassNames.JAVA_LANG_STRING, operands[index + 1].getType())) { + return true; + } + } else { + return true; + } + } else if (parent instanceof PsiExpressionList) { + final PsiExpressionList expressionList = (PsiExpressionList)parent; + final PsiElement grandParent = expressionList.getParent(); + if (!(grandParent instanceof PsiMethodCallExpression)) { + return true; + } + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)grandParent; + final PsiReferenceExpression methodExpression1 = methodCallExpression.getMethodExpression(); + final String name = methodExpression1.getReferenceName(); + final PsiExpression[] expressions = expressionList.getExpressions(); + if ("insert".equals(name)) { + if (expressions.length < 2 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[1]))) { + return true; + } + if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) { + return true; + } + + } else if ("append".equals(name)) { + if (expressions.length < 1 || !expression.equals(ParenthesesUtils.stripParentheses(expressions[0]))) { + return true; + } + if (!isCallToMethodIn(methodCallExpression, "java.lang.StringBuilder", "java.lang.StringBuffer")) { + return true; + } + } else if ("print".equals(name) || "println".equals(name)) { + if (!isCallToMethodIn(methodCallExpression, "java.io.PrintStream", "java.io.PrintWriter")) { + return true; + } + } + } else { + return true; + } + return false; + } + + private boolean isCallToMethodIn(PsiMethodCallExpression methodCallExpression, String... classNames) { + final PsiMethod method = methodCallExpression.resolveMethod(); + if (method == null) { + return false; + } + final PsiClass containingClass = method.getContainingClass(); + if (containingClass == null) { + return false; + } + final String qualifiedName = containingClass.getQualifiedName(); + for (String className : classNames) { + if (className.equals(qualifiedName)) { + return true; + } + } + return false; + } + } +} diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html index 3d95b3ac2f14..ecbdf6ff2ff8 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryCallToStringValueOf.html @@ -1,8 +1,8 @@ <html> <body> -Reports on any calls to <b>String.valueOf()</b> -used in string concatenations. The conversion to string is handled automatically by the compiler -without a call to <b>String.valueOf()</b>, making it unnecessary. +Reports on any calls to <b>String.valueOf()</b> used in string concatenations and as arguments to the +<b>print</b> and <b>println</b> methods of <b>java.io.PrintWriter</b> and <b>java.io.PrintStream</b>. The conversion +to string will be handled by the underlying library methods without an explicit call to <b>String.valueOf()</b>. <!-- tooltip end --> <p> <small>Powered by InspectionGadgets</small> diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryToStringCall.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryToStringCall.html new file mode 100644 index 000000000000..69cfb6134430 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnnecessaryToStringCall.html @@ -0,0 +1,13 @@ +<html> +<body> +Reports on any calls to <b>.toString()</b> used in string concatenations and as arguments to the +<b>print</b> and <b>println</b> methods of <b>java.io.PrintWriter</b> and <b>java.io.PrintStream</b>. The conversion +to string will be handled by the underlying library methods without an explicit call to <b>.toString()</b>. +<p> +Note that without the <b>.toString()</b> the expression will have different semantics (the string "null" will be used instead of + throwing a <b>NullPointerException</b>). +<!-- tooltip end --> +<p> +<small>Powered by InspectionGadgets</small> +</body> +</html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/jdk/vararg_parameter/GenericType.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/jdk/vararg_parameter/GenericType.after.java new file mode 100644 index 000000000000..7c6d0d34ed4d --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/jdk/vararg_parameter/GenericType.after.java @@ -0,0 +1,11 @@ +package com.siyeh.igfixes.jdk.vararg_parameter; + +@SuppressWarnings("UnusedDeclaration") +public class GenericType { + final void addClasses(Class<? extends Number>[] classes) { + } + + void test() { + addClasses(new Class[]{Number.class, Byte.class}); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/jdk/vararg_parameter/GenericType.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/jdk/vararg_parameter/GenericType.java new file mode 100644 index 000000000000..595bf0032e2b --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/jdk/vararg_parameter/GenericType.java @@ -0,0 +1,12 @@ +package com.siyeh.igfixes.jdk.vararg_parameter; + +@SuppressWarnings("UnusedDeclaration") +public class GenericType { + @java.lang.SafeVarargs + final void addCl<caret>asses(Class<? extends Number>... classes) { + } + + void test() { + addClasses(Number.class, Byte.class); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_tostring/UnnecessaryToString.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_tostring/UnnecessaryToString.java new file mode 100644 index 000000000000..a77660ab714a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_tostring/UnnecessaryToString.java @@ -0,0 +1,30 @@ +package com.siyeh.igtest.style.unnecessary_tostring; + +public class UnnecessaryToString { + + String foo(Object o) { + return "star" + o.toString(); + } + + String bar() { + char[] cs = {'!'}; + return "wars" + cs.toString(); + } + + void fizzz(Object o) { + boolean c = true; + System.out.println(o.toString() + c); + } + + void polyadic(Object s) { + s = "abc" + s.toString() + "efg"; + } + + void printStream(Object o) { + System.out.print(o.toString()); + } + + void builder(StringBuilder builder, Object o) { + builder.append(o.toString()); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_tostring/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_tostring/expected.xml new file mode 100644 index 000000000000..9aea4d234a81 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_tostring/expected.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="UTF-8"?> +<problems> + + <problem> + <file>UnnecessaryToString.java</file> + <line>6</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to '.toString()'</problem_class> + <description>Unnecessary <code>toString()</code> call #loc</description> + </problem> + + <problem> + <file>UnnecessaryToString.java</file> + <line>20</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to '.toString()'</problem_class> + <description>Unnecessary <code>toString()</code> call #loc</description> + </problem> + + <problem> + <file>UnnecessaryToString.java</file> + <line>24</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to '.toString()'</problem_class> + <description>Unnecessary <code>toString()</code> call #loc</description> + </problem> + + <problem> + <file>UnnecessaryToString.java</file> + <line>28</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to '.toString()'</problem_class> + <description>Unnecessary <code>toString()</code> call #loc</description> + </problem> + +</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java index cf3f87cfae7a..023d86fef245 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/UnnecessaryCallToStringValueOf.java @@ -21,4 +21,11 @@ public class UnnecessaryCallToStringValueOf { s = "abc" + String.valueOf('d') + "efg"; } + void printStream() { + System.out.print(String.valueOf(7)); + } + + void builder(StringBuilder builder) { + builder.append(String.valueOf(0x8)); + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml index 315e44ea81b7..c453124dc5ca 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_valueof/expected.xml @@ -15,4 +15,18 @@ <description><code>String.valueOf('d')</code> can be simplified to ''d'' #loc</description> </problem> + <problem> + <file>UnnecessaryCallToStringValueOf.java</file> + <line>25</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to 'String.valueOf()'</problem_class> + <description><code>String.valueOf(7)</code> can be simplified to '7' #loc</description> + </problem> + + <problem> + <file>UnnecessaryCallToStringValueOf.java</file> + <line>29</line> + <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Unnecessary call to 'String.valueOf()'</problem_class> + <description><code>String.valueOf(0x8)</code> can be simplified to '0x8' #loc</description> + </problem> + </problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/jdk/VarargParameterFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/jdk/VarargParameterFixTest.java new file mode 100644 index 000000000000..f14e395ff243 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/jdk/VarargParameterFixTest.java @@ -0,0 +1,18 @@ +package com.siyeh.ig.fixes.jdk; + +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.IGQuickFixesTestCase; +import com.siyeh.ig.jdk.VarargParameterInspection; + +public class VarargParameterFixTest extends IGQuickFixesTestCase { + + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(new VarargParameterInspection()); + myRelativePath = "jdk/vararg_parameter"; + myDefaultHint = InspectionGadgetsBundle.message("variable.argument.method.quickfix"); + } + + public void testGenericType() { doTest(); } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryToStringCallInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryToStringCallInspectionTest.java new file mode 100644 index 000000000000..797b4273106d --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryToStringCallInspectionTest.java @@ -0,0 +1,10 @@ +package com.siyeh.ig.style; + +import com.siyeh.ig.IGInspectionTestCase; + +public class UnnecessaryToStringCallInspectionTest extends IGInspectionTestCase { + + public void test() throws Exception { + doTest("com/siyeh/igtest/style/unnecessary_tostring", new UnnecessaryToStringCallInspection()); + } +}
\ No newline at end of file |