use of org.eclipse.jdt.core.search.SearchEngine 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]);
}
use of org.eclipse.jdt.core.search.SearchEngine in project sts4 by spring-projects.
the class OpenFullyQualifiedNameInEditor method execute.
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String fqName = event.getParameter(FQ_NAME);
String projectName = event.getParameter(PROJECT_NAME);
String packageName = "";
String typeName = fqName;
int idx = fqName.lastIndexOf('.');
if (idx >= 0) {
packageName = fqName.substring(0, idx);
typeName = idx < fqName.length() - 1 ? fqName.substring(idx + 1) : "";
}
if (!typeName.isEmpty()) {
SearchEngine engine = new SearchEngine((WorkingCopyOwner) null);
List<TypeNameMatch> matches = new ArrayList<>();
String searchedTypeName = getSearchedTypeName(typeName);
final boolean isInnerType = searchedTypeName != typeName;
TypeNameMatchRequestor requestor = new TypeNameMatchRequestor() {
@Override
public void acceptTypeNameMatch(TypeNameMatch match) {
if (isInnerType) {
if (match.getFullyQualifiedName().equals(fqName.replace("$", "."))) {
matches.add(match);
}
} else {
matches.add(match);
}
}
};
try {
IJavaSearchScope searchScope = createSearchScope(projectName);
engine.searchAllTypeNames(packageName.toCharArray(), SearchPattern.R_EXACT_MATCH, searchedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, searchScope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
if (matches.isEmpty()) {
BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "Cannot find type: " + fqName));
} else {
if (matches.size() > 1) {
BootLanguageServerPlugin.getDefault().getLog().log(new Status(IStatus.WARNING, BootLanguageServerPlugin.ID, "More than one type is defined for: " + fqName));
}
try {
IType type = matches.get(0).getType();
IEditorPart editorPart = JavaUI.openInEditor(type);
JavaUI.revealInEditor(editorPart, (IJavaElement) type);
} catch (JavaModelException ex) {
// $NON-NLS-1$
throw new ExecutionException("Error opening java element in editor", ex);
} catch (PartInitException ex) {
// $NON-NLS-1$
throw new ExecutionException("Error opening java element in editor", ex);
}
}
} catch (JavaModelException e) {
BootLanguageServerPlugin.getDefault().getLog().log(e.getStatus());
}
}
return null;
}
use of org.eclipse.jdt.core.search.SearchEngine in project webtools.sourceediting by eclipse.
the class JavaTypeFinder method findProposals.
/**
* @param resource
* @param replacementStart
* @param replacementLength
* @param searchFor IJavaSearchConstants.TYPE, IJavaSearchConstants.CLASS
* @return
*/
private static ICompletionProposal[] findProposals(IResource resource, int replacementStart, int replacementLength, int searchFor, boolean ignoreAbstractClasses) {
JavaTypeNameRequestor requestor = new JavaTypeNameRequestor();
requestor.setJSPOffset(replacementStart);
requestor.setReplacementLength(replacementLength);
requestor.setIgnoreAbstractClasses(ignoreAbstractClasses);
try {
IJavaElement[] elements = new IJavaElement[] { getJavaProject(resource) };
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(elements);
new SearchEngine().searchAllTypeNames(null, null, SearchPattern.R_PATTERN_MATCH | SearchPattern.R_PREFIX_MATCH, searchFor, scope, requestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
} catch (CoreException exc) {
Logger.logException(exc);
} catch (Exception exc) {
// JavaModel
Logger.logException(exc);
}
return requestor.getProposals();
}
use of org.eclipse.jdt.core.search.SearchEngine in project liferay-ide by liferay.
the class PortletURLHyperlinkDetector method _findPortletMethods.
private IMethod[] _findPortletMethods(IDocument document, String nameValue) {
IMethod[] retval = null;
IFile file = DOMUtils.getFile(document);
if ((file != null) && file.exists()) {
IJavaProject project = JavaCore.create(file.getProject());
if ((project != null) && project.exists()) {
try {
IType portlet = project.findType("javax.portlet.Portlet");
if (portlet != null) {
List<IMethod> methods = new ArrayList<>();
SearchRequestor requestor = new ActionMethodCollector(methods);
IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(project, portlet, true, false, null);
SearchPattern search = SearchPattern.createPattern(nameValue, IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
SearchParticipant[] searchParticipant = { SearchEngine.getDefaultSearchParticipant() };
new SearchEngine().search(search, searchParticipant, scope, requestor, new NullProgressMonitor());
retval = methods.toArray(new IMethod[0]);
}
} catch (JavaModelException jme) {
} catch (CoreException ce) {
}
}
}
return retval;
}
use of org.eclipse.jdt.core.search.SearchEngine in project liferay-ide by liferay.
the class ServiceMethodHyperlinkDetector method _getServiceWrapperMethod.
private IMethodWrapper _getServiceWrapperMethod(IMethod method) {
IMethodWrapper retval = null;
try {
IJavaElement methodClass = method.getParent();
IType methodClassType = method.getDeclaringType();
String methodClassName = methodClass.getElementName();
if (methodClassName.endsWith("Util") && JdtFlags.isPublic(method) && JdtFlags.isStatic(method)) {
String packageName = methodClassType.getPackageFragment().getElementName();
String baseServiceName = methodClassName.substring(0, methodClassName.length() - 4);
/*
* as per liferay standard wrapper type will be in service package with Wrapper suffix
* e.g com.example.service.FooUtil.getBar() --> com.example.service.FooWrapper.getBar()
*/
String fullyQualifiedName = packageName + "." + baseServiceName + "Wrapper";
IType wrapperType = _findType(methodClass, fullyQualifiedName);
if (wrapperType != null) {
IMethod[] wrapperBaseMethods = wrapperType.findMethods(method);
if (ListUtil.isNotEmpty(wrapperBaseMethods)) {
// look for classes that implement this wrapper
List<IMethod> overrides = new ArrayList<>();
SearchRequestor requestor = new WrapperMethodCollector(overrides, method);
IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(null, wrapperType, true, false, null);
SearchPattern search = SearchPattern.createPattern(method.getElementName(), IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
new SearchEngine().search(search, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
if (overrides.size() > 1) {
retval = new IMethodWrapper(wrapperBaseMethods[0], true);
} else if (overrides.size() == 1) {
retval = new IMethodWrapper(overrides.get(0), false);
}
}
}
}
} catch (Exception e) {
}
return retval;
}
Aggregations