use of org.eclipse.jdt.core.search.SearchParticipant in project eclipse.jdt.ls by eclipse.
the class ImplementationCollector method findMethodImplementations.
private List<T> findMethodImplementations(IProgressMonitor monitor) throws CoreException {
IMethod method = (IMethod) javaElement;
try {
if (cannotBeOverriddenMethod(method)) {
return null;
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Find method implementations failure ", e);
return null;
}
CompilationUnit ast = CoreASTProvider.getInstance().getAST(typeRoot, CoreASTProvider.WAIT_YES, monitor);
if (ast == null) {
return null;
}
ASTNode node = NodeFinder.perform(ast, region.getOffset(), region.getLength());
ITypeBinding parentTypeBinding = null;
if (node instanceof SimpleName) {
ASTNode parent = node.getParent();
if (parent instanceof MethodInvocation) {
Expression expression = ((MethodInvocation) parent).getExpression();
if (expression == null) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
} else {
parentTypeBinding = expression.resolveTypeBinding();
}
} else if (parent instanceof SuperMethodInvocation) {
// Directly go to the super method definition
return Collections.singletonList(mapper.convert(method, 0, 0));
} else if (parent instanceof MethodDeclaration) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
}
}
final IType receiverType = getType(parentTypeBinding);
if (receiverType == null) {
return null;
}
final List<T> results = new ArrayList<>();
try {
String methodLabel = JavaElementLabelsCore.getElementLabel(method, JavaElementLabelsCore.DEFAULT_QUALIFIED);
monitor.beginTask(Messages.format(JavaElementImplementationHyperlink_search_method_implementors, methodLabel), 10);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
Object element = match.getElement();
if (element instanceof IMethod) {
IMethod methodFound = (IMethod) element;
if (!JdtFlags.isAbstract(methodFound)) {
T result = mapper.convert(methodFound, match.getOffset(), match.getLength());
if (result != null) {
results.add(result);
}
}
}
}
}
};
IJavaSearchScope hierarchyScope;
if (receiverType.isInterface()) {
hierarchyScope = SearchEngine.createHierarchyScope(method.getDeclaringType());
} else {
if (isFullHierarchyNeeded(new SubProgressMonitor(monitor, 3), method, receiverType)) {
hierarchyScope = SearchEngine.createHierarchyScope(receiverType);
} else {
boolean isMethodAbstract = JdtFlags.isAbstract(method);
hierarchyScope = SearchEngine.createStrictHierarchyScope(null, receiverType, true, isMethodAbstract, null);
}
}
int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
SearchPattern pattern = SearchPattern.createPattern(method, limitTo);
Assert.isNotNull(pattern);
SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
SearchEngine engine = new SearchEngine();
engine.search(pattern, participants, hierarchyScope, requestor, new SubProgressMonitor(monitor, 7));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
} finally {
monitor.done();
}
return results;
}
Aggregations