diff --git a/java/java-usages/openidea-java-usages.jar b/java/java-usages/openidea-java-usages.jar index bdafdf667..e8df2feb3 100644 Binary files a/java/java-usages/openidea-java-usages.jar and b/java/java-usages/openidea-java-usages.jar differ diff --git a/java/java-usages/src/ai/openidea/implementations/ImplementationsCommand.java b/java/java-usages/src/ai/openidea/implementations/ImplementationsCommand.java index 50bd541af..e34218d2d 100644 --- a/java/java-usages/src/ai/openidea/implementations/ImplementationsCommand.java +++ b/java/java-usages/src/ai/openidea/implementations/ImplementationsCommand.java @@ -2,8 +2,10 @@ package ai.openidea.implementations; import ai.openidea.Documents; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; @@ -76,6 +78,7 @@ public final class ImplementationsCommand implements IDelegateCommandHandler { CreateTypeHierarchyOperation operation = new CreateTypeHierarchyOperation(type, workingCopies, scope, true); operation.runOperation(monitor); ITypeHierarchy hierarchy = operation.getResult(); + Set seen = new HashSet<>(); for (IType subtype : hierarchy.getAllSubtypes(type)) { if (monitor.isCanceled()) throw new OperationCanceledException(); IMember found; @@ -84,14 +87,30 @@ public final class ImplementationsCommand implements IDelegateCommandHandler { } else { // Seen from the subtype, so its type arguments substitute the method's type variables. IMethod overriding = new MethodOverrideTester(subtype, hierarchy).findOverridingMethodInType(subtype, method); + if (overriding == null && !subtype.isInterface()) overriding = inherited(subtype, method, monitor); found = overriding == null || Flags.isAbstract(overriding.getFlags()) ? null : overriding; } - Location location = found == null ? null : JDTUtils.toLocation(found); + Location location = found == null || !seen.add(found) ? null : JDTUtils.toLocation(found); if (location != null) result.add(location); } return !type.isBinary(); } + /** + * The method of a superclass of {@code type} that implements {@code method} for it: a class + * that implements an interface with a method it inherits from a class that doesn't. + */ + private static IMethod inherited(IType type, IMethod method, IProgressMonitor monitor) throws JavaModelException { + ITypeHierarchy supertypes = type.newSupertypeHierarchy(monitor); + MethodOverrideTester tester = new MethodOverrideTester(type, supertypes); + for (IType s = supertypes.getSuperclass(type); s != null; s = supertypes.getSuperclass(s)) { + if (s.isInterface()) break; + IMethod found = tester.findOverridingMethodInType(s, method); + if (found != null) return found; + } + return null; + } + /** The project's compiled Kotlin classes, attached as a library by the Kotlin bridge. */ private static boolean isKotlinOutput(IPackageFragmentRoot root) throws JavaModelException { if (root.getKind() != IPackageFragmentRoot.K_BINARY) return false; diff --git a/java/java-usages/tests/implementations.py b/java/java-usages/tests/implementations.py index 425ad2b65..bede114d4 100644 --- a/java/java-usages/tests/implementations.py +++ b/java/java-usages/tests/implementations.py @@ -86,6 +86,29 @@ public class Words implements SubRepo { return "words"; } } +''', + 'Handler.java': '''package zoo; + +public interface Handler { + void handle(); +} +''', + 'BaseHandler.java': '''package zoo; + +public class BaseHandler { + public void handle() { + } +} +''', + 'HandlerImpl.java': '''package zoo; + +public class HandlerImpl extends BaseHandler implements Handler { +} +''', + 'OtherHandler.java': '''package zoo; + +public class OtherHandler extends BaseHandler implements Handler { +} ''', 'Task.java': '''package zoo; @@ -246,6 +269,8 @@ try: ('Repo.java', 'save(T'): (['Anon.java:5', 'Anon.java:9', 'Longs.java:0', 'Numbers.java:3', 'Words.java:3'], True), ('Use.java', 'save("x")'): (['Anon.java:5', 'Anon.java:9', 'Longs.java:0', 'Numbers.java:3', 'Words.java:3'], True), ('Repo.java', 'name()'): (['Words.java:6'], True), + # Implemented by a superclass of the implementing classes, listed once. + ('Handler.java', 'handle()'): (['BaseHandler.java:3'], True), # A library type's implementations in the sources; the library has more. ('Use.java', 'Runnable task'): (['Task.java:2'], False), ('Use.java', 'run();'): (['Task.java:3'], False),