use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class RefactoringSearchEngine2 method searchReferencedMethods.
/**
* Performs the search of referenced methods.
*
* @param element the java element whose referenced methods have to be found
* @param monitor the progress monitor, or <code>null</code>
* @throws JavaModelException if an error occurs during search
*/
public final void searchReferencedMethods(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
Assert.isNotNull(element);
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
// $NON-NLS-1$
monitor.beginTask("", 1);
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_methods);
try {
SearchEngine engine = null;
if (fOwner != null) {
engine = new SearchEngine(fOwner);
} else {
engine = new SearchEngine(fWorkingCopies);
}
engine.searchDeclarationsOfSentMessages(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
} catch (CoreException exception) {
throw new JavaModelException(exception);
}
} finally {
monitor.done();
}
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class RefactoringSearchEngine2 method searchReferencedFields.
/**
* Performs the search of referenced fields.
*
* @param element the java element whose referenced fields have to be found
* @param monitor the progress monitor, or <code>null</code>
* @throws JavaModelException if an error occurs during search
*/
public final void searchReferencedFields(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
Assert.isNotNull(element);
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
// $NON-NLS-1$
monitor.beginTask("", 1);
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_fields);
try {
SearchEngine engine = null;
if (fOwner != null) {
engine = new SearchEngine(fOwner);
} else {
engine = new SearchEngine(fWorkingCopies);
}
engine.searchDeclarationsOfAccessedFields(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
} catch (CoreException exception) {
throw new JavaModelException(exception);
}
} finally {
monitor.done();
}
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class ImplementationCollector method findMethodImplementations.
private List<T> findMethodImplementations(IProgressMonitor monitor) throws CoreException {
IMethod method = (IMethod) javaElement;
try {
if (cannotBeOverriddenMethod(method)) {
return null;
}
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Find method implementations failure ", e);
return null;
}
CompilationUnit ast = CoreASTProvider.getInstance().getAST(typeRoot, CoreASTProvider.WAIT_YES, monitor);
if (ast == null) {
return null;
}
ASTNode node = NodeFinder.perform(ast, region.getOffset(), region.getLength());
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
return Collections.singletonList(mapper.convert(method, 0, 0));
} else if (parent instanceof MethodDeclaration) {
parentTypeBinding = Bindings.getBindingOfParentType(node);
}
}
final IType receiverType = getType(parentTypeBinding);
if (receiverType == null) {
return null;
}
final List<T> results = new ArrayList<>();
try {
String methodLabel = JavaElementLabelsCore.getElementLabel(method, JavaElementLabelsCore.DEFAULT_QUALIFIED);
monitor.beginTask(Messages.format(JavaElementImplementationHyperlink_search_method_implementors, methodLabel), 10);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
Object element = match.getElement();
if (element instanceof IMethod) {
IMethod methodFound = (IMethod) element;
if (!JdtFlags.isAbstract(methodFound)) {
T result = mapper.convert(methodFound, match.getOffset(), match.getLength());
if (result != null) {
results.add(result);
}
}
}
}
}
};
IJavaSearchScope hierarchyScope;
if (receiverType.isInterface()) {
hierarchyScope = SearchEngine.createHierarchyScope(method.getDeclaringType());
} else {
if (isFullHierarchyNeeded(new SubProgressMonitor(monitor, 3), method, receiverType)) {
hierarchyScope = SearchEngine.createHierarchyScope(receiverType);
} else {
boolean isMethodAbstract = JdtFlags.isAbstract(method);
hierarchyScope = SearchEngine.createStrictHierarchyScope(null, receiverType, true, isMethodAbstract, null);
}
}
int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
SearchPattern pattern = SearchPattern.createPattern(method, limitTo);
Assert.isNotNull(pattern);
SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
SearchEngine engine = new SearchEngine();
engine.search(pattern, participants, hierarchyScope, requestor, new SubProgressMonitor(monitor, 7));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
} finally {
monitor.done();
}
return results;
}
use of org.eclipse.jdt.core.search.SearchEngine in project lsp4jakarta by eclipse.
the class TypeHierarchyUtils method hasKnownDeclaration.
private static boolean hasKnownDeclaration(IType type) throws CoreException {
String typeName = type.getElementName();
final AtomicInteger references = new AtomicInteger(0);
SearchEngine engine = new SearchEngine();
SearchPattern pattern = SearchPattern.createPattern(typeName, IJavaSearchConstants.CLASS, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(type.getJavaProject()), new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
Object o = match.getElement();
if (o instanceof IType) {
IType t = (IType) o;
if (t.getElementName().equals(typeName)) {
references.incrementAndGet();
}
}
}
}, null);
if (references.get() > 0) {
return true;
}
return false;
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ui by eclipse-jdt.
the class RefactoringSearchEngine2 method searchReferencedTypes.
/**
* Performs the search of referenced types.
*
* @param element the java element whose referenced types have to be found
* @param monitor the progress monitor, or <code>null</code>
* @throws JavaModelException if an error occurs during search
*/
public void searchReferencedTypes(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
Assert.isNotNull(element);
if (monitor == null)
monitor = new NullProgressMonitor();
try {
// $NON-NLS-1$
monitor.beginTask("", 1);
monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_types);
try {
SearchEngine engine = null;
if (fOwner != null)
engine = new SearchEngine(fOwner);
else
engine = new SearchEngine(fWorkingCopies);
engine.searchDeclarationsOfReferencedTypes(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
} catch (CoreException exception) {
throw new JavaModelException(exception);
}
} finally {
monitor.done();
}
}
Aggregations