use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class IntroduceFactoryRefactoring method replaceConstructorCalls.
/**
* Use the given <code>ASTRewrite</code> to replace direct calls to the constructor
* with calls to the newly-created factory method.
* @param rg the <code>SearchResultGroup</code> indicating all of the constructor references
* @param unit the <code>CompilationUnit</code> to be rewritten
* @param unitRewriter the rewriter
* @param unitChange the compilation unit change
* @throws CoreException
* @return true iff at least one constructor call site was rewritten.
*/
private boolean replaceConstructorCalls(SearchResultGroup rg, CompilationUnit unit, ASTRewrite unitRewriter, CompilationUnitChange unitChange) throws CoreException {
Assert.isTrue(ASTCreator.getCu(unit).equals(rg.getCompilationUnit()));
SearchMatch[] hits = rg.getSearchResults();
Arrays.sort(hits, new Comparator<SearchMatch>() {
/**
* Sort by descending offset, such that nested constructor calls are processed first.
* This is necessary, since they can only be moved into the factory method invocation
* after they have been rewritten.
*/
public int compare(SearchMatch m1, SearchMatch m2) {
return m2.getOffset() - m1.getOffset();
}
});
boolean someCallPatched = false;
for (int i = 0; i < hits.length; i++) {
ASTNode ctrCall = getCtorCallAt(hits[i].getOffset(), hits[i].getLength(), unit);
if (ctrCall instanceof ClassInstanceCreation) {
TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_replaceCalls);
rewriteFactoryMethodCall((ClassInstanceCreation) ctrCall, unitRewriter, gd);
unitChange.addTextEditGroup(gd);
someCallPatched = true;
} else if (ctrCall instanceof MethodRef) {
TextEditGroup gd = new TextEditGroup(RefactoringCoreMessages.IntroduceFactoryRefactoring_replaceJavadocReference);
rewriteJavadocReference((MethodRef) ctrCall, unitRewriter, gd);
unitChange.addTextEditGroup(gd);
someCallPatched = true;
}
}
return someCallPatched;
}
use of org.eclipse.jdt.core.search.SearchMatch in project che 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.SearchMatch in project tdi-studio-se by Talend.
the class OpenDeclarationAction method doSearchSource.
/**
* Searches the source for the given class name.
*
* @param name The class name
* @return The source
* @throws CoreException
*/
IType doSearchSource(String name) throws CoreException {
final List<IType> results = new ArrayList<IType>();
// create requester
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
Object element = match.getElement();
if (element instanceof IType) {
results.add((IType) element);
}
}
};
String baseName = name.replace('$', '.');
// create search engine and pattern
SearchEngine engine = new SearchEngine();
SearchPattern pattern = SearchPattern.createPattern(baseName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
// search the source for the given name
engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), requestor, null);
if (results.size() > 0) {
// at most one source should be found
return results.get(0);
}
return null;
}
use of org.eclipse.jdt.core.search.SearchMatch in project bndtools by bndtools.
the class PkgPatternsProposalProvider method doGenerateProposals.
@Override
protected Collection<? extends IContentProposal> doGenerateProposals(String contents, int position) {
String prefix = contents.substring(0, position);
final int replaceFromPos;
if (prefix.startsWith("!")) {
//$NON-NLS-1$
prefix = prefix.substring(1);
replaceFromPos = 1;
} else {
replaceFromPos = 0;
}
Comparator<PkgPatternProposal> comparator = new Comparator<PkgPatternProposal>() {
public int compare(PkgPatternProposal o1, PkgPatternProposal o2) {
int result = o1.getPackageFragment().getElementName().compareTo(o2.getPackageFragment().getElementName());
if (result == 0) {
result = Boolean.valueOf(o1.isWildcard()).compareTo(Boolean.valueOf(o2.isWildcard()));
}
return result;
}
};
final TreeSet<PkgPatternProposal> result = new TreeSet<PkgPatternProposal>(comparator);
final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() });
final SearchPattern pattern = SearchPattern.createPattern("*" + prefix + "*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
final SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
IPackageFragment pkg = (IPackageFragment) match.getElement();
// "java." since these cannot be imported
if (pkg.isDefaultPackage() || pkg.getElementName().startsWith("java."))
return;
result.add(new PkgPatternProposal(pkg, false, replaceFromPos));
result.add(new PkgPatternProposal(pkg, true, replaceFromPos));
}
};
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
}
}
};
try {
IRunnableContext runContext = searchContext.getRunContext();
if (runContext != null) {
runContext.run(false, false, runnable);
} else {
runnable.run(new NullProgressMonitor());
}
return result;
} catch (InvocationTargetException e) {
logger.logError("Error searching for packages.", e);
return Collections.emptyList();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return Collections.emptyList();
}
}
use of org.eclipse.jdt.core.search.SearchMatch 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]);
}
Aggregations