diff options
Diffstat (limited to 'plugins/InspectionGadgets')
24 files changed, 431 insertions, 242 deletions
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 5581810b3f2c..c8854d4dbb26 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -2363,7 +2363,7 @@ 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" cleanupTool="true" + groupKey="group.names.code.style.issues" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.style.UnnecessaryConstructorInspection"/> <localInspection language="JAVA" shortName="UnnecessaryEnumModifier" bundle="com.siyeh.InspectionGadgetsBundle" key="unnecessary.enum.modifier.display.name" groupBundle="messages.InspectionsBundle" diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 715bf898c50a..1dfdfb9eb50a 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1700,7 +1700,7 @@ this.class=this class assertequals.may.be.assertsame.display.name='assertEquals()' may be 'assertSame()' assertequals.may.be.assertsame.problem.descriptor=<code>#ref()</code> may be 'assertSame()' #loc logger.initialized.with.foreign.class.quickfix=Replace with ''{0}.class'' -logger.initialized.with.foreign.class.problem.descriptor=Logger initializer with foreign class <code>#ref</code> #loc +logger.initialized.with.foreign.class.problem.descriptor=Logger initialized with foreign class <code>#ref</code> #loc logger.initialized.with.foreign.class.display.name=Logger initialized with foreign class logger.factory.method.name=Logger factory method name logger.factory.class.name=Logger factory class name @@ -2018,8 +2018,8 @@ string.concatenation.argument.to.log.call.display.name=Non-constant string conca string.concatenation.argument.to.log.call.problem.descriptor=Non-constant string concatenation as argument to <code>#ref()</code> logging call #loc string.concatenation.argument.to.log.call.quickfix=Replace concatenation with parameterized log message placeholder.count.matches.argument.count.display.name=Number of placeholders does not match number of arguments in logging call -placeholder.count.matches.argument.count.more.problem.descriptor=<code>#ref()</code> call has more arguments ({0}) than placeholders ({1}) #loc -placeholder.count.matches.argument.count.fewer.problem.descriptor=<code>#ref()</code> call has fewer arguments ({0}) than placeholders ({1}) #loc +placeholder.count.matches.argument.count.more.problem.descriptor=More arguments provided ({0}) than placeholders specified ({1}) in ''{2}'' #loc +placeholder.count.matches.argument.count.fewer.problem.descriptor=Fewer arguments provided ({0}) than placeholders specified ({1}) in ''{2}'' #loc assignment.to.superclass.field.display.name=Constructor assigns value to field defined in superclass assignment.to.superclass.field.problem.descriptor=Assignment to field ''{0}'' defined in superclass ''{1}'' #loc junit.rule.display.name=Malformed @Rule/@ClassRule field diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/MakeFieldFinalFix.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/MakeFieldFinalFix.java index 80ebfa0ae7d3..00d9f04b8c02 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/MakeFieldFinalFix.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/fixes/MakeFieldFinalFix.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2010 Bas Leijdekkers + * Copyright 2007-2014 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,6 @@ package com.siyeh.ig.fixes; 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.InspectionGadgetsFix; import com.siyeh.ig.psiutils.FinalUtils; @@ -61,8 +60,7 @@ public class MakeFieldFinalFix extends InspectionGadgetsFix { } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { + protected void doFix(Project project, ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PsiField field; if (element instanceof PsiReferenceExpression) { @@ -81,6 +79,7 @@ public class MakeFieldFinalFix extends InspectionGadgetsFix { } field = (PsiField)parent; } + field.normalizeDeclaration(); final PsiModifierList modifierList = field.getModifierList(); if (modifierList == null) { return; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspection.java index a2a3b12bd8de..ef598f5d2216 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspection.java @@ -19,6 +19,7 @@ import com.intellij.psi.*; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.psiutils.TypeUtils; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -143,9 +144,10 @@ public class ImplicitDefaultCharsetUsageInspection extends BaseInspection { } private static boolean hasCharsetType(PsiVariable variable) { - final PsiType lastType = variable.getType(); - return lastType.equalsToText(CommonClassNames.JAVA_LANG_STRING) || - lastType.equalsToText("java.nio.charset.Charset"); + return TypeUtils.variableHasTypeOrSubtype(variable, CommonClassNames.JAVA_LANG_STRING, + "java.nio.charset.Charset", + "java.nio.charset.CharsetEncoder", + "java.nio.charset.CharsetDecoder"); } } } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionBase.java index 750b723031d4..08c54abce2a1 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +20,13 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.*; -import com.intellij.psi.util.PsiTreeUtil; 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.PsiReplacementUtil; +import com.siyeh.ig.psiutils.ClassUtils; import org.jdom.Element; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -157,7 +157,10 @@ public class LoggerInitializedWithForeignClassInspectionBase extends BaseInspect if (expressions.length != 1) { return; } - final PsiClass containingClass = PsiTreeUtil.getParentOfType(expression, PsiClass.class); + PsiClass containingClass = ClassUtils.getContainingClass(expression); + while (containingClass instanceof PsiAnonymousClass) { + containingClass = ClassUtils.getContainingClass(containingClass); + } if (containingClass == null) { return; } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspection.java index a2c9307fc80c..711b0edb5c4b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspection.java @@ -45,13 +45,14 @@ public class PlaceholderCountMatchesArgumentCountInspection extends BaseInspecti protected String buildErrorString(Object... infos) { final Integer argumentCount = (Integer)infos[0]; final Integer placeholderCount = (Integer)infos[1]; + final Object value = infos[2]; if (argumentCount.intValue() > placeholderCount.intValue()) { return InspectionGadgetsBundle.message("placeholder.count.matches.argument.count.more.problem.descriptor", - argumentCount, placeholderCount); + argumentCount, placeholderCount, value); } else { return InspectionGadgetsBundle.message("placeholder.count.matches.argument.count.fewer.problem.descriptor", - argumentCount, placeholderCount); + argumentCount, placeholderCount, value); } } @@ -83,25 +84,24 @@ public class PlaceholderCountMatchesArgumentCountInspection extends BaseInspecti if (arguments.length == 0) { return; } - final PsiExpression firstArgument = arguments[0]; - final int placeholderCount; + PsiExpression logStringArgument = arguments[0]; final int argumentCount; - if (InheritanceUtil.isInheritor(firstArgument.getType(), "org.slf4j.Marker")) { + if (InheritanceUtil.isInheritor(logStringArgument.getType(), "org.slf4j.Marker")) { if (arguments.length < 2) { return; } - final PsiExpression secondArgument = arguments[1]; - placeholderCount = countPlaceholders(secondArgument); + logStringArgument = arguments[1]; argumentCount = countArguments(arguments, 2); } else { - placeholderCount = countPlaceholders(firstArgument); argumentCount = countArguments(arguments, 1); } + final Object value = ExpressionUtils.computeConstantExpression(logStringArgument); + final int placeholderCount = countPlaceholders(value); if (placeholderCount < 0 || argumentCount < 0 || placeholderCount == argumentCount) { return; } - registerMethodCallError(expression, Integer.valueOf(argumentCount), Integer.valueOf(placeholderCount)); + registerError(logStringArgument, Integer.valueOf(argumentCount), Integer.valueOf(placeholderCount), value); } private static boolean hasThrowableType(PsiExpression lastArgument) { @@ -118,8 +118,7 @@ public class PlaceholderCountMatchesArgumentCountInspection extends BaseInspecti return InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_LANG_THROWABLE); } - public static int countPlaceholders(PsiExpression argument) { - final Object value = ExpressionUtils.computeConstantExpression(argument); + public static int countPlaceholders(Object value) { if (!(value instanceof String)) { return -1; } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/MethodMayBeStaticInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/MethodMayBeStaticInspectionBase.java new file mode 100644 index 000000000000..2377e4e14c20 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/MethodMayBeStaticInspectionBase.java @@ -0,0 +1,181 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * 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.performance; + +import com.intellij.codeInspection.InspectionManager; +import com.intellij.openapi.util.Condition; +import com.intellij.openapi.util.WriteExternalException; +import com.intellij.psi.*; +import com.intellij.psi.search.searches.ClassInheritorsSearch; +import com.intellij.util.Processor; +import com.intellij.util.Query; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.psiutils.ClassUtils; +import com.siyeh.ig.psiutils.MethodUtils; +import com.siyeh.ig.psiutils.SerializationUtils; +import org.jdom.Element; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.atomic.AtomicInteger; + +public class MethodMayBeStaticInspectionBase extends BaseInspection { + protected static final String IGNORE_DEFAULT_METHODS_ATTR_NAME = "m_ignoreDefaultMethods"; + protected static final String ONLY_PRIVATE_OR_FINAL_ATTR_NAME = "m_onlyPrivateOrFinal"; + protected static final String IGNORE_EMPTY_METHODS_ATTR_NAME = "m_ignoreEmptyMethods"; + protected static final String REPLACE_QUALIFIER_ATTR_NAME = "m_replaceQualifier"; + /** + * @noinspection PublicField + */ + public boolean m_onlyPrivateOrFinal = false; + /** + * @noinspection PublicField + */ + public boolean m_ignoreEmptyMethods = true; + public boolean m_ignoreDefaultMethods = true; + public boolean m_replaceQualifier = true; + + @Override + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message("method.may.be.static.display.name"); + } + + @Override + @NotNull + protected String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("method.may.be.static.problem.descriptor"); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new MethodCanBeStaticVisitor(); + } + + @Override + public void writeSettings(@NotNull Element node) throws WriteExternalException { + node.addContent(new Element("option").setAttribute("name", ONLY_PRIVATE_OR_FINAL_ATTR_NAME).setAttribute("value", String.valueOf(m_onlyPrivateOrFinal))); + node.addContent(new Element("option").setAttribute("name", IGNORE_EMPTY_METHODS_ATTR_NAME).setAttribute("value", String.valueOf( + m_ignoreEmptyMethods))); + if (!m_ignoreDefaultMethods) { + node.addContent(new Element("option").setAttribute("name", IGNORE_DEFAULT_METHODS_ATTR_NAME).setAttribute("value", "false")); + } + if (!m_replaceQualifier) { + node.addContent(new Element("option").setAttribute("name", REPLACE_QUALIFIER_ATTR_NAME).setAttribute("value", "false")); + } + } + + private class MethodCanBeStaticVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethod(@NotNull PsiMethod method) { + super.visitMethod(method); + if (method.hasModifierProperty(PsiModifier.STATIC) || + method.hasModifierProperty(PsiModifier.ABSTRACT) || + method.hasModifierProperty(PsiModifier.SYNCHRONIZED) || + method.hasModifierProperty(PsiModifier.NATIVE)) { + return; + } + if (method.isConstructor() || method.getNameIdentifier() == null) { + return; + } + if (m_ignoreDefaultMethods && method.hasModifierProperty(PsiModifier.DEFAULT)) { + return; + } + if (m_ignoreEmptyMethods && MethodUtils.isEmpty(method)) { + return; + } + final PsiClass containingClass = ClassUtils.getContainingClass(method); + if (containingClass == null) { + return; + } + final Condition<PsiElement>[] addins = InspectionManager.CANT_BE_STATIC_EXTENSION.getExtensions(); + for (Condition<PsiElement> addin : addins) { + if (addin.value(method)) { + return; + } + } + final PsiElement scope = containingClass.getScope(); + if (!(scope instanceof PsiJavaFile) && !containingClass.hasModifierProperty(PsiModifier.STATIC) && !containingClass.isInterface()) { + return; + } + if (m_onlyPrivateOrFinal && !method.hasModifierProperty(PsiModifier.FINAL) && !method.hasModifierProperty(PsiModifier.PRIVATE)) { + return; + } + if (isExcluded(method) || MethodUtils.hasSuper(method) || MethodUtils.isOverridden(method)) { + return; + } + if (implementsSurprisingInterface(method)) { + return; + } + final MethodReferenceVisitor visitor = new MethodReferenceVisitor(method); + method.accept(visitor); + if (!visitor.areReferencesStaticallyAccessible()) { + return; + } + registerMethodError(method); + } + + private boolean implementsSurprisingInterface(final PsiMethod method) { + final PsiClass containingClass = method.getContainingClass(); + if (containingClass == null) { + return false; + } + final Query<PsiClass> search = ClassInheritorsSearch.search(containingClass, method.getUseScope(), true, true, false); + final boolean[] result = new boolean[1]; + search.forEach(new Processor<PsiClass>() { + AtomicInteger count = new AtomicInteger(0); + + @Override + public boolean process(PsiClass subClass) { + if (count.incrementAndGet() > 5) { + result[0] = true; + return false; + } + final PsiReferenceList list = subClass.getImplementsList(); + if (list == null) { + return true; + } + final PsiJavaCodeReferenceElement[] referenceElements = list.getReferenceElements(); + for (PsiJavaCodeReferenceElement referenceElement : referenceElements) { + final PsiElement target = referenceElement.resolve(); + if (!(target instanceof PsiClass)) { + result[0] = true; + return false; + } + final PsiClass aClass = (PsiClass)target; + if (!aClass.isInterface()) { + result[0] = true; + return false; + } + if (aClass.findMethodBySignature(method, true) != null) { + result[0] = true; + return false; + } + } + return true; + } + }); + return result[0]; + } + + private boolean isExcluded(PsiMethod method) { + return SerializationUtils.isWriteObject(method) || SerializationUtils.isReadObject(method) || + SerializationUtils.isWriteReplace(method) || SerializationUtils.isReadResolve(method); + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentExcludedVisitor.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentExcludedVisitor.java index 69710a084904..f7778e0a89bc 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentExcludedVisitor.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentExcludedVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -44,49 +44,35 @@ class VariablePassedAsArgumentExcludedVisitor extends JavaRecursiveElementVisito } @Override - public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { + public void visitCallExpression(PsiCallExpression callExpression) { if (passed) { return; } - super.visitMethodCallExpression(call); - final PsiExpressionList argumentList = call.getArgumentList(); - final PsiExpression[] arguments = argumentList.getExpressions(); - for (PsiExpression argument : arguments) { - if (!VariableAccessUtils.mayEvaluateToVariable(argument, variable, myBuilderPattern)) { - continue; - } - final PsiMethod method = call.resolveMethod(); - if (method != null) { - final PsiClass aClass = method.getContainingClass(); - if (aClass != null) { - final String name = aClass.getQualifiedName(); - if (excludes.contains(name)) { - continue; - } - } - } - passed = true; - } + super.visitCallExpression(callExpression); + visitCall(callExpression); } @Override - public void visitNewExpression(@NotNull PsiNewExpression newExpression) { + public void visitEnumConstant(PsiEnumConstant enumConstant) { if (passed) { return; } - super.visitNewExpression(newExpression); - final PsiExpressionList argumentList = newExpression.getArgumentList(); + super.visitEnumConstant(enumConstant); + visitCall(enumConstant); + } + + private void visitCall(PsiCall call) { + final PsiExpressionList argumentList = call.getArgumentList(); if (argumentList == null) { return; } - final PsiExpression[] arguments = argumentList.getExpressions(); - for (PsiExpression argument : arguments) { + for (PsiExpression argument : argumentList.getExpressions()) { if (!VariableAccessUtils.mayEvaluateToVariable(argument, variable, myBuilderPattern)) { continue; } - final PsiMethod constructor = newExpression.resolveConstructor(); - if (constructor != null) { - final PsiClass aClass = constructor.getContainingClass(); + final PsiMethod method = call.resolveMethod(); + if (method != null) { + final PsiClass aClass = method.getContainingClass(); if (aClass != null) { final String name = aClass.getQualifiedName(); if (excludes.contains(name)) { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentVisitor.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentVisitor.java index 7938fbc5c696..92082cea9e27 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentVisitor.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/VariablePassedAsArgumentVisitor.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -37,33 +37,29 @@ class VariablePassedAsArgumentVisitor extends JavaRecursiveElementVisitor { } @Override - public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { + public void visitCallExpression(PsiCallExpression callExpression) { + super.visitCallExpression(callExpression); if (passed) { return; } - super.visitMethodCallExpression(call); - final PsiExpressionList argumentList = call.getArgumentList(); - final PsiExpression[] arguments = argumentList.getExpressions(); - for (PsiExpression argument : arguments) { - if (VariableAccessUtils.mayEvaluateToVariable(argument, variable)) { - passed = true; - break; - } - } + visitCall(callExpression); } @Override - public void visitNewExpression(@NotNull PsiNewExpression newExpression) { + public void visitEnumConstant(PsiEnumConstant enumConstant) { if (passed) { return; } - super.visitNewExpression(newExpression); - final PsiExpressionList argumentList = newExpression.getArgumentList(); + super.visitEnumConstant(enumConstant); + visitCall(enumConstant); + } + + private void visitCall(PsiCall call) { + final PsiExpressionList argumentList = call.getArgumentList(); if (argumentList == null) { return; } - final PsiExpression[] arguments = argumentList.getExpressions(); - for (PsiExpression argument : arguments) { + for (PsiExpression argument : argumentList.getExpressions()) { if (VariableAccessUtils.mayEvaluateToVariable(argument, variable)) { passed = true; break; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryConstructorInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryConstructorInspection.java index 126f5021a47f..8b7e23fa1547 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryConstructorInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryConstructorInspection.java @@ -15,7 +15,6 @@ */ package com.siyeh.ig.style; -import com.intellij.codeInspection.CleanupLocalInspectionTool; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; @@ -30,7 +29,7 @@ import org.jetbrains.annotations.NotNull; import javax.swing.*; -public class UnnecessaryConstructorInspection extends BaseInspection implements CleanupLocalInspectionTool { +public class UnnecessaryConstructorInspection extends BaseInspection { @NonNls private static final String SUPER_CALL_TEXT = PsiKeyword.SUPER + "();"; diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/MethodMayBeStaticInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/MethodMayBeStaticInspection.java index e73b6cc234d7..5936bb5433a3 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/MethodMayBeStaticInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/MethodMayBeStaticInspection.java @@ -15,62 +15,22 @@ */ package com.siyeh.ig.performance; -import com.intellij.codeInspection.InspectionManager; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.Condition; -import com.intellij.openapi.util.WriteExternalException; -import com.intellij.psi.*; -import com.intellij.psi.search.searches.ClassInheritorsSearch; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiModifier; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.makeStatic.MakeMethodStaticProcessor; import com.intellij.refactoring.makeStatic.Settings; -import com.intellij.util.Processor; -import com.intellij.util.Query; import com.siyeh.InspectionGadgetsBundle; -import com.siyeh.ig.BaseInspection; -import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.fixes.ChangeModifierFix; -import com.siyeh.ig.psiutils.ClassUtils; -import com.siyeh.ig.psiutils.MethodUtils; -import com.siyeh.ig.psiutils.SerializationUtils; -import org.jdom.Element; import org.jetbrains.annotations.NotNull; import javax.swing.*; -import java.util.concurrent.atomic.AtomicInteger; - -public class MethodMayBeStaticInspection extends BaseInspection { - - private static final String IGNORE_DEFAULT_METHODS_ATTR_NAME = "m_ignoreDefaultMethods"; - private static final String ONLY_PRIVATE_OR_FINAL_ATTR_NAME = "m_onlyPrivateOrFinal"; - private static final String IGNORE_EMPTY_METHODS_ATTR_NAME = "m_ignoreEmptyMethods"; - private static final String REPLACE_QUALIFIER_ATTR_NAME = "m_replaceQualifier"; - /** - * @noinspection PublicField - */ - public boolean m_onlyPrivateOrFinal = false; - /** - * @noinspection PublicField - */ - public boolean m_ignoreEmptyMethods = true; - public boolean m_ignoreDefaultMethods = true; - public boolean m_replaceQualifier = true; - - @Override - @NotNull - public String getDisplayName() { - return InspectionGadgetsBundle.message("method.may.be.static.display.name"); - } - - @Override - @NotNull - protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message("method.may.be.static.problem.descriptor"); - } +public class MethodMayBeStaticInspection extends MethodMayBeStaticInspectionBase { @Override protected InspectionGadgetsFix buildFix(Object... infos) { return new InspectionGadgetsFix() { @@ -103,122 +63,4 @@ public class MethodMayBeStaticInspection extends BaseInspection { optionsPanel.addCheckbox("Replace qualifier by class name", REPLACE_QUALIFIER_ATTR_NAME); return optionsPanel; } - - @Override - public BaseInspectionVisitor buildVisitor() { - return new MethodCanBeStaticVisitor(); - } - - @Override - public void writeSettings(@NotNull Element node) throws WriteExternalException { - node.addContent(new Element("option").setAttribute("name", ONLY_PRIVATE_OR_FINAL_ATTR_NAME).setAttribute("value", String.valueOf(m_onlyPrivateOrFinal))); - node.addContent(new Element("option").setAttribute("name", IGNORE_EMPTY_METHODS_ATTR_NAME).setAttribute("value", String.valueOf( - m_ignoreEmptyMethods))); - if (!m_ignoreDefaultMethods) { - node.addContent(new Element("option").setAttribute("name", IGNORE_DEFAULT_METHODS_ATTR_NAME).setAttribute("value", "false")); - } - if (!m_replaceQualifier) { - node.addContent(new Element("option").setAttribute("name", REPLACE_QUALIFIER_ATTR_NAME).setAttribute("value", "false")); - } - } - - private class MethodCanBeStaticVisitor extends BaseInspectionVisitor { - - @Override - public void visitMethod(@NotNull PsiMethod method) { - super.visitMethod(method); - if (method.hasModifierProperty(PsiModifier.STATIC) || - method.hasModifierProperty(PsiModifier.ABSTRACT) || - method.hasModifierProperty(PsiModifier.SYNCHRONIZED) || - method.hasModifierProperty(PsiModifier.NATIVE)) { - return; - } - if (method.isConstructor() || method.getNameIdentifier() == null) { - return; - } - if (m_ignoreDefaultMethods && method.hasModifierProperty(PsiModifier.DEFAULT)) { - return; - } - if (m_ignoreEmptyMethods && MethodUtils.isEmpty(method)) { - return; - } - final PsiClass containingClass = ClassUtils.getContainingClass(method); - if (containingClass == null) { - return; - } - final Condition<PsiElement>[] addins = InspectionManager.CANT_BE_STATIC_EXTENSION.getExtensions(); - for (Condition<PsiElement> addin : addins) { - if (addin.value(method)) { - return; - } - } - final PsiElement scope = containingClass.getScope(); - if (!(scope instanceof PsiJavaFile) && !containingClass.hasModifierProperty(PsiModifier.STATIC) && !containingClass.isInterface()) { - return; - } - if (m_onlyPrivateOrFinal && !method.hasModifierProperty(PsiModifier.FINAL) && !method.hasModifierProperty(PsiModifier.PRIVATE)) { - return; - } - if (isExcluded(method) || MethodUtils.hasSuper(method) || MethodUtils.isOverridden(method)) { - return; - } - if (implementsSurprisingInterface(method)) { - return; - } - final MethodReferenceVisitor visitor = new MethodReferenceVisitor(method); - method.accept(visitor); - if (!visitor.areReferencesStaticallyAccessible()) { - return; - } - registerMethodError(method); - } - - private boolean implementsSurprisingInterface(final PsiMethod method) { - final PsiClass containingClass = method.getContainingClass(); - if (containingClass == null) { - return false; - } - final Query<PsiClass> search = ClassInheritorsSearch.search(containingClass, method.getUseScope(), true, true, false); - final boolean[] result = new boolean[1]; - search.forEach(new Processor<PsiClass>() { - AtomicInteger count = new AtomicInteger(0); - - @Override - public boolean process(PsiClass subClass) { - if (count.incrementAndGet() > 5) { - result[0] = true; - return false; - } - final PsiReferenceList list = subClass.getImplementsList(); - if (list == null) { - return true; - } - final PsiJavaCodeReferenceElement[] referenceElements = list.getReferenceElements(); - for (PsiJavaCodeReferenceElement referenceElement : referenceElements) { - final PsiElement target = referenceElement.resolve(); - if (!(target instanceof PsiClass)) { - result[0] = true; - return false; - } - final PsiClass aClass = (PsiClass)target; - if (!aClass.isInterface()) { - result[0] = true; - return false; - } - if (aClass.findMethodBySignature(method, true) != null) { - result[0] = true; - return false; - } - } - return true; - } - }); - return result[0]; - } - - private boolean isExcluded(PsiMethod method) { - return SerializationUtils.isWriteObject(method) || SerializationUtils.isReadObject(method) || - SerializationUtils.isWriteReplace(method) || SerializationUtils.isReadResolve(method); - } - } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspection.java index e79f9df5fa7d..25109cc5f26e 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspection.java @@ -15,6 +15,7 @@ */ package com.siyeh.ig.style; +import com.intellij.codeInsight.javadoc.JavaDocUtil; import com.intellij.codeInspection.CleanupLocalInspectionTool; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.diagnostic.Logger; @@ -205,11 +206,8 @@ public class UnnecessaryFullyQualifiedNameInspection extends BaseInspection impl return; } final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(reference.getProject()); - if (styleSettings.USE_FQ_CLASS_NAMES_IN_JAVADOC) { - final PsiElement containingComment = PsiTreeUtil.getParentOfType(reference, PsiDocComment.class); - if (containingComment != null) { - return; - } + if (acceptFullyQualifiedNamesInJavadoc(reference, styleSettings)) { + return; } final PsiFile containingFile = reference.getContainingFile(); if (!(containingFile instanceof PsiJavaFile)) { @@ -261,4 +259,14 @@ public class UnnecessaryFullyQualifiedNameInspection extends BaseInspection impl } } } + + public static boolean acceptFullyQualifiedNamesInJavadoc(PsiJavaCodeReferenceElement reference, CodeStyleSettings styleSettings) { + final PsiDocComment containingComment = PsiTreeUtil.getParentOfType(reference, PsiDocComment.class); + if (containingComment != null) { + if (styleSettings.USE_FQ_CLASS_NAMES_IN_JAVADOC || JavaDocUtil.isInsidePackageInfo(containingComment)) { + return true; + } + } + return false; + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java index 5c53674cd15b..5af84ecb21e7 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_collection_query_update/MismatchedCollectionQueryUpdate.java @@ -275,4 +275,18 @@ class SimpleAdd { return null; } +} +class EnumConstant { + private static final List<String> CONSTANT_ARRAY = new ArrayList(); + static { + CONSTANT_ARRAY.add("asdf"); + } + + enum SomeEnum { + ITEM(CONSTANT_ARRAY); // passed as argument + private final List<String> myPatterns; + SomeEnum(List<String> patterns) { + myPatterns = patterns; + } + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java index c8d9fb80cf19..2274472eb2fa 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/bugs/mismatched_string_builder_query_update/MismatchedStringBuilderQueryUpdate.java @@ -31,3 +31,15 @@ interface List<T> { interface Consumer<T> { void accept(T t); } +class EnumConstant { + private static final StringBuilder sb = new StringBuilder(); + static { + sb.append(""); + } + + enum SomeEnum { + ITEM(sb); // passed as argument + + SomeEnum(StringBuilder sb) {} + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/IfStatementWithIdenticalBranches.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/IfStatementWithIdenticalBranches.java index 666651d71e21..fef2e0f1f6ff 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/IfStatementWithIdenticalBranches.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/IfStatementWithIdenticalBranches.java @@ -162,4 +162,15 @@ class NotADup { private static String placeOrder(int i, Object o) { return null; } + + void m() { + int j; + <warning descr="'if' statement with identical branches">if</warning> (true) { + j = 2; + } + else { + j = 2; + } + System.out.println("j = " + j); + } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java index 032fa0002dea..a161e388ce3b 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.after.java @@ -117,4 +117,18 @@ class TryIdenticalCatches { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } + + public static void main() { + Throwable causeException; + try { + throw new NullPointerException(); + } catch (final NullPointerException e) { + causeException = e; + } catch (final IllegalArgumentException e) { + causeException = e; + } catch (final IndexOutOfBoundsException e) { + causeException = e; + } + System.out.println("causeException = " + causeException); + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java index 5d33c50de89e..09694491d69d 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/errorhandling/try_identical_catches/TryIdenticalCatches.java @@ -120,4 +120,18 @@ class TryIdenticalCatches { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } + + public static void main() { + Throwable causeException; + try { + throw new NullPointerException(); + } catch (final NullPointerException e) { + causeException = e; + } <warning descr="catch branch identical to 'NullPointerException' branch">catch (final IllegalArgumentException e)</warning> { + causeException = e; + } <warning descr="catch branch identical to 'NullPointerException' branch">catch (final IndexOutOfBoundsException e)</warning> { + causeException = e; + } + System.out.println("causeException = " + causeException); + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/internationalization/implicit_default_charset_usage/ImplicitDefaultCharsetUsage.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/internationalization/implicit_default_charset_usage/ImplicitDefaultCharsetUsage.java index 17dd59eadf7a..151042fa3ed9 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/internationalization/implicit_default_charset_usage/ImplicitDefaultCharsetUsage.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/internationalization/implicit_default_charset_usage/ImplicitDefaultCharsetUsage.java @@ -1,6 +1,9 @@ package com.siyeh.igtest.internationalization.implicit_default_charset_usage; import java.io.*; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; import java.util.ArrayList; import java.util.Formatter; import java.util.Locale; @@ -37,4 +40,12 @@ class ImplicitDefaultCharsetUsage { new Scanner("string input"); new ArrayList(10); } + + void charsetEnAndDecoders(InputStream inputStream, OutputStream outputStream) throws IOException { + final Charset cs = Charset.forName("UTF-8"); + CharsetDecoder cd = cs.newDecoder(); + InputStreamReader is = new InputStreamReader(inputStream, cd); + CharsetEncoder ce = cs.newEncoder(); + final OutputStreamWriter ow = new OutputStreamWriter(outputStream, ce); + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/logging/logger_initialized_with_foreign_class/LoggerInitializedWithForeignClass.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/logging/logger_initialized_with_foreign_class/LoggerInitializedWithForeignClass.java new file mode 100644 index 000000000000..b252b8af1cd3 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/logging/logger_initialized_with_foreign_class/LoggerInitializedWithForeignClass.java @@ -0,0 +1,13 @@ +import java.util.logging.*; + +class LoggerInitializedWithForeignClass { + + void foo() { + new Object() { + void bar() { + Logger.getLogger(LoggerInitializedWithForeignClass.class.getName()); + Logger.getLogger(<warning descr="Logger initialized with foreign class 'String.class'">String.class</warning>.getName()); + } + }; + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_fully_qualified_name/test1/package-info.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_fully_qualified_name/test1/package-info.java new file mode 100644 index 000000000000..01825303d455 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/style/unnecessary_fully_qualified_name/test1/package-info.java @@ -0,0 +1,3 @@ +/** + * {@link java.util.Map(java.util.function.BiConsumer)} +*/
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspectionTest.java index 78822492b4bc..312e08aebcc3 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/internationalization/ImplicitDefaultCharsetUsageInspectionTest.java @@ -16,13 +16,22 @@ package com.siyeh.ig.internationalization; import com.intellij.codeInspection.InspectionProfileEntry; +import com.intellij.testFramework.LightProjectDescriptor; import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.NotNull; /** * @author Bas Leijdekkers */ public class ImplicitDefaultCharsetUsageInspectionTest extends LightInspectionTestCase { + + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_8; + } + @Override protected InspectionProfileEntry getInspection() { return new ImplicitDefaultCharsetUsageInspection(); diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionTest.java new file mode 100644 index 000000000000..7f0b521fcf4e --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/LoggerInitializedWithForeignClassInspectionTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * 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.logging; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class LoggerInitializedWithForeignClassInspectionTest extends LightInspectionTestCase { + + public void testLoggerInitializedWithForeignClass() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + final LoggerInitializedWithForeignClassInspection inspection = new LoggerInitializedWithForeignClassInspection(); + inspection.loggerFactoryClassNames.add("java.util.logging.Logger"); + inspection.loggerFactoryMethodNames.add("getLogger"); + return inspection; + } + + @Override + protected String[] getEnvironmentClasses() { + return new String[] { + "package java.util.logging;" + + "public class Logger {" + + " public static Logger getLogger(String name) {" + + " return null;" + + " }" + + "}" + }; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspectionTest.java index 1bd7feadae45..48d120189740 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/logging/PlaceholderCountMatchesArgumentCountInspectionTest.java @@ -32,7 +32,7 @@ public class PlaceholderCountMatchesArgumentCountInspectionTest extends LightIns "class X {\n" + " void foo() {\n" + " Logger logger = LoggerFactory.getLogger(X.class);\n" + - " logger./*'info()' call has fewer arguments (1) than placeholders (2)*/info/**/(\"string {}{}\", 1);\n" + + " logger.info(/*Fewer arguments provided (1) than placeholders specified (2) in 'string {}{}'*/\"string {}{}\"/**/, 1);\n" + " }\n" + "}" ); @@ -43,7 +43,7 @@ public class PlaceholderCountMatchesArgumentCountInspectionTest extends LightIns "class X {\n" + " void foo() {\n" + " Logger logger = LoggerFactory.getLogger(X.class);\n" + - " logger./*'info()' call has more arguments (1) than placeholders (0)*/info/**/(\"string\", 1);\n" + + " logger.info(/*More arguments provided (1) than placeholders specified (0) in 'string'*/\"string\"/**/, 1);\n" + " }\n" + "}" ); @@ -107,4 +107,15 @@ public class PlaceholderCountMatchesArgumentCountInspectionTest extends LightIns " }" + "}"); } + + public void testConstant() { + doTest("import org.slf4j.*;" + + "class X {" + + " Logger LOG = LoggerFactory.getLogger(X.class);" + + " private static final String message = \"HELLO {}\";" + + " void m() {" + + " LOG.info(/*Fewer arguments provided (0) than placeholders specified (1) in 'HELLO {}'*/message/**/);" + + " }" + + "}"); + } }
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java index 6c5a89009735..2e1b839c85d3 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/style/UnnecessaryFullyQualifiedNameInspectionTest.java @@ -1,11 +1,22 @@ package com.siyeh.ig.style; +import com.intellij.psi.codeStyle.CodeStyleSettings; +import com.intellij.psi.codeStyle.CodeStyleSettingsManager; import com.siyeh.ig.IGInspectionTestCase; public class UnnecessaryFullyQualifiedNameInspectionTest extends IGInspectionTestCase { public void test() throws Exception { - doTest("com/siyeh/igtest/style/unnecessary_fully_qualified_name", - new UnnecessaryFullyQualifiedNameInspection()); + final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(getProject()); + + boolean inJavadoc = styleSettings.USE_FQ_CLASS_NAMES_IN_JAVADOC; + try { + styleSettings.USE_FQ_CLASS_NAMES_IN_JAVADOC = false; + doTest("com/siyeh/igtest/style/unnecessary_fully_qualified_name", + new UnnecessaryFullyQualifiedNameInspection()); + } + finally { + styleSettings.USE_FQ_CLASS_NAMES_IN_JAVADOC = inJavadoc; + } } } |