use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class ClassFileUtil method getURI.
public static String getURI(IProject project, String fqcn) throws JavaModelException {
Assert.isNotNull(project, "Project can't be null");
Assert.isNotNull(fqcn, "FQCN can't be null");
IJavaProject javaProject = JavaCore.create(project);
int lastDot = fqcn.lastIndexOf(".");
String packageName = lastDot > 0 ? fqcn.substring(0, lastDot) : "";
String className = lastDot > 0 ? fqcn.substring(lastDot + 1) : fqcn;
ClassUriExtractor extractor = new ClassUriExtractor();
new SearchEngine().searchAllTypeNames(packageName.toCharArray(), SearchPattern.R_EXACT_MATCH, className.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, JDTUtils.createSearchScope(javaProject, JavaLanguageServerPlugin.getPreferencesManager()), extractor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
return extractor.uri;
}
use of org.eclipse.jdt.core.search.SearchEngine in project xtext-eclipse by eclipse.
the class JdtBasedConstructorScope method collectContents.
public void collectContents(IJavaSearchScope searchScope, SearchRequestor searchRequestor) throws CoreException {
SearchPattern pattern = new ConstructorDeclarationPattern(null, null, SearchPattern.R_PREFIX_MATCH);
new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), searchScope, searchRequestor, new NullProgressMonitor());
}
use of org.eclipse.jdt.core.search.SearchEngine in project xtext-eclipse by eclipse.
the class JvmImplementationOpener method openImplementations.
/**
* Main parts of the logic is taken from {@link org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink}
*
* @param element - Element to show implementations for
* @param textviewer - Viewer to show hierarchy view on
* @param region - Region where to show hierarchy view
*/
public void openImplementations(final IJavaElement element, ITextViewer textviewer, IRegion region) {
if (element instanceof IMethod) {
ITypeRoot typeRoot = ((IMethod) element).getTypeRoot();
CompilationUnit ast = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null);
if (ast == null) {
openQuickHierarchy(textviewer, element, region);
return;
}
try {
ISourceRange nameRange = ((IMethod) element).getNameRange();
ASTNode node = NodeFinder.perform(ast, nameRange);
ITypeBinding parentTypeBinding = null;
if (node instanceof SimpleName) {
ASTNode parent = node.getParent();
if (parent instanceof MethodInvocation) {
Expression expression = ((MethodInvocation) parent).getExpression();
if (expression == null) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
} else {
parentTypeBinding = expression.resolveTypeBinding();
}
} else if (parent instanceof SuperMethodInvocation) {
// Directly go to the super method definition
openEditor(element);
return;
} else if (parent instanceof MethodDeclaration) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
}
}
final IType type = parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null;
if (type == null) {
openQuickHierarchy(textviewer, element, region);
return;
}
final String earlyExitIndicator = "EarlyExitIndicator";
final ArrayList<IJavaElement> links = Lists.newArrayList();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
String methodLabel = JavaElementLabels.getElementLabel(element, JavaElementLabels.DEFAULT_QUALIFIED);
monitor.beginTask(Messages.format("Searching for implementors of ''{0}''", methodLabel), 100);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
IJavaElement element = (IJavaElement) match.getElement();
if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod) element)) {
links.add(element);
if (links.size() > 1) {
throw new OperationCanceledException(earlyExitIndicator);
}
}
}
}
};
int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
SearchPattern pattern = SearchPattern.createPattern(element, limitTo);
Assert.isNotNull(pattern);
SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
SearchEngine engine = new SearchEngine();
engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, SubMonitor.convert(monitor, 100));
if (monitor.isCanceled()) {
throw new InterruptedException();
}
} catch (OperationCanceledException e) {
throw new InterruptedException(e.getMessage());
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
} catch (InvocationTargetException e) {
IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format("An error occurred while searching for implementations of method ''{0}''. See error log for details.", element.getElementName()), e.getCause());
JavaPlugin.log(status);
ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Implementation", "Problems finding implementations.", status);
} catch (InterruptedException e) {
if (e.getMessage() != earlyExitIndicator) {
return;
}
}
if (links.size() == 1) {
openEditor(links.get(0));
} else {
openQuickHierarchy(textviewer, element, region);
}
} catch (JavaModelException e) {
log.error("An error occurred while searching for implementations", e.getCause());
} catch (PartInitException e) {
log.error("An error occurred while searching for implementations", e.getCause());
}
}
}
use of org.eclipse.jdt.core.search.SearchEngine in project egradle by de-jcup.
the class JDTDataAccess method findType.
/**
* Find type
*
* @param className
* @param monitor
* @return type or <code>null</code>
*/
public IType findType(String className, IProgressMonitor monitor) {
final IType[] result = { null };
TypeNameMatchRequestor nameMatchRequestor = new TypeNameMatchRequestor() {
@Override
public void acceptTypeNameMatch(TypeNameMatch match) {
result[0] = match.getType();
}
};
int lastDot = className.lastIndexOf('.');
char[] packageName = lastDot >= 0 ? className.substring(0, lastDot).toCharArray() : null;
char[] typeName = (lastDot >= 0 ? className.substring(lastDot + 1) : className).toCharArray();
SearchEngine engine = new SearchEngine();
int packageMatchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
try {
engine.searchAllTypeNames(packageName, packageMatchRule, typeName, packageMatchRule, IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(), nameMatchRequestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
} catch (JavaModelException e) {
EditorUtil.INSTANCE.logError("Was not able to search all type names", e);
}
return result[0];
}
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);
}
}
Aggregations