diff options
Diffstat (limited to 'plugins/InspectionGadgets')
26 files changed, 640 insertions, 533 deletions
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index 890da15b546b..164c690d4da3 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -408,6 +408,10 @@ key="class.name.differs.from.file.name.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.class.structure" enabledByDefault="false" level="WARNING" implementationClass="com.siyeh.ig.classlayout.ClassNameDiffersFromFileNameInspection"/> + <localInspection language="JAVA" shortName="ClassWithOnlyPrivateConstructors" bundle="com.siyeh.InspectionGadgetsBundle" + key="class.with.only.private.constructors.display.name" groupBundle="messages.InspectionsBundle" + groupKey="group.names.class.structure" enabledByDefault="false" level="WARNING" + implementationClass="com.siyeh.ig.classlayout.ClassWithOnlyPrivateConstructorsInspection"/> <localInspection language="JAVA" shortName="ConstantDeclaredInAbstractClass" bundle="com.siyeh.InspectionGadgetsBundle" key="constant.declared.in.abstract.class.display.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.class.structure" enabledByDefault="false" level="WARNING" 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; diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspection.java new file mode 100644 index 000000000000..3aeebcd724e8 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspection.java @@ -0,0 +1,33 @@ +/* + * 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.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.MakeClassFinalFix; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class ClassWithOnlyPrivateConstructorsInspection extends ClassWithOnlyPrivateConstructorsInspectionBase { + + @Nullable + @Override + protected InspectionGadgetsFix buildFix(Object... infos) { + return new MakeClassFinalFix((PsiClass)infos[0]); + } +} diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/InlineCallFix.java b/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/InlineCallFix.java index 43e57741bbe3..b9503e220c68 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/InlineCallFix.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/fixes/InlineCallFix.java @@ -16,52 +16,54 @@ package com.siyeh.ig.fixes; import com.intellij.codeInspection.ProblemDescriptor; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiMethodCallExpression; import com.intellij.psi.PsiReferenceExpression; -import com.intellij.refactoring.JavaRefactoringActionHandlerFactory; -import com.intellij.refactoring.RefactoringActionHandler; +import com.intellij.refactoring.JavaRefactoringSettings; +import com.intellij.refactoring.inline.InlineMethodProcessor; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.InspectionGadgetsFix; import org.jetbrains.annotations.NotNull; public class InlineCallFix extends InspectionGadgetsFix { - @Override - @NotNull - public String getFamilyName() { - return getName(); - } + private String myName; + + public InlineCallFix(String name) { + myName = name; + } + + public InlineCallFix() { + this(InspectionGadgetsBundle.message("inline.call.quickfix")); + } + + @Override + @NotNull + public String getFamilyName() { + return myName; + } @Override @NotNull public String getName() { - return InspectionGadgetsBundle.message("inline.call.quickfix"); + return getFamilyName(); } @Override public void doFix(final Project project, ProblemDescriptor descriptor) { final PsiElement nameElement = descriptor.getPsiElement(); - final PsiReferenceExpression methodExpression = - (PsiReferenceExpression)nameElement.getParent(); - assert methodExpression != null; - final PsiMethodCallExpression methodCallExpression = - (PsiMethodCallExpression)methodExpression.getParent(); - final JavaRefactoringActionHandlerFactory factory = - JavaRefactoringActionHandlerFactory.getInstance(); - final RefactoringActionHandler inlineHandler = factory.createInlineHandler(); - final Runnable runnable = new Runnable() { - @Override - public void run() { - inlineHandler.invoke(project, new PsiElement[]{methodCallExpression}, null); - } - }; - if (ApplicationManager.getApplication().isUnitTestMode()) { - runnable.run(); - } - else { - ApplicationManager.getApplication().invokeLater(runnable, project.getDisposed()); - } + final PsiReferenceExpression methodExpression = (PsiReferenceExpression)nameElement.getParent(); + if (methodExpression == null) return; + final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)methodExpression.getParent(); + final PsiMethod method = methodCallExpression.resolveMethod(); + if (method == null) return; + inline(project, methodExpression, method); + } + + protected void inline(Project project, PsiReferenceExpression methodExpression, PsiMethod method) { + new InlineMethodProcessor(project, method, methodExpression, null, true, + JavaRefactoringSettings.getInstance().RENAME_SEARCH_IN_COMMENTS_FOR_METHOD, + JavaRefactoringSettings.getInstance().RENAME_SEARCH_FOR_TEXT_FOR_METHOD).inlineMethodCall(methodExpression); } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspection.java new file mode 100644 index 000000000000..c550093cef63 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/CallToSimpleGetterInClassInspection.java @@ -0,0 +1,63 @@ +/* + * 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.ui.MultipleCheckboxOptionsPanel; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiExpressionStatement; +import com.intellij.psi.PsiMethod; +import com.intellij.psi.PsiReferenceExpression; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.InlineCallFix; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class CallToSimpleGetterInClassInspection extends CallToSimpleGetterInClassInspectionBase { + @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 InlineOrDeleteCallFix(InspectionGadgetsBundle.message("call.to.simple.getter.in.class.inline.quickfix")); + } + + private static class InlineOrDeleteCallFix extends InlineCallFix { + public InlineOrDeleteCallFix(String name) { + super(name); + } + + @Override + protected void inline(Project project, PsiReferenceExpression methodExpression, PsiMethod method) { + final PsiElement statement = methodExpression.getParent().getParent(); + if (statement instanceof PsiExpressionStatement) { + statement.delete(); + } else { + super.inline(project, methodExpression, method); + } + } + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspection.java new file mode 100644 index 000000000000..652a8e89ea98 --- /dev/null +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/performance/CallToSimpleSetterInClassInspection.java @@ -0,0 +1,43 @@ +/* + * 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.ui.MultipleCheckboxOptionsPanel; +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.InspectionGadgetsFix; +import com.siyeh.ig.fixes.InlineCallFix; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; + +public class CallToSimpleSetterInClassInspection extends CallToSimpleSetterInClassInspectionBase { + + @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(InspectionGadgetsBundle.message("call.to.simple.setter.in.class.inline.quickfix")); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithOnlyPrivateConstructors.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithOnlyPrivateConstructors.html new file mode 100644 index 000000000000..87122815ae53 --- /dev/null +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithOnlyPrivateConstructors.html @@ -0,0 +1,9 @@ +<html> +<body> +Reports classes with only <b>private</b> constructors that are not extended by any nested class. +Such classes can not be extended and should be declared <b>final</b>. +<!-- tooltip end --> +<p> +<small>New in 14</small> +</body> +</html>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutConstructor.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutConstructor.html index 24229c1d262b..184a994f90cd 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutConstructor.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutConstructor.html @@ -1,6 +1,6 @@ <html> <body> -Reports a classes without constructors. Some coding standards prohibit such classes. +Reports classes without constructors. Some coding standards prohibit such classes. <!-- tooltip end --> <p> diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutNoArgConstructor.html b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutNoArgConstructor.html index fbc102e8154c..0ce4bbb23575 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutNoArgConstructor.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/ClassWithoutNoArgConstructor.html @@ -1,6 +1,6 @@ <html> <body> -Reports a classes without a no-argument constructor. +Reports classes without a no-argument constructor. Such constructors are necessary in some contexts if a class is to be created reflexively. <!-- tooltip end --> <p> 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 377e50fa8a21..666651d71e21 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 @@ -7,20 +7,20 @@ import java.util.Set; public class IfStatementWithIdenticalBranches { void one() { - if (true) { + <warning descr="'if' statement with identical branches">if</warning> (true) { } else { } - if (false) { + <warning descr="'if' statement with identical branches">if</warning> (false) { System.out.println(); return; } System.out.println(); } - int two() { - if (true) { + int two() { + <warning descr="'if' statement with identical branches">if</warning> (true) { int i = 2; return i; } else { @@ -41,7 +41,7 @@ public class IfStatementWithIdenticalBranches { void four() { if (true) { - if (false) { + <warning descr="'if' statement with identical branches">if</warning> (false) { System.out.println(); return; } @@ -61,7 +61,7 @@ public class IfStatementWithIdenticalBranches { void six() { if (true) { - if (false) { + <warning descr="'if' statement with identical branches">if</warning> (false) { System.out.println(); System.out.println(); return; @@ -93,11 +93,11 @@ public class IfStatementWithIdenticalBranches { } void nine() { - if (true) { + <warning descr="'if' statement with identical branches">if</warning> (true) { - } else if (true) { + } else <warning descr="'if' statement with identical branches">if</warning> (true) { - } else if (true) { + } else <warning descr="'if' statement with identical branches">if</warning> (true) { } else { @@ -105,7 +105,7 @@ public class IfStatementWithIdenticalBranches { } void blocks() { - if (true) { + <warning descr="'if' statement with identical branches">if</warning> (true) { System.out.println(); return; } @@ -135,4 +135,31 @@ class NotADup { return null; } + public static String calculate(int someNumber) { + if (someNumber == 0 ) { + try { + return placeOrder(3, null); + } + catch( Exception e ) { + System.out.println("e = " + e); + } + } + else if (someNumber == 1) { + try { + return placeOrder(3, someNumber, null); + } + catch(Exception e ) { + System.out.println("e = " + e); + } + } + return null; + } + + private static String placeOrder(int i, int someNumber, Object o) { + return null; + } + + private static String placeOrder(int i, Object o) { + return null; + } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/expected.xml deleted file mode 100644 index c85bba14ea3b..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/expected.xml +++ /dev/null @@ -1,65 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<problems> - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>10</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>15</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>23</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>44</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>64</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>96</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>98</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>100</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> - - <problem> - <file>IfStatementWithIdenticalBranches.java</file> - <line>108</line> - <problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">'if' statement with identical branches</problem_class> - <description><code>if</code> statement with identical branches #loc</description> - </problem> -</problems>
\ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java index 019805adcf17..4ba7a97033ad 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java @@ -124,4 +124,5 @@ class Expanded {{ long g = 8L / 8L; long h = 9L * 0L; int a = 8 * 0 * 8 * ; // don't warn + int minus = 2 - 1 - 1; }}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspectionTest.java new file mode 100644 index 000000000000..a177242537d2 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/classlayout/ClassWithOnlyPrivateConstructorsInspectionTest.java @@ -0,0 +1,49 @@ +/* + * 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.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +public class ClassWithOnlyPrivateConstructorsInspectionTest extends LightInspectionTestCase { + + public void testSimple() { + doTest("class /*Class 'X' with only 'private' constructors should be declared 'final'*/X/**/ {" + + " private X() {}" + + " private X(int i) {}" + + "}"); + } + + public void testExtendingInnerClass() { + doTest("class X {\n" + + " private X() {}\n" + + " class Y {\n" + + " class Z extends X{}\n" + + " }\n" + + "}"); + } + + public void testNoConstructors() { + doTest("class X {}"); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new ClassWithOnlyPrivateConstructorsInspection(); + } +}
\ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspectionTest.java index 1562b8c9053c..e3e2f9c8a857 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspectionTest.java @@ -1,12 +1,18 @@ package com.siyeh.ig.controlflow; -import com.siyeh.ig.IGInspectionTestCase; +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; -public class IfStatementWithIdenticalBranchesInspectionTest - extends IGInspectionTestCase { +public class IfStatementWithIdenticalBranchesInspectionTest extends LightInspectionTestCase { - public void test() throws Exception { - doTest("com/siyeh/igtest/controlflow/if_statement_with_identical_branches", - new IfStatementWithIdenticalBranchesInspection()); + public void testIfStatementWithIdenticalBranches() throws Exception { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new IfStatementWithIdenticalBranchesInspection(); } }
\ No newline at end of file |