Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary calls to findOverriddenMethod #1922 #1927

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading