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]);
}
Aggregations