use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
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 final 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();
}
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class WorkspaceSymbolHandler method search.
public static List<SymbolInformation> search(String query, int maxResults, String projectName, boolean sourceOnly, IProgressMonitor monitor) {
ArrayList<SymbolInformation> symbols = new ArrayList<>();
if (StringUtils.isBlank(query)) {
return symbols;
}
try {
monitor.beginTask("Searching the types...", 100);
IJavaSearchScope searchScope = createSearchScope(projectName, sourceOnly);
String tQuery = query.trim();
String qualifierName = null;
String typeName = tQuery;
int qualifierMatchRule = SearchPattern.R_PATTERN_MATCH;
int qualIndex = tQuery.lastIndexOf('.');
if (qualIndex != -1) {
qualifierName = tQuery.substring(0, qualIndex);
typeName = tQuery.substring(qualIndex + 1);
qualifierMatchRule = SearchPattern.R_CAMELCASE_MATCH;
if (qualifierName.contains("*") || qualifierName.contains("?")) {
qualifierMatchRule = SearchPattern.R_PATTERN_MATCH;
}
}
int typeMatchRule = SearchPattern.R_CAMELCASE_MATCH;
if (typeName.contains("*") || typeName.contains("?")) {
typeMatchRule = SearchPattern.R_PATTERN_MATCH;
}
PreferenceManager preferenceManager = JavaLanguageServerPlugin.getPreferencesManager();
new SearchEngine().searchAllTypeNames(qualifierName == null ? null : qualifierName.toCharArray(), qualifierMatchRule, typeName.toCharArray(), typeMatchRule, IJavaSearchConstants.TYPE, searchScope, new TypeNameMatchRequestor() {
@Override
public void acceptTypeNameMatch(TypeNameMatch match) {
try {
if (maxResults > 0 && symbols.size() >= maxResults) {
return;
}
Location location = null;
try {
if (!sourceOnly && match.getType().isBinary()) {
location = JDTUtils.toLocation(match.getType().getClassFile());
} else if (!match.getType().isBinary()) {
location = JDTUtils.toLocation(match.getType());
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
return;
}
if (location != null && match.getSimpleTypeName() != null && !match.getSimpleTypeName().isEmpty()) {
SymbolInformation symbolInformation = new SymbolInformation();
symbolInformation.setContainerName(match.getTypeContainerName());
symbolInformation.setName(match.getSimpleTypeName());
symbolInformation.setKind(mapKind(match));
if (Flags.isDeprecated(match.getType().getFlags())) {
if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
symbolInformation.setTags(List.of(SymbolTag.Deprecated));
} else {
symbolInformation.setDeprecated(true);
}
}
symbolInformation.setLocation(location);
symbols.add(symbolInformation);
if (maxResults > 0 && symbols.size() >= maxResults) {
monitor.setCanceled(true);
}
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getSimpleTypeName(), e);
return;
}
}
private SymbolKind mapKind(TypeNameMatch match) {
int flags = match.getModifiers();
if (Flags.isInterface(flags)) {
return SymbolKind.Interface;
}
if (Flags.isAnnotation(flags)) {
return SymbolKind.Property;
}
if (Flags.isEnum(flags)) {
return SymbolKind.Enum;
}
return SymbolKind.Class;
}
}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
if (preferenceManager != null && preferenceManager.getPreferences().isIncludeSourceMethodDeclarations()) {
monitor.beginTask("Searching methods...", 100);
IJavaSearchScope nonSourceSearchScope = createSearchScope(projectName, true);
new SearchEngine().searchAllMethodNames(null, SearchPattern.R_PATTERN_MATCH, query.trim().toCharArray(), typeMatchRule, nonSourceSearchScope, new MethodNameMatchRequestor() {
@Override
public void acceptMethodNameMatch(MethodNameMatch match) {
try {
if (maxResults > 0 && symbols.size() >= maxResults) {
return;
}
Location location = null;
try {
location = JDTUtils.toLocation(match.getMethod());
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
return;
}
if (location != null && match.getMethod().getElementName() != null && !match.getMethod().getElementName().isEmpty()) {
SymbolInformation symbolInformation = new SymbolInformation();
symbolInformation.setContainerName(match.getMethod().getDeclaringType().getFullyQualifiedName());
symbolInformation.setName(match.getMethod().getElementName());
symbolInformation.setKind(SymbolKind.Method);
if (Flags.isDeprecated(match.getMethod().getFlags())) {
if (preferenceManager != null && preferenceManager.getClientPreferences().isSymbolTagSupported()) {
symbolInformation.setTags(List.of(SymbolTag.Deprecated));
} else {
symbolInformation.setDeprecated(true);
}
}
symbolInformation.setLocation(location);
symbols.add(symbolInformation);
if (maxResults > 0 && symbols.size() >= maxResults) {
monitor.setCanceled(true);
}
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Unable to determine location for " + match.getMethod().getElementName(), e);
return;
}
}
}, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
}
} catch (Exception e) {
if (e instanceof OperationCanceledException) {
// ignore.
} else {
JavaLanguageServerPlugin.logException("Problem getting search for" + query, e);
}
} finally {
monitor.done();
}
return symbols;
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class RippleMethodFinder method findAllDeclarations.
private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException {
fDeclarations = new ArrayList<>();
class MethodRequestor extends SearchRequestor {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
IMethod method = (IMethod) match.getElement();
boolean isBinary = method.isBinary();
if (!isBinary) {
fDeclarations.add(method);
}
}
}
int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
int matchRule = SearchPattern.R_ERASURE_MATCH | SearchPattern.R_CASE_SENSITIVE;
SearchPattern pattern = SearchPattern.createPattern(fMethod, limitTo, matchRule);
MethodRequestor requestor = new MethodRequestor();
SearchEngine searchEngine = owner != null ? new SearchEngine(owner) : new SearchEngine();
searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), requestor, monitor);
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class RenameMethodProcessor method batchFindNewOccurrences.
// Lower memory footprint than batchFindNewOccurrences. Not used because it is too slow.
// Final solution is maybe to do searches in chunks of ~ 50 CUs.
// private SearchResultGroup[] findNewOccurrences(IMethod[] newMethods, ICompilationUnit[] newDeclarationWCs, IProgressMonitor pm) throws CoreException {
// pm.beginTask("", fOccurrences.length * 2); //$NON-NLS-1$
//
// SearchPattern refsPattern= RefactoringSearchEngine.createOrPattern(newMethods, IJavaSearchConstants.REFERENCES);
// SearchParticipant[] searchParticipants= SearchUtils.getDefaultSearchParticipants();
// IJavaSearchScope scope= RefactoringScopeFactory.create(newMethods);
// MethodOccurenceCollector requestor= new MethodOccurenceCollector(getNewElementName());
// SearchEngine searchEngine= new SearchEngine(fWorkingCopyOwner);
//
// //TODO: should process only references
// for (int j= 0; j < fOccurrences.length; j++) { //should be getReferences()
// //cut memory peak by holding only one reference CU at a time in memory
// ICompilationUnit originalCu= fOccurrences[j].getCompilationUnit();
// ICompilationUnit newWc= null;
// try {
// ICompilationUnit wc= RenameAnalyzeUtil.findWorkingCopyForCu(newDeclarationWCs, originalCu);
// if (wc == null) {
// newWc= RenameAnalyzeUtil.createNewWorkingCopy(originalCu, fChangeManager, fWorkingCopyOwner,
// new SubProgressMonitor(pm, 1));
// }
// searchEngine.search(refsPattern, searchParticipants, scope, requestor, new SubProgressMonitor(pm, 1));
// } finally {
// if (newWc != null)
// newWc.discardWorkingCopy();
// }
// }
// SearchResultGroup[] newResults= RefactoringSearchEngine.groupByResource(requestor.getResults());
// pm.done();
// return newResults;
// }
private SearchResultGroup[] batchFindNewOccurrences(IMethod[] wcNewMethods, final IMethod[] wcOldMethods, ICompilationUnit[] newDeclarationWCs, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
// $NON-NLS-1$
pm.beginTask("", 2);
SearchPattern refsPattern = RefactoringSearchEngine.createOrPattern(wcNewMethods, IJavaSearchConstants.REFERENCES);
SearchParticipant[] searchParticipants = SearchUtils.getDefaultSearchParticipants();
IJavaSearchScope scope = RefactoringScopeFactory.create(wcNewMethods);
MethodOccurenceCollector requestor;
if (getDelegateUpdating()) {
// There will be two new matches inside the delegate(s) (the invocation
// and the javadoc) which are OK and must not be reported.
// Note that except these ocurrences, the delegate bodies are empty
// (as they were created this way).
requestor = new MethodOccurenceCollector(getNewElementName()) {
@Override
public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
for (int i = 0; i < wcOldMethods.length; i++) {
if (wcOldMethods[i].equals(match.getElement())) {
return;
}
}
super.acceptSearchMatch(unit, match);
}
};
} else {
requestor = new MethodOccurenceCollector(getNewElementName());
}
SearchEngine searchEngine = new SearchEngine(fWorkingCopyOwner);
ArrayList<ICompilationUnit> needWCs = new ArrayList<>();
HashSet<ICompilationUnit> declaringCUs = new HashSet<>(newDeclarationWCs.length);
for (int i = 0; i < newDeclarationWCs.length; i++) {
declaringCUs.add(newDeclarationWCs[i].getPrimary());
}
for (int i = 0; i < fOccurrences.length; i++) {
ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
if (!declaringCUs.contains(cu)) {
needWCs.add(cu);
}
}
ICompilationUnit[] otherWCs = null;
try {
otherWCs = RenameAnalyzeUtil.createNewWorkingCopies(needWCs.toArray(new ICompilationUnit[needWCs.size()]), fChangeManager, fWorkingCopyOwner, new SubProgressMonitor(pm, 1));
searchEngine.search(refsPattern, searchParticipants, scope, requestor, new SubProgressMonitor(pm, 1));
} finally {
pm.done();
if (otherWCs != null) {
for (int i = 0; i < otherWCs.length; i++) {
otherWCs[i].discardWorkingCopy();
}
}
}
SearchResultGroup[] newResults = RefactoringSearchEngine.groupByCu(requestor.getResults(), status);
return newResults;
}
use of org.eclipse.jdt.core.search.SearchEngine in project eclipse.jdt.ls by eclipse.
the class RenameMethodProcessor method searchForOuterTypesOfReferences.
private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
final Set<IType> outerTypesOfReferences = new HashSet<>();
SearchPattern pattern = RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
IJavaSearchScope scope = createRefactoringScope(getMethod());
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
Object element = match.getElement();
if (!(element instanceof IMember)) {
// e.g. an IImportDeclaration for a static method import
return;
}
IMember member = (IMember) element;
IType declaring = member.getDeclaringType();
if (declaring == null) {
return;
}
IType outer = declaring.getDeclaringType();
if (outer != null) {
outerTypesOfReferences.add(declaring);
}
}
};
new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
Aggregations