Merge remote-tracking branch 'origin/master'

This commit is contained in:
Dmitry Trofimov
2014-02-05 19:38:12 +04:00
4 changed files with 109 additions and 78 deletions
@@ -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<String> annotations1 = new HashSet();
for (PsiAnnotation annotation : list1.getAnnotations()) {
annotations1.add(annotation.getQualifiedName());
}
final Set<String> annotations2 = new HashSet();
for (PsiAnnotation annotation : list2.getAnnotations()) {
annotations2.add(annotation.getQualifiedName());
}
final Set<String> 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 <T> Set<T> disjunction(Collection<T> set1, Collection<T> set2) {
final Set<T> 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;
}
}
}
@@ -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;
@@ -0,0 +1,31 @@
package redundant_method_override;
public class RedundantMethodOverride extends S {
@Override
void <warning descr="Method 'foo()' is identical to its super method">foo</warning>() {
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();
}
}
@@ -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(); }
}