Search in sources :

Example 11 with SearchParticipant

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;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) Expression(org.eclipse.jdt.core.dom.Expression)

Aggregations

SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)11 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)9 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)7 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)7 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)6 ArrayList (java.util.ArrayList)5 IMethod (org.eclipse.jdt.core.IMethod)5 IType (org.eclipse.jdt.core.IType)5 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)5 HashSet (java.util.HashSet)4 CoreException (org.eclipse.core.runtime.CoreException)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IFile (org.eclipse.core.resources.IFile)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 IPath (org.eclipse.core.runtime.IPath)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Path (org.eclipse.core.runtime.Path)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2