Skip to content

Commit

Permalink
Avoid unnecessary calls to findOverriddenMethod #1922
Browse files Browse the repository at this point in the history
This speeds up the quick outline.

Fixes #1922
  • Loading branch information
fedejeanne committed Jan 10, 2025
1 parent 6f377b8 commit 2ee7c64
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,20 @@ public IMethod findOverriddenMethod(IMethod overriding, boolean testVisibility)
return null;
}

Set<String> visited = new HashSet<>();

IType type= overriding.getDeclaringType();
IType superClass= fHierarchy.getSuperclass(type);
if (superClass != null) {
IMethod res= findOverriddenMethodInHierarchy(superClass, overriding);
IMethod res= findOverriddenMethodInHierarchy(superClass, overriding, visited);
if (res != null) {
if (!testVisibility || JavaModelUtil.isVisibleInHierarchy(res, type.getPackageFragment())) {
return res;
}
}
}
for (IType intf : fHierarchy.getSuperInterfaces(type)) {
IMethod res= findOverriddenMethodInHierarchy(intf, overriding);
IMethod res= findOverriddenMethodInHierarchy(intf, overriding, visited);
if (res != null) {
return res; // methods from interfaces are always public and therefore visible
}
Expand All @@ -160,23 +162,29 @@ public IMethod findOverriddenMethod(IMethod overriding, boolean testVisibility)
* With generics it is possible that 2 methods in the same type are overidden at the same time. In that case, the first overridden method found is returned.
* @param type The type to find methods in
* @param overriding The overriding method
* @param visited The types that were visited already
* @return The first overridden method or <code>null</code> if no method is overridden
* @throws JavaModelException if a problem occurs
*/
public IMethod findOverriddenMethodInHierarchy(IType type, IMethod overriding) throws JavaModelException {
public IMethod findOverriddenMethodInHierarchy(IType type, IMethod overriding, Set<String> visited) throws JavaModelException {

if (!visited.add(type.getElementName())) {
return null;
}

IMethod method= findOverriddenMethodInType(type, overriding);
if (method != null) {
return method;
}
IType superClass= fHierarchy.getSuperclass(type);
if (superClass != null) {
IMethod res= findOverriddenMethodInHierarchy(superClass, overriding);
IMethod res= findOverriddenMethodInHierarchy(superClass, overriding, visited);
if (res != null) {
return res;
}
}
for (IType superInterface : fHierarchy.getSuperInterfaces(type)) {
IMethod res= findOverriddenMethodInHierarchy(superInterface, overriding);
IMethod res= findOverriddenMethodInHierarchy(superInterface, overriding, visited);
if (res != null) {
return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -540,7 +541,7 @@ public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreEx

IType actualTargetType= (IType) fIntermediaryFirstParameterType.getJavaElement();
if (!fTargetMethod.getDeclaringType().equals(actualTargetType)) {
IMethod actualTargetMethod= new MethodOverrideTester(actualTargetType, actualTargetType.newSupertypeHierarchy(null)).findOverriddenMethodInHierarchy(actualTargetType, fTargetMethod);
IMethod actualTargetMethod= new MethodOverrideTester(actualTargetType, actualTargetType.newSupertypeHierarchy(null)).findOverriddenMethodInHierarchy(actualTargetType, fTargetMethod, new HashSet<>());
fTargetMethod= actualTargetMethod;
fTargetMethodBinding= findMethodBindingInHierarchy(fIntermediaryFirstParameterType, actualTargetMethod);
Assert.isNotNull(fTargetMethodBinding);
Expand Down

0 comments on commit 2ee7c64

Please sign in to comment.