Search in sources :

Example 76 with SearchEngine

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

the class ReferenceFinderUtil method getMethodReferencesIn.

private static List<SearchMatch> getMethodReferencesIn(IJavaElement element, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException {
    CollectingSearchRequestor requestor = new CollectingSearchRequestor();
    SearchEngine engine = owner != null ? new SearchEngine(owner) : new SearchEngine();
    engine.searchDeclarationsOfSentMessages(element, requestor, pm);
    return requestor.getResults();
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CollectingSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor)

Example 77 with SearchEngine

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

the class ReferenceFinderUtil method getFieldReferencesIn.

private static List<SearchMatch> getFieldReferencesIn(IJavaElement element, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException {
    CollectingSearchRequestor requestor = new CollectingSearchRequestor();
    SearchEngine engine = owner != null ? new SearchEngine(owner) : new SearchEngine();
    engine.searchDeclarationsOfAccessedFields(element, requestor, pm);
    return requestor.getResults();
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CollectingSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor)

Example 78 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project tdi-studio-se by Talend.

the class OpenDeclarationAction method doSearchSource.

/**
     * Searches the source for the given class name.
     * 
     * @param name The class name
     * @return The source
     * @throws CoreException
     */
IType doSearchSource(String name) throws CoreException {
    final List<IType> results = new ArrayList<IType>();
    // create requester
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IType) {
                results.add((IType) element);
            }
        }
    };
    String baseName = name.replace('$', '.');
    // create search engine and pattern
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(baseName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    // search the source for the given name
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), requestor, null);
    if (results.size() > 0) {
        // at most one source should be found
        return results.get(0);
    }
    return null;
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ArrayList(java.util.ArrayList) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IType(org.eclipse.jdt.core.IType)

Example 79 with SearchEngine

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

the class ReplaceQualifiedNamesBySimpleNamesRefactoring method importTypesFromPackage.

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

        @Override
        public void acceptTypeNameMatch(TypeNameMatch typeNameMatch) {
            final boolean isTopLevelType = typeNameMatch.getType().getDeclaringType() == null;
            if (isTopLevelType) {
                if (!pkgName.equals(typeNameMatch.getPackageName())) {
                    // sanity check failed
                    throw new IllegalStateException("Expected package '" + typeNameMatch.getPackageName() + "' to be equal to '" + pkgName + "'");
                }
                QName qname = QName.valueOf(typeNameMatch.getFullyQualifiedName());
                types.addName(FQN.fromImport(qname, true));
            }
        }
    };
    try {
        final SearchEngine searchEngine = new SearchEngine();
        searchEngine.searchAllTypeNames(// search in this package
        pkgName.toCharArray(), // search in this package
        R_EXACT_MATCH, // do not filter by type name
        null, // do not filter by type name
        R_EXACT_MATCH, // look for all java types (class, interfaces, enums, etc.)
        TYPE, // search everywhere
        createWorkspaceScope(), importTypeCollector, // wait in case the indexer is indexing
        WAIT_UNTIL_READY_TO_SEARCH, ctx.getProgressMonitor());
    } catch (JavaModelException e) {
        throw new UnhandledException(node, 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 80 with SearchEngine

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

the class PkgPatternsProposalProvider method doGenerateProposals.

@Override
protected Collection<? extends IContentProposal> doGenerateProposals(String contents, int position) {
    String prefix = contents.substring(0, position);
    final int replaceFromPos;
    if (prefix.startsWith("!")) {
        // $NON-NLS-1$
        prefix = prefix.substring(1);
        replaceFromPos = 1;
    } else {
        replaceFromPos = 0;
    }
    Comparator<PkgPatternProposal> comparator = new Comparator<PkgPatternProposal>() {

        @Override
        public int compare(PkgPatternProposal o1, PkgPatternProposal o2) {
            int result = o1.getPackageFragment().getElementName().compareTo(o2.getPackageFragment().getElementName());
            if (result == 0) {
                result = Boolean.valueOf(o1.isWildcard()).compareTo(Boolean.valueOf(o2.isWildcard()));
            }
            return result;
        }
    };
    final TreeSet<PkgPatternProposal> result = new TreeSet<PkgPatternProposal>(comparator);
    final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() });
    final SearchPattern pattern = SearchPattern.createPattern("*" + prefix + "*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IPackageFragment pkg = (IPackageFragment) match.getElement();
            // "java." since these cannot be imported
            if (pkg.isDefaultPackage() || pkg.getElementName().startsWith("java."))
                return;
            result.add(new PkgPatternProposal(pkg, false, replaceFromPos));
            result.add(new PkgPatternProposal(pkg, true, replaceFromPos));
        }
    };
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        IRunnableContext runContext = searchContext.getRunContext();
        if (runContext != null) {
            runContext.run(false, false, runnable);
        } else {
            runnable.run(new NullProgressMonitor());
        }
        return result;
    } catch (InvocationTargetException e) {
        logger.logError("Error searching for packages.", e);
        return Collections.emptyList();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return Collections.emptyList();
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InvocationTargetException(java.lang.reflect.InvocationTargetException) Comparator(java.util.Comparator) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TreeSet(java.util.TreeSet) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

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