Search in sources :

Example 91 with SearchEngine

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

the class ObsoleteSimpleNameRatherThanQualifiedNameCleanUp method importTypesFromPackage.

private void importTypesFromPackage(final String pkgName, final ASTNode visited) {
    TypeNameMatchRequestor importTypeCollector = new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(final TypeNameMatch typeNameMatch) {
            boolean isTopLevelType = typeNameMatch.getType().getDeclaringType() == null;
            if (isTopLevelType) {
                if (!pkgName.equals(typeNameMatch.getPackageName())) {
                    // Sanity check failed
                    throw new IllegalStateException(// $NON-NLS-1$
                    "Expected package '" + typeNameMatch.getPackageName() + "' to be equal to '" + pkgName + // $NON-NLS-1$ //$NON-NLS-2$
                    "'");
                }
                QName qname = QName.valueOf(typeNameMatch.getFullyQualifiedName());
                types.addName(FQN.fromImport(qname, true));
            }
        }
    };
    try {
        SearchEngine searchEngine = new SearchEngine();
        // search in this package
        searchEngine.searchAllTypeNames(// search in this package
        pkgName.toCharArray(), // search in this package
        SearchPattern.R_EXACT_MATCH, // do not filter by type name
        null, // do not filter by type name
        SearchPattern.R_EXACT_MATCH, // look for all java types (class, interfaces, enums, etc.)
        IBinding.TYPE, // search everywhere
        SearchEngine.createWorkspaceScope(), // wait in case the indexer is indexing
        importTypeCollector, // wait in case the indexer is indexing
        IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, cuRewrite.getProgressMonitor());
    } catch (JavaModelException e) {
        throw new UnhandledException(visited, e);
    }
}
Also used : UnhandledException(org.autorefactor.util.UnhandledException) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor)

Example 92 with SearchEngine

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

the class CodeLensHandler method findReferences.

private List<Location> findReferences(IJavaElement element, IProgressMonitor monitor) throws JavaModelException, CoreException {
    if (element == null) {
        return Collections.emptyList();
    }
    SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES);
    final List<Location> result = new ArrayList<>();
    SearchEngine engine = new SearchEngine();
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            if (match.getAccuracy() == SearchMatch.A_INACCURATE) {
                return;
            }
            Object o = match.getElement();
            if (o instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) o;
                ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (compilationUnit == null) {
                    return;
                }
                Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength());
                result.add(location);
            }
        }
    }, monitor);
    return result;
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ArrayList(java.util.ArrayList) Location(org.eclipse.lsp4j.Location)

Example 93 with SearchEngine

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

the class ReferencesHandler method search.

private void search(IJavaElement elementToSearch, final List<Location> locations, IProgressMonitor monitor) throws CoreException, JavaModelException {
    boolean includeClassFiles = preferenceManager.isClientSupportsClassFileContent();
    boolean includeDecompiledSources = preferenceManager.getPreferences().isIncludeDecompiledSources();
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(elementToSearch, IJavaSearchConstants.REFERENCES);
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            if (match.getAccuracy() == SearchMatch.A_INACCURATE) {
                return;
            }
            Object o = match.getElement();
            if (o instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) o;
                ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (compilationUnit != null) {
                    Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength());
                    locations.add(location);
                } else if (includeClassFiles) {
                    IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
                    if (cf != null && cf.getSourceRange() != null) {
                        Location location = JDTUtils.toLocation(cf, match.getOffset(), match.getLength());
                        locations.add(location);
                    } else if (includeDecompiledSources && cf != null) {
                        List<Location> result = JDTUtils.searchDecompiledSources(element, cf, false, false, monitor);
                        locations.addAll(result);
                    }
                }
            }
        }
    }, monitor);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IClassFile(org.eclipse.jdt.core.IClassFile) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ArrayList(java.util.ArrayList) List(java.util.List) Location(org.eclipse.lsp4j.Location)

Example 94 with SearchEngine

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

the class ChangeSignatureProcessor method findOccurrences.

private SearchResultGroup[] findOccurrences(IProgressMonitor pm, ReferencesInBinaryContext binaryRefs, RefactoringStatus status) throws JavaModelException {
    final boolean isConstructor = fMethod.isConstructor();
    CuCollectingSearchRequestor requestor = new CuCollectingSearchRequestor(binaryRefs) {

        @Override
        protected void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
            // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=27236 :
            if (isConstructor && match instanceof MethodReferenceMatch) {
                MethodReferenceMatch mrm = (MethodReferenceMatch) match;
                if (mrm.isSynthetic()) {
                    return;
                }
            }
            collectMatch(match);
        }
    };
    SearchPattern pattern;
    if (isConstructor) {
        // // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=226151 : don't find binary refs for constructors for now
        // return ConstructorReferenceFinder.getConstructorOccurrences(fMethod, pm, status);
        // SearchPattern occPattern= SearchPattern.createPattern(fMethod, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
        SearchPattern declPattern = SearchPattern.createPattern(fMethod, IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
        SearchPattern refPattern = SearchPattern.createPattern(fMethod, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
        // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=226151 : do two searches
        try {
            SearchEngine engine = new SearchEngine();
            engine.search(declPattern, SearchUtils.getDefaultSearchParticipants(), createRefactoringScope(), requestor, new NullProgressMonitor());
            engine.search(refPattern, SearchUtils.getDefaultSearchParticipants(), createRefactoringScope(), requestor, pm);
        } catch (CoreException e) {
            throw new JavaModelException(e);
        }
        return RefactoringSearchEngine.groupByCu(requestor.getResults(), status);
    } else {
        pattern = RefactoringSearchEngine.createOrPattern(fRippleMethods, IJavaSearchConstants.ALL_OCCURRENCES);
    }
    return RefactoringSearchEngine.search(pattern, createRefactoringScope(), requestor, pm, status);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) JavaModelException(org.eclipse.jdt.core.JavaModelException) MethodReferenceMatch(org.eclipse.jdt.core.search.MethodReferenceMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CuCollectingSearchRequestor(org.eclipse.jdt.ls.core.internal.corext.refactoring.rename.CuCollectingSearchRequestor) CoreException(org.eclipse.core.runtime.CoreException) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 95 with SearchEngine

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

the class RenameMethodProcessor method searchForDeclarationsOfClashingMethods.

private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException {
    final List<IMethod> results = new ArrayList<>();
    SearchPattern pattern = createNewMethodPattern();
    IJavaSearchScope scope = RefactoringScopeFactory.create(getMethod().getJavaProject());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object method = match.getElement();
            if (method instanceof IMethod) {
                results.add((IMethod) method);
            } else {
                // $NON-NLS-1$
                JavaLanguageServerPlugin.logError("Unexpected element in search match: " + match.toString());
            }
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return results.toArray(new IMethod[results.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.ls.core.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) ArrayList(java.util.ArrayList) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

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