mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-122661 "Method overloads method of superclass" inspection missing default methods in Java 8 interfaces
This commit is contained in:
+30
-10
@@ -17,6 +17,7 @@ package com.siyeh.ig.visibility;
|
||||
|
||||
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
@@ -85,18 +86,36 @@ public class MethodOverloadsParentMethodInspectionBase extends BaseInspection {
|
||||
if (MethodUtils.hasSuper(method)) {
|
||||
return;
|
||||
}
|
||||
PsiClass ancestorClass = aClass.getSuperClass();
|
||||
final Set<PsiClass> visitedClasses = new HashSet<PsiClass>();
|
||||
while (ancestorClass != null) {
|
||||
if (!visitedClasses.add(ancestorClass)) {
|
||||
return;
|
||||
}
|
||||
if (methodOverloads(method, ancestorClass)) {
|
||||
registerMethodError(method);
|
||||
return;
|
||||
}
|
||||
ancestorClass = ancestorClass.getSuperClass();
|
||||
processSupers(method, aClass, aClass, visitedClasses);
|
||||
}
|
||||
|
||||
private boolean processSupers(final PsiMethod method,
|
||||
final PsiClass psiClass,
|
||||
final PsiClass initialClass,
|
||||
final Set<PsiClass> visitedClasses) {
|
||||
if (!visitedClasses.add(psiClass)) {
|
||||
return false;
|
||||
}
|
||||
if (initialClass != psiClass && methodOverloads(method, psiClass)) {
|
||||
registerMethodError(method);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
if (PsiUtil.isLanguageLevel8OrHigher(method)) {
|
||||
for (PsiClass superClass : psiClass.getSupers()) {
|
||||
if (processSupers(method, superClass, initialClass, visitedClasses)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
final PsiClass superClass = psiClass.getSuperClass();
|
||||
if (superClass != null) {
|
||||
return processSupers(method, superClass, initialClass, visitedClasses);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean methodOverloads(PsiMethod method, PsiClass ancestorClass) {
|
||||
@@ -107,6 +126,7 @@ public class MethodOverloadsParentMethodInspectionBase extends BaseInspection {
|
||||
for (final PsiMethod testMethod : methods) {
|
||||
if (!testMethod.hasModifierProperty(PsiModifier.PRIVATE) &&
|
||||
!testMethod.hasModifierProperty(PsiModifier.STATIC) &&
|
||||
!testMethod.hasModifierProperty(PsiModifier.ABSTRACT) &&
|
||||
!isOverriddenInClass(testMethod, method.getContainingClass())) {
|
||||
final PsiParameterList testParameterList = testMethod.getParameterList();
|
||||
final PsiParameter[] testParameters = testParameterList.getParameters();
|
||||
|
||||
+21
@@ -24,4 +24,25 @@ class Parent {
|
||||
Object bla(double d) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
class DefaultMethod {
|
||||
public static interface IFoo {
|
||||
default void foo(int i) {
|
||||
System.out.println("IFoo.foo(int): " + i);
|
||||
}
|
||||
|
||||
void bar(int i);
|
||||
|
||||
}
|
||||
|
||||
public static class Foo implements IFoo {
|
||||
public void foo(long l) {
|
||||
System.out.println("Foo.foo(long): " + l);
|
||||
}
|
||||
|
||||
public void bar(long l) {
|
||||
System.out.println("Foo.bar(long): " + l);
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
@@ -21,4 +21,11 @@
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Method overloads method of superclass</problem_class>
|
||||
<description>Method <code>bla()</code> overloads a compatible method of a superclass, when overriding might have been intended #loc</description>
|
||||
</problem>
|
||||
|
||||
<problem>
|
||||
<file>MethodOverloadsParentMethod.java</file>
|
||||
<line>40</line>
|
||||
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Method overloads method of superclass</problem_class>
|
||||
<description>Method <code>foo()</code> overloads a compatible method of a superclass, when overriding might have been intended #loc</description>
|
||||
</problem>
|
||||
</problems>
|
||||
+12
@@ -1,5 +1,9 @@
|
||||
package com.siyeh.ig.visibility;
|
||||
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.testFramework.IdeaTestUtil;
|
||||
import com.siyeh.ig.IGInspectionTestCase;
|
||||
|
||||
public class MethodOverloadsParentMethodInspectionTest extends IGInspectionTestCase {
|
||||
@@ -7,4 +11,12 @@ public class MethodOverloadsParentMethodInspectionTest extends IGInspectionTestC
|
||||
public void test() throws Exception {
|
||||
doTest("com/siyeh/igtest/visibility/method_overloads_parent_method", new MethodOverloadsParentMethodInspection());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Sdk getTestProjectSdk() {
|
||||
final Sdk sdk = IdeaTestUtil.getMockJdk17();
|
||||
LanguageLevelProjectExtension.getInstance(getProject()).setLanguageLevel(LanguageLevel.HIGHEST);
|
||||
return sdk;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user