Search in sources :

Example 21 with SearchPattern

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

the class JavaSearchScopePackageLister method getPackages.

@Override
public String[] getPackages(boolean includeNonSource, IPackageFilter filter) throws PackageListException {
    final List<IJavaElement> packageList = new LinkedList<IJavaElement>();
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IJavaElement enclosingElement = (IJavaElement) match.getElement();
            String name = enclosingElement.getElementName();
            if (name.length() > 0) {
                // Do not include default pkg
                packageList.add(enclosingElement);
            }
        }
    };
    final SearchPattern pattern = SearchPattern.createPattern("*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH | SearchPattern.R_CASE_SENSITIVE);
    IRunnableWithProgress operation = new IRunnableWithProgress() {

        @Override
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        runContext.run(true, true, operation);
    } catch (InvocationTargetException e) {
        throw new PackageListException(e.getCause());
    } catch (InterruptedException e) {
        throw new PackageListException("Operation interrupted");
    }
    // Remove non-source and excludes
    Set<String> packageNames = new LinkedHashSet<String>();
    for (Iterator<IJavaElement> iter = packageList.iterator(); iter.hasNext(); ) {
        boolean omit = false;
        IJavaElement element = iter.next();
        if (!includeNonSource) {
            IPackageFragment pkgFragment = (IPackageFragment) element;
            try {
                if (pkgFragment.getCompilationUnits().length == 0) {
                    omit = true;
                }
            } catch (JavaModelException e) {
                throw new PackageListException(e);
            }
        }
        if (filter != null && !filter.select(element.getElementName())) {
            omit = true;
        }
        if (!omit) {
            packageNames.add(element.getElementName());
        }
    }
    return packageNames.toArray(new String[0]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Aggregations

SearchPattern (org.eclipse.jdt.core.search.SearchPattern)21 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)13 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)12 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)9 CoreException (org.eclipse.core.runtime.CoreException)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)6 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)6 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 ArrayList (java.util.ArrayList)4 IMethod (org.eclipse.jdt.core.IMethod)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 HashSet (java.util.HashSet)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 IType (org.eclipse.jdt.core.IType)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IField (org.eclipse.jdt.core.IField)2