diff options
Diffstat (limited to 'plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com')
13 files changed, 356 insertions, 421 deletions
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index efeb65889a93..f92925b33bc0 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -2096,3 +2096,5 @@ serializable.local.class.stores.non.serializable.problem.descriptor=Serializable serializable.anonymous.class.stores.non.serializable.problem.descriptor=Serializable anonymous class implicitly stores non-Serializable object of type ''{0}'' assignment.to.lambda.parameter.display.name=Assignment to lambda parameter assignment.to.lambda.parameter.problem.descriptor=Assignment to lambda parameter <code>#ref</code> #loc +class.with.only.private.constructors.display.name=Class with only 'private' constructors should be declared 'final' +class.with.only.private.constructors.problem.descriptor=Class <code>#ref</code> with only 'private' constructors should be declared 'final' diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/BaseGlobalInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/BaseGlobalInspection.java index b0f4a3633ad9..8d2e5049cdbc 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/BaseGlobalInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/BaseGlobalInspection.java @@ -16,6 +16,7 @@ package com.siyeh.ig; import com.intellij.codeInspection.GlobalJavaBatchInspectionTool; +import com.intellij.codeInspection.InspectionProfileEntry; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -34,8 +35,7 @@ public abstract class BaseGlobalInspection extends GlobalJavaBatchInspectionTool assert name.endsWith(INSPECTION) : "class name must end with 'Inspection' to correctly" + " calculate the short name: " + name; - shortName = name.substring(name.lastIndexOf((int)'.') + 1, - name.length() - INSPECTION.length()); + shortName = InspectionProfileEntry.getShortName(getClass().getSimpleName()); } return shortName; } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/AssignmentToMethodParameterInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/AssignmentToMethodParameterInspection.java index 56dbecc83d6b..5fb2f2b8ff5e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/AssignmentToMethodParameterInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/assignment/AssignmentToMethodParameterInspection.java @@ -26,9 +26,6 @@ import javax.swing.*; public class AssignmentToMethodParameterInspection extends BaseAssignmentToParameterInspection { - @SuppressWarnings({"PublicField"}) - public boolean ignoreTransformationOfOriginalParameter = false; - @Override @NotNull public String getDisplayName() { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspectionBase.java new file mode 100644 index 000000000000..6d091699ce82 --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspectionBase.java @@ -0,0 +1,91 @@ +/* + * 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.classlayout; + +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiModifier; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.BaseInspection; +import com.siyeh.ig.BaseInspectionVisitor; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +import java.util.HashSet; +import java.util.Set; + +/** + * @author Bas Leijdekkers + */ +public class ClassWithOnlyPrivateConstructorsInspectionBase extends BaseInspection { + @Nls + @NotNull + @Override + public String getDisplayName() { + return InspectionGadgetsBundle.message("class.with.only.private.constructors.display.name"); + } + + @NotNull + @Override + protected String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("class.with.only.private.constructors.problem.descriptor"); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new ClassWithOnlyPrivateConstructorsVisitor(); + } + + private static class ClassWithOnlyPrivateConstructorsVisitor extends BaseInspectionVisitor { + + @Override + public void visitClass(PsiClass aClass) { + super.visitClass(aClass); + final PsiMethod[] constructors = aClass.getConstructors(); + if (constructors.length == 0) { + return; + } + for (PsiMethod constructor : constructors) { + if (constructor.hasModifierProperty(PsiModifier.FINAL)) { + return; + } + } + final PsiClass[] innerClasses = aClass.getInnerClasses(); + for (PsiClass innerClass : innerClasses) { + if (isExtendedByInnerClass(innerClass, aClass, new HashSet<PsiClass>())) { + return; + } + } + registerClassError(aClass, aClass); + } + + private static boolean isExtendedByInnerClass(PsiClass innerClass, PsiClass superClass, Set<PsiClass> visited) { + if (!visited.add(innerClass)) { + return false; + } + if (innerClass.isInheritor(superClass, false)) { + return true; + } + final PsiClass[] innerClasses = innerClass.getInnerClasses(); + for (PsiClass aClass : innerClasses) { + if (isExtendedByInnerClass(aClass, superClass, visited)) { + return true; + } + } + return false; + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspection.java index 755a4c349e21..0b7524de01a4 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspection.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. @@ -17,12 +17,15 @@ package com.siyeh.ig.controlflow; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.Ref; import com.intellij.psi.*; import com.intellij.psi.search.LocalSearchScope; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.refactoring.extractMethod.InputVariables; +import com.intellij.refactoring.util.duplicates.ConditionalReturnStatementValue; import com.intellij.refactoring.util.duplicates.DuplicatesFinder; import com.intellij.refactoring.util.duplicates.Match; +import com.intellij.refactoring.util.duplicates.ReturnValue; import com.intellij.util.IncorrectOperationException; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; @@ -158,12 +161,10 @@ public class IfStatementWithIdenticalBranchesInspection return new IfStatementWithIdenticalBranchesVisitor(); } - private static class IfStatementWithIdenticalBranchesVisitor - extends BaseInspectionVisitor { + private static class IfStatementWithIdenticalBranchesVisitor extends BaseInspectionVisitor { @Override - public void visitIfStatement( - @NotNull PsiIfStatement ifStatement) { + public void visitIfStatement(@NotNull PsiIfStatement ifStatement) { super.visitIfStatement(ifStatement); final PsiStatement elseBranch = ifStatement.getElseBranch(); final PsiStatement thenBranch = ifStatement.getThenBranch(); @@ -179,14 +180,18 @@ public class IfStatementWithIdenticalBranchesInspection inputVariables, null, Collections.<PsiVariable>emptyList()); if (elseBranch instanceof PsiIfStatement) { - final PsiIfStatement statement = - (PsiIfStatement)elseBranch; + final PsiIfStatement statement = (PsiIfStatement)elseBranch; final PsiStatement branch = statement.getThenBranch(); if (branch == null) { return; } final Match match = finder.isDuplicate(branch, true); - if (match != null && match.getReturnValue() == null) { + if (match != null) { + final ReturnValue matchReturnValue = match.getReturnValue(); + if (matchReturnValue instanceof ConditionalReturnStatementValue && + !matchReturnValue.isEquivalent(buildReturnValue(thenBranch))) { + return; + } registerStatementError(ifStatement, statement); return; } @@ -197,11 +202,37 @@ public class IfStatementWithIdenticalBranchesInspection else { final Match match = finder.isDuplicate(elseBranch, true); if (match != null) { + final ReturnValue matchReturnValue = match.getReturnValue(); + if (matchReturnValue instanceof ConditionalReturnStatementValue && + !matchReturnValue.isEquivalent(buildReturnValue(thenBranch))) { + return; + } registerStatementError(ifStatement); } } } + @Nullable + private ReturnValue buildReturnValue(PsiElement element) { + final Ref<PsiReturnStatement> result = Ref.create(null); + element.accept(new JavaRecursiveElementWalkingVisitor() { + @Override + public void visitReturnStatement(PsiReturnStatement statement) { + super.visitReturnStatement(statement); + result.set(statement); + } + }); + final PsiReturnStatement returnStatement = result.get(); + if (returnStatement == null) { + return null; + } + final PsiExpression expression = returnStatement.getReturnValue(); + if (expression == null) { + return null; + } + return new ConditionalReturnStatementValue(expression); + } + private void checkIfStatementWithoutElseBranch( PsiIfStatement ifStatement) { final PsiStatement thenBranch = ifStatement.getThenBranch(); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java index ad4273156865..0bc871664bfd 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/UnnecessaryLocalVariableInspectionBase.java @@ -97,11 +97,6 @@ public class UnnecessaryLocalVariableInspectionBase extends BaseInspection { } @Override - protected boolean buildQuickFixesOnlyForOnTheFlyErrors() { - return true; - } - - @Override public BaseInspectionVisitor buildVisitor() { return new UnnecessaryLocalVariableVisitor(); } diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java index bebf5a559d15..023b4bc9f949 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java @@ -105,7 +105,7 @@ public class PointlessArithmeticExpressionInspection fromTarget = (i == length - 1) ? polyadicExpression.getTokenBeforeOperand(operand) : operand; break; } - else if ((tokenType.equals(JavaTokenType.MINUS) || tokenType.equals(JavaTokenType.DIV)) && + else if ((tokenType.equals(JavaTokenType.MINUS) && i == 1 || tokenType.equals(JavaTokenType.DIV)) && EquivalenceChecker.expressionsAreEquivalent(previousOperand, operand)) { fromTarget = previousOperand; untilTarget = operand; @@ -229,9 +229,10 @@ public class PointlessArithmeticExpressionInspection private boolean subtractionExpressionIsPointless(PsiExpression[] expressions) { PsiExpression previousExpression = null; - for (PsiExpression expression : expressions) { + for (int i = 0; i < expressions.length; i++) { + PsiExpression expression = expressions[i]; if (previousExpression != null && - (isZero(expression) || EquivalenceChecker.expressionsAreEquivalent(previousExpression, expression))) { + (isZero(expression) || i == 1 && EquivalenceChecker.expressionsAreEquivalent(previousExpression, expression))) { return true; } previousExpression = expression; diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspection.java deleted file mode 100644 index 997e57ba0cda..000000000000 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspection.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright 2003-2011 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. - * 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.ProblemDescriptor; -import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; -import com.intellij.openapi.project.Project; -import com.intellij.psi.*; -import com.intellij.psi.search.searches.OverridingMethodsSearch; -import com.intellij.psi.util.PropertyUtil; -import com.intellij.util.IncorrectOperationException; -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.PsiReplacementUtil; -import com.siyeh.ig.psiutils.ClassUtils; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; - -public class CallToSimpleGetterInClassInspection extends BaseInspection { - - @SuppressWarnings("UnusedDeclaration") - public boolean ignoreGetterCallsOnOtherObjects = false; - - @SuppressWarnings("UnusedDeclaration") - public boolean onlyReportPrivateGetter = false; - - @Override - @NotNull - public String getID() { - return "CallToSimpleGetterFromWithinClass"; - } - - @Override - @NotNull - public String getDisplayName() { - return InspectionGadgetsBundle.message("call.to.simple.getter.in.class.display.name"); - } - - @Override - @NotNull - public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message("call.to.simple.getter.in.class.problem.descriptor"); - } - - @Override - @Nullable - public JComponent createOptionsPanel() { - final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message("call.to.simple.getter.in.class.ignore.option"), - "ignoreGetterCallsOnOtherObjects"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message("call.to.private.simple.getter.in.class.option"), - "onlyReportPrivateGetter"); - return optionsPanel; - } - - @Override - public InspectionGadgetsFix buildFix(Object... infos) { - return new InlineCallFix(); - } - - private static class InlineCallFix extends InspectionGadgetsFix { - @NotNull - @Override - public String getFamilyName() { - return getName(); - } - - @Override - @NotNull - public String getName() { - return InspectionGadgetsBundle.message("call.to.simple.getter.in.class.inline.quickfix"); - } - - @Override - public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { - final PsiElement methodIdentifier = descriptor.getPsiElement(); - final PsiReferenceExpression methodExpression = (PsiReferenceExpression)methodIdentifier.getParent(); - if (methodExpression == null) { - return; - } - final PsiMethodCallExpression call = (PsiMethodCallExpression)methodExpression.getParent(); - if (call == null) { - return; - } - final PsiMethod method = call.resolveMethod(); - if (method == null) { - return; - } - final PsiCodeBlock body = method.getBody(); - if (body == null) { - return; - } - final PsiStatement[] statements = body.getStatements(); - final PsiReturnStatement returnStatement = (PsiReturnStatement)statements[0]; - final PsiExpression returnValue = returnStatement.getReturnValue(); - if (!(returnValue instanceof PsiReferenceExpression)) { - return; - } - final PsiReferenceExpression referenceExpression = (PsiReferenceExpression)returnValue; - final PsiField field = (PsiField)referenceExpression.resolve(); - if (field == null) { - return; - } - final String fieldName = field.getName(); - if (fieldName == null) { - return; - } - final PsiExpression qualifier = methodExpression.getQualifierExpression(); - if (qualifier == null) { - final JavaPsiFacade facade = JavaPsiFacade.getInstance(call.getProject()); - final PsiResolveHelper resolveHelper = facade.getResolveHelper(); - final PsiVariable variable = resolveHelper.resolveReferencedVariable(fieldName, call); - if (variable == null) { - return; - } - if (variable.equals(field)) { - PsiReplacementUtil.replaceExpression(call, fieldName); - } - else { - PsiReplacementUtil.replaceExpression(call, "this." + fieldName); - } - } - else { - PsiReplacementUtil.replaceExpression(call, qualifier.getText() + '.' + fieldName); - } - } - } - - @Override - public BaseInspectionVisitor buildVisitor() { - return new CallToSimpleGetterInClassVisitor(); - } - - private class CallToSimpleGetterInClassVisitor extends BaseInspectionVisitor { - - @Override - public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { - super.visitMethodCallExpression(call); - final PsiClass containingClass = ClassUtils.getContainingClass(call); - if (containingClass == null) { - return; - } - final PsiMethod method = call.resolveMethod(); - if (method == null) { - return; - } - if (!containingClass.equals(method.getContainingClass())) { - return; - } - final PsiReferenceExpression methodExpression = call.getMethodExpression(); - final PsiExpression qualifier = methodExpression.getQualifierExpression(); - if (qualifier != null && !(qualifier instanceof PsiThisExpression)) { - if (ignoreGetterCallsOnOtherObjects) { - return; - } - final PsiType type = qualifier.getType(); - if (!(type instanceof PsiClassType)) { - return; - } - final PsiClassType classType = (PsiClassType)type; - final PsiClass qualifierClass = classType.resolve(); - if (!containingClass.equals(qualifierClass)) { - return; - } - } - if (!PropertyUtil.isSimpleGetter(method)) { - return; - } - if (onlyReportPrivateGetter && !method.hasModifierProperty(PsiModifier.PRIVATE)) { - return; - } - final Query<PsiMethod> query = OverridingMethodsSearch.search(method, true); - final PsiMethod overridingMethod = query.findFirst(); - if (overridingMethod != null) { - return; - } - registerMethodCallError(call); - } - } -}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspectionBase.java new file mode 100644 index 000000000000..f21fd50918da --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspectionBase.java @@ -0,0 +1,103 @@ +/* + * 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.psi.*; +import com.intellij.psi.search.searches.OverridingMethodsSearch; +import com.intellij.psi.util.PropertyUtil; +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 org.jetbrains.annotations.NotNull; + +public class CallToSimpleGetterInClassInspectionBase extends BaseInspection { + @SuppressWarnings("UnusedDeclaration") + public boolean ignoreGetterCallsOnOtherObjects = false; + @SuppressWarnings("UnusedDeclaration") + public boolean onlyReportPrivateGetter = false; + + @Override + @NotNull + public String getID() { + return "CallToSimpleGetterFromWithinClass"; + } + + @Override + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message("call.to.simple.getter.in.class.display.name"); + } + + @Override + @NotNull + public String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("call.to.simple.getter.in.class.problem.descriptor"); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new CallToSimpleGetterInClassVisitor(); + } + + private class CallToSimpleGetterInClassVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { + super.visitMethodCallExpression(call); + final PsiClass containingClass = ClassUtils.getContainingClass(call); + if (containingClass == null) { + return; + } + final PsiMethod method = call.resolveMethod(); + if (method == null) { + return; + } + if (!containingClass.equals(method.getContainingClass())) { + return; + } + final PsiReferenceExpression methodExpression = call.getMethodExpression(); + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (qualifier != null && !(qualifier instanceof PsiThisExpression)) { + if (ignoreGetterCallsOnOtherObjects) { + return; + } + final PsiType type = qualifier.getType(); + if (!(type instanceof PsiClassType)) { + return; + } + final PsiClassType classType = (PsiClassType)type; + final PsiClass qualifierClass = classType.resolve(); + if (!containingClass.equals(qualifierClass)) { + return; + } + } + if (!PropertyUtil.isSimpleGetter(method)) { + return; + } + if (onlyReportPrivateGetter && !method.hasModifierProperty(PsiModifier.PRIVATE)) { + return; + } + final Query<PsiMethod> query = OverridingMethodsSearch.search(method, true); + final PsiMethod overridingMethod = query.findFirst(); + if (overridingMethod != null) { + return; + } + registerMethodCallError(call); + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspection.java deleted file mode 100644 index bfcf47cd0465..000000000000 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspection.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright 2003-2011 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. - * 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.ProblemDescriptor; -import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel; -import com.intellij.openapi.project.Project; -import com.intellij.psi.*; -import com.intellij.psi.search.searches.OverridingMethodsSearch; -import com.intellij.psi.util.PropertyUtil; -import com.intellij.util.IncorrectOperationException; -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.PsiReplacementUtil; -import com.siyeh.ig.psiutils.ClassUtils; -import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; - -public class CallToSimpleSetterInClassInspection extends BaseInspection { - - @SuppressWarnings("UnusedDeclaration") - public boolean ignoreSetterCallsOnOtherObjects = false; - - @SuppressWarnings("UnusedDeclaration") - public boolean onlyReportPrivateSetter = false; - - @Override - @NotNull - public String getID() { - return "CallToSimpleSetterFromWithinClass"; - } - - @Override - @NotNull - public String getDisplayName() { - return InspectionGadgetsBundle.message("call.to.simple.setter.in.class.display.name"); - } - - @Override - @NotNull - public String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message("call.to.simple.setter.in.class.problem.descriptor"); - } - - @Override - @Nullable - public JComponent createOptionsPanel() { - final MultipleCheckboxOptionsPanel optionsPanel = new MultipleCheckboxOptionsPanel(this); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message("call.to.simple.setter.in.class.ignore.option"), - "ignoreSetterCallsOnOtherObjects"); - optionsPanel.addCheckbox(InspectionGadgetsBundle.message("call.to.private.setter.in.class.option"), - "onlyReportPrivateSetter"); - return optionsPanel; - } - - @Override - public InspectionGadgetsFix buildFix(Object... infos) { - return new InlineCallFix(); - } - - private static class InlineCallFix extends InspectionGadgetsFix { - - @Override - @NotNull - public String getName() { - return InspectionGadgetsBundle.message("call.to.simple.setter.in.class.inline.quickfix"); - } - - @NotNull - @Override - public String getFamilyName() { - return getName(); - } - - @Override - public void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { - final PsiElement methodIdentifier = descriptor.getPsiElement(); - final PsiReferenceExpression methodExpression = (PsiReferenceExpression)methodIdentifier.getParent(); - if (methodExpression == null) { - return; - } - final PsiMethodCallExpression call = (PsiMethodCallExpression)methodExpression.getParent(); - if (call == null) { - return; - } - final PsiExpressionList argumentList = call.getArgumentList(); - final PsiExpression[] arguments = argumentList.getExpressions(); - final PsiExpression argument = arguments[0]; - final PsiMethod method = call.resolveMethod(); - if (method == null) { - return; - } - final PsiCodeBlock body = method.getBody(); - if (body == null) { - return; - } - final PsiStatement[] statements = body.getStatements(); - final PsiExpressionStatement assignmentStatement = (PsiExpressionStatement)statements[0]; - final PsiAssignmentExpression assignment = (PsiAssignmentExpression)assignmentStatement.getExpression(); - final PsiExpression qualifier = methodExpression.getQualifierExpression(); - final PsiReferenceExpression lhs = (PsiReferenceExpression)assignment.getLExpression(); - final PsiField field = (PsiField)lhs.resolve(); - if (field == null) { - return; - } - final String fieldName = field.getName(); - if (qualifier == null) { - final JavaPsiFacade manager = JavaPsiFacade.getInstance(call.getProject()); - final PsiResolveHelper resolveHelper = manager.getResolveHelper(); - final PsiVariable variable = resolveHelper.resolveReferencedVariable(fieldName, call); - if (variable == null) { - return; - } - @NonNls final String newExpression; - if (variable.equals(field)) { - newExpression = fieldName + " = " + argument.getText(); - } - else { - newExpression = "this." + fieldName + " = " + argument.getText(); - } - PsiReplacementUtil.replaceExpression(call, newExpression); - } - else { - final String newExpression = qualifier.getText() + '.' + fieldName + " = " + argument.getText(); - PsiReplacementUtil.replaceExpression(call, newExpression); - } - } - } - - @Override - public BaseInspectionVisitor buildVisitor() { - return new CallToSimpleSetterInClassVisitor(); - } - - private class CallToSimpleSetterInClassVisitor extends BaseInspectionVisitor { - - @Override - public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { - super.visitMethodCallExpression(call); - final PsiClass containingClass = ClassUtils.getContainingClass(call); - if (containingClass == null) { - return; - } - final PsiMethod method = call.resolveMethod(); - if (method == null) { - return; - } - if (!containingClass.equals(method.getContainingClass())) { - return; - } - final PsiReferenceExpression methodExpression = call.getMethodExpression(); - final PsiExpression qualifier = methodExpression.getQualifierExpression(); - if (qualifier != null && !(qualifier instanceof PsiThisExpression)) { - if (ignoreSetterCallsOnOtherObjects) { - return; - } - final PsiType type = qualifier.getType(); - if (!(type instanceof PsiClassType)) { - return; - } - final PsiClassType classType = (PsiClassType)type; - final PsiClass qualifierClass = classType.resolve(); - if (!containingClass.equals(qualifierClass)) { - return; - } - } - if (!PropertyUtil.isSimpleSetter(method)) { - return; - } - if (onlyReportPrivateSetter && !method.hasModifierProperty(PsiModifier.PRIVATE)) { - return; - } - final Query<PsiMethod> query = OverridingMethodsSearch.search(method, true); - final PsiMethod overridingMethod = query.findFirst(); - if (overridingMethod != null) { - return; - } - registerMethodCallError(call); - } - } -}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspectionBase.java new file mode 100644 index 000000000000..6e6512a8866a --- /dev/null +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspectionBase.java @@ -0,0 +1,103 @@ +/* + * 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.psi.*; +import com.intellij.psi.search.searches.OverridingMethodsSearch; +import com.intellij.psi.util.PropertyUtil; +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 org.jetbrains.annotations.NotNull; + +public class CallToSimpleSetterInClassInspectionBase extends BaseInspection { + @SuppressWarnings("UnusedDeclaration") + public boolean ignoreSetterCallsOnOtherObjects = false; + @SuppressWarnings("UnusedDeclaration") + public boolean onlyReportPrivateSetter = false; + + @Override + @NotNull + public String getID() { + return "CallToSimpleSetterFromWithinClass"; + } + + @Override + @NotNull + public String getDisplayName() { + return InspectionGadgetsBundle.message("call.to.simple.setter.in.class.display.name"); + } + + @Override + @NotNull + public String buildErrorString(Object... infos) { + return InspectionGadgetsBundle.message("call.to.simple.setter.in.class.problem.descriptor"); + } + + @Override + public BaseInspectionVisitor buildVisitor() { + return new CallToSimpleSetterInClassVisitor(); + } + + private class CallToSimpleSetterInClassVisitor extends BaseInspectionVisitor { + + @Override + public void visitMethodCallExpression(@NotNull PsiMethodCallExpression call) { + super.visitMethodCallExpression(call); + final PsiClass containingClass = ClassUtils.getContainingClass(call); + if (containingClass == null) { + return; + } + final PsiMethod method = call.resolveMethod(); + if (method == null) { + return; + } + if (!containingClass.equals(method.getContainingClass())) { + return; + } + final PsiReferenceExpression methodExpression = call.getMethodExpression(); + final PsiExpression qualifier = methodExpression.getQualifierExpression(); + if (qualifier != null && !(qualifier instanceof PsiThisExpression)) { + if (ignoreSetterCallsOnOtherObjects) { + return; + } + final PsiType type = qualifier.getType(); + if (!(type instanceof PsiClassType)) { + return; + } + final PsiClassType classType = (PsiClassType)type; + final PsiClass qualifierClass = classType.resolve(); + if (!containingClass.equals(qualifierClass)) { + return; + } + } + if (!PropertyUtil.isSimpleSetter(method)) { + return; + } + if (onlyReportPrivateSetter && !method.hasModifierProperty(PsiModifier.PRIVATE)) { + return; + } + final Query<PsiMethod> query = OverridingMethodsSearch.search(method, true); + final PsiMethod overridingMethod = query.findFirst(); + if (overridingMethod != null) { + return; + } + registerMethodCallError(call); + } + } +} diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java index 712449655cdd..375e0f6a8017 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ImportUtils.java @@ -388,6 +388,14 @@ public class ImportUtils { } final PsiImportStatementBase existingImportStatement = importList.findSingleImportStatement(memberName); if (existingImportStatement != null) { + if (existingImportStatement instanceof PsiImportStaticStatement) { + final PsiImportStaticStatement importStaticStatement = (PsiImportStaticStatement)existingImportStatement; + if (!memberName.equals(importStaticStatement.getReferenceName())) { + return false; + } + final PsiClass targetClass = importStaticStatement.resolveTargetClass(); + return targetClass != null && qualifierClass.equals(targetClass.getQualifiedName()); + } return false; } final PsiImportStaticStatement onDemandImportStatement = findOnDemandImportStaticStatement(importList, qualifierClass); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryInterfaceModifierInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryInterfaceModifierInspection.java index 0a4b57e8543b..310cc20c30b6 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryInterfaceModifierInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/style/UnnecessaryInterfaceModifierInspection.java @@ -19,6 +19,7 @@ import com.intellij.codeInspection.CleanupLocalInspectionTool; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.openapi.project.Project; import com.intellij.psi.*; +import com.intellij.psi.util.PsiUtil; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; @@ -121,8 +122,10 @@ public class UnnecessaryInterfaceModifierInspection extends BaseInspection imple } modifierList = (PsiModifierList)parent; } - modifierList.setModifierProperty(PsiModifier.STATIC, false); final PsiElement modifierOwner = modifierList.getParent(); + if (!(modifierOwner instanceof PsiMethod && PsiUtil.isLanguageLevel8OrHigher(modifierList))) { + modifierList.setModifierProperty(PsiModifier.STATIC, false); + } assert modifierOwner != null; if (modifierOwner instanceof PsiClass) { final PsiClass aClass = (PsiClass)modifierOwner; |