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);
}
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class CodeLensHandler method findReferences.
private List<Location> findReferences(IJavaElement element, IProgressMonitor monitor) throws JavaModelException, CoreException {
if (element == null) {
return Collections.emptyList();
}
SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.REFERENCES);
final List<Location> result = new ArrayList<>();
SearchEngine engine = new SearchEngine();
engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_INACCURATE) {
return;
}
Object o = match.getElement();
if (o instanceof IJavaElement) {
IJavaElement element = (IJavaElement) o;
ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
if (compilationUnit == null) {
return;
}
Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength());
result.add(location);
}
}
}, monitor);
return result;
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class ReferencesHandler method search.
private void search(IJavaElement elementToSearch, final List<Location> locations, IProgressMonitor monitor) throws CoreException, JavaModelException {
boolean includeClassFiles = preferenceManager.isClientSupportsClassFileContent();
boolean includeDecompiledSources = preferenceManager.getPreferences().isIncludeDecompiledSources();
SearchEngine engine = new SearchEngine();
SearchPattern pattern = SearchPattern.createPattern(elementToSearch, IJavaSearchConstants.REFERENCES);
engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_INACCURATE) {
return;
}
Object o = match.getElement();
if (o instanceof IJavaElement) {
IJavaElement element = (IJavaElement) o;
ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
if (compilationUnit != null) {
Location location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength());
locations.add(location);
} else if (includeClassFiles) {
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (cf != null && cf.getSourceRange() != null) {
Location location = JDTUtils.toLocation(cf, match.getOffset(), match.getLength());
locations.add(location);
} else if (includeDecompiledSources && cf != null) {
List<Location> result = JDTUtils.searchDecompiledSources(element, cf, false, false, monitor);
locations.addAll(result);
}
}
}
}
}, monitor);
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class ChangeSignatureProcessor method findOccurrences.
private SearchResultGroup[] findOccurrences(IProgressMonitor pm, ReferencesInBinaryContext binaryRefs, RefactoringStatus status) throws JavaModelException {
final boolean isConstructor = fMethod.isConstructor();
CuCollectingSearchRequestor requestor = new CuCollectingSearchRequestor(binaryRefs) {
@Override
protected void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=27236 :
if (isConstructor && match instanceof MethodReferenceMatch) {
MethodReferenceMatch mrm = (MethodReferenceMatch) match;
if (mrm.isSynthetic()) {
return;
}
}
collectMatch(match);
}
};
SearchPattern pattern;
if (isConstructor) {
// // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=226151 : don't find binary refs for constructors for now
// return ConstructorReferenceFinder.getConstructorOccurrences(fMethod, pm, status);
// SearchPattern occPattern= SearchPattern.createPattern(fMethod, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
SearchPattern declPattern = SearchPattern.createPattern(fMethod, IJavaSearchConstants.DECLARATIONS, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
SearchPattern refPattern = SearchPattern.createPattern(fMethod, IJavaSearchConstants.REFERENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
// workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=226151 : do two searches
try {
SearchEngine engine = new SearchEngine();
engine.search(declPattern, SearchUtils.getDefaultSearchParticipants(), createRefactoringScope(), requestor, new NullProgressMonitor());
engine.search(refPattern, SearchUtils.getDefaultSearchParticipants(), createRefactoringScope(), requestor, pm);
} catch (CoreException e) {
throw new JavaModelException(e);
}
return RefactoringSearchEngine.groupByCu(requestor.getResults(), status);
} else {
pattern = RefactoringSearchEngine.createOrPattern(fRippleMethods, IJavaSearchConstants.ALL_OCCURRENCES);
}
return RefactoringSearchEngine.search(pattern, createRefactoringScope(), requestor, pm, status);
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class RenameMethodProcessor method searchForDeclarationsOfClashingMethods.
private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException {
final List<IMethod> results = new ArrayList<>();
SearchPattern pattern = createNewMethodPattern();
IJavaSearchScope scope = RefactoringScopeFactory.create(getMethod().getJavaProject());
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
Object method = match.getElement();
if (method instanceof IMethod) {
results.add((IMethod) method);
} else {
// $NON-NLS-1$
JavaLanguageServerPlugin.logError("Unexpected element in search match: " + match.toString());
}
}
};
new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
return results.toArray(new IMethod[results.size()]);
}
Aggregations