From 7d228d5b3267994b447d673ef2ba9ea0a4bf6e03 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 5 Feb 2014 15:52:22 +0100 Subject: [PATCH] warn when only difference is @Override annotation --- .../RedundantMethodOverrideInspection.java | 57 +++++++++++++--- .../siyeh/ig/psiutils/EquivalenceChecker.java | 68 +------------------ .../RedundantMethodOverride.java | 31 +++++++++ ...RedundantMethodOverrideInspectionTest.java | 31 +++++++++ 4 files changed, 109 insertions(+), 78 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/redundant_method_override/RedundantMethodOverride.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/RedundantMethodOverrideInspectionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/RedundantMethodOverrideInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/RedundantMethodOverrideInspection.java index 46ef38b26697..9694d347aff8 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/RedundantMethodOverrideInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/inheritance/RedundantMethodOverrideInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2007 Bas Leijdekkers + * Copyright 2005-2014 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,8 @@ import com.siyeh.ig.psiutils.EquivalenceChecker; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.*; + public class RedundantMethodOverrideInspection extends BaseInspection { @Override @@ -107,19 +109,11 @@ public class RedundantMethodOverrideInspection extends BaseInspection { if (superBody == null) { return; } - final PsiModifierList superModifierList = - superMethod.getModifierList(); - final PsiModifierList modifierList = method.getModifierList(); - if (!EquivalenceChecker.modifierListsAreEquivalent( - modifierList, superModifierList)) { + if (!modifierListsAreEquivalent(method.getModifierList(), superMethod.getModifierList())) { return; } final PsiType superReturnType = superMethod.getReturnType(); - if (superReturnType == null) { - return; - } - final PsiType returnType = method.getReturnType(); - if (!superReturnType.equals(returnType)) { + if (superReturnType == null || !superReturnType.equals(method.getReturnType())) { return; } if (!EquivalenceChecker.codeBlocksAreEquivalent(body, superBody)) { @@ -127,5 +121,46 @@ public class RedundantMethodOverrideInspection extends BaseInspection { } registerMethodError(method); } + + private static boolean modifierListsAreEquivalent(@Nullable PsiModifierList list1, @Nullable PsiModifierList list2) { + if (list1 == null) { + return list2 == null; + } + else if (list2 == null) { + return false; + } + final Set annotations1 = new HashSet(); + for (PsiAnnotation annotation : list1.getAnnotations()) { + annotations1.add(annotation.getQualifiedName()); + } + final Set annotations2 = new HashSet(); + for (PsiAnnotation annotation : list2.getAnnotations()) { + annotations2.add(annotation.getQualifiedName()); + } + final Set uniques = disjunction(annotations1, annotations2); + uniques.remove(CommonClassNames.JAVA_LANG_OVERRIDE); + if (!uniques.isEmpty()) { + return false; + } + return list1.hasModifierProperty(PsiModifier.STRICTFP) == list2.hasModifierProperty(PsiModifier.STRICTFP) && + list1.hasModifierProperty(PsiModifier.SYNCHRONIZED) == list2.hasModifierProperty(PsiModifier.SYNCHRONIZED) && + list1.hasModifierProperty(PsiModifier.PUBLIC) == list2.hasModifierProperty(PsiModifier.PUBLIC) && + list1.hasModifierProperty(PsiModifier.PROTECTED) == list2.hasModifierProperty(PsiModifier.PROTECTED); + } + + private static Set disjunction(Collection set1, Collection set2) { + final Set result = new HashSet(); + for (T t : set1) { + if (!set2.contains(t)) { + result.add(t); + } + } + for (T t : set2) { + if (!set1.contains(t)) { + result.add(t); + } + } + return result; + } } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/EquivalenceChecker.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/EquivalenceChecker.java index 9ece6a3e212e..e6f88ab42457 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/EquivalenceChecker.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/EquivalenceChecker.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 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. @@ -27,72 +27,6 @@ public class EquivalenceChecker { private EquivalenceChecker() {} - public static boolean modifierListsAreEquivalent( - @Nullable PsiModifierList list1, @Nullable PsiModifierList list2) { - if (list1 == null) { - return list2 == null; - } - else if (list2 == null) { - return false; - } - final PsiAnnotation[] annotations = list1.getAnnotations(); - for (PsiAnnotation annotation : annotations) { - final String qualifiedName = annotation.getQualifiedName(); - if (qualifiedName == null) { - return false; - } - if (list2.findAnnotation(qualifiedName) == null) { - return false; - } - } - if (list1.hasModifierProperty(PsiModifier.ABSTRACT) && - !list2.hasModifierProperty(PsiModifier.ABSTRACT)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.FINAL) && - !list2.hasModifierProperty(PsiModifier.FINAL)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.NATIVE) && - !list2.hasModifierProperty(PsiModifier.NATIVE)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && - !list2.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.PRIVATE) && - !list2.hasModifierProperty(PsiModifier.PRIVATE)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.PROTECTED) && - !list2.hasModifierProperty(PsiModifier.PROTECTED)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.PUBLIC) && - !list2.hasModifierProperty(PsiModifier.PUBLIC)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.STATIC) && - !list2.hasModifierProperty(PsiModifier.STATIC)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.STRICTFP) && - !list2.hasModifierProperty(PsiModifier.STRICTFP)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.SYNCHRONIZED) && - !list2.hasModifierProperty(PsiModifier.SYNCHRONIZED)) { - return false; - } - if (list1.hasModifierProperty(PsiModifier.TRANSIENT) && - !list2.hasModifierProperty(PsiModifier.TRANSIENT)) { - return false; - } - return !(list1.hasModifierProperty(PsiModifier.VOLATILE) && - !list2.hasModifierProperty(PsiModifier.VOLATILE)); - } - public static boolean statementsAreEquivalent(@Nullable PsiStatement statement1, @Nullable PsiStatement statement2) { if (statement1 == null) { return statement2 == null; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/redundant_method_override/RedundantMethodOverride.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/redundant_method_override/RedundantMethodOverride.java new file mode 100644 index 000000000000..797cab3c9f13 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/inheritance/redundant_method_override/RedundantMethodOverride.java @@ -0,0 +1,31 @@ +package redundant_method_override; + +public class RedundantMethodOverride extends S { + + @Override + void foo() { + System.out.println(); + } + + void bar() { + System.out.println(); + } + + public void m() { + System.out.println(); + } +} +class S { + + void foo() { + System.out.println(); + } + + synchronized void bar() { + System.out.println(); + } + + void m() { + System.out.println(); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/RedundantMethodOverrideInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/RedundantMethodOverrideInspectionTest.java new file mode 100644 index 000000000000..47b1d9f63560 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/inheritance/RedundantMethodOverrideInspectionTest.java @@ -0,0 +1,31 @@ +/* + * 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.inheritance; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; + +/** + * @author Bas Leijdekkers + */ +public class RedundantMethodOverrideInspectionTest extends LightInspectionTestCase { + @Override + protected InspectionProfileEntry getInspection() { + return new RedundantMethodOverrideInspection(); + } + + public void testRedundantMethodOverride() { doTest(); } +}