Search in sources :

Example 96 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RefactoringSearchEngine2 method searchReferencedMethods.

/**
 * Performs the search of referenced methods.
 *
 * @param element the java element whose referenced methods have to be found
 * @param monitor the progress monitor, or <code>null</code>
 * @throws JavaModelException if an error occurs during search
 */
public final void searchReferencedMethods(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(element);
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        // $NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_methods);
        try {
            SearchEngine engine = null;
            if (fOwner != null) {
                engine = new SearchEngine(fOwner);
            } else {
                engine = new SearchEngine(fWorkingCopies);
            }
            engine.searchDeclarationsOfSentMessages(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 97 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.

the class RefactoringSearchEngine2 method searchReferencedFields.

/**
 * Performs the search of referenced fields.
 *
 * @param element the java element whose referenced fields have to be found
 * @param monitor the progress monitor, or <code>null</code>
 * @throws JavaModelException if an error occurs during search
 */
public final void searchReferencedFields(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(element);
    if (monitor == null) {
        monitor = new NullProgressMonitor();
    }
    try {
        // $NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_fields);
        try {
            SearchEngine engine = null;
            if (fOwner != null) {
                engine = new SearchEngine(fOwner);
            } else {
                engine = new SearchEngine(fWorkingCopies);
            }
            engine.searchDeclarationsOfAccessedFields(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 98 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine 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)

Example 99 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project lsp4jakarta by eclipse.

the class TypeHierarchyUtils method hasKnownDeclaration.

private static boolean hasKnownDeclaration(IType type) throws CoreException {
    String typeName = type.getElementName();
    final AtomicInteger references = new AtomicInteger(0);
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(typeName, IJavaSearchConstants.CLASS, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(type.getJavaProject()), new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object o = match.getElement();
            if (o instanceof IType) {
                IType t = (IType) o;
                if (t.getElementName().equals(typeName)) {
                    references.incrementAndGet();
                }
            }
        }
    }, null);
    if (references.get() > 0) {
        return true;
    }
    return false;
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IType(org.eclipse.jdt.core.IType)

Example 100 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ui by eclipse-jdt.

the class RefactoringSearchEngine2 method searchReferencedTypes.

/**
 * Performs the search of referenced types.
 *
 * @param element the java element whose referenced types have to be found
 * @param monitor the progress monitor, or <code>null</code>
 * @throws JavaModelException if an error occurs during search
 */
public void searchReferencedTypes(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(element);
    if (monitor == null)
        monitor = new NullProgressMonitor();
    try {
        // $NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_types);
        try {
            SearchEngine engine = null;
            if (fOwner != null)
                engine = new SearchEngine(fOwner);
            else
                engine = new SearchEngine(fWorkingCopies);
            engine.searchDeclarationsOfReferencedTypes(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)131 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)73 CoreException (org.eclipse.core.runtime.CoreException)71 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)61 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)61 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)58 ArrayList (java.util.ArrayList)52 JavaModelException (org.eclipse.jdt.core.JavaModelException)48 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)38 IType (org.eclipse.jdt.core.IType)38 IJavaElement (org.eclipse.jdt.core.IJavaElement)37 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)24 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)24 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)21 HashSet (java.util.HashSet)20 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IMethod (org.eclipse.jdt.core.IMethod)20 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)16 IJavaProject (org.eclipse.jdt.core.IJavaProject)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)12