use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class IntroduceIndirectionRefactoring method updateReferences.
private RefactoringStatus updateReferences(IProgressMonitor monitor) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
//$NON-NLS-1$
monitor.beginTask("", 90);
if (monitor.isCanceled())
throw new OperationCanceledException();
IMethod[] ripple = RippleMethodFinder2.getRelatedMethods(fTargetMethod, false, new NoOverrideProgressMonitor(monitor, 10), null);
if (monitor.isCanceled())
throw new OperationCanceledException();
SearchResultGroup[] references = Checks.excludeCompilationUnits(getReferences(ripple, new NoOverrideProgressMonitor(monitor, 10), result), result);
if (result.hasFatalError())
return result;
result.merge(Checks.checkCompileErrorsInAffectedFiles(references));
if (monitor.isCanceled())
throw new OperationCanceledException();
int ticksPerCU = references.length == 0 ? 0 : 70 / references.length;
for (int i = 0; i < references.length; i++) {
SearchResultGroup group = references[i];
SearchMatch[] searchResults = group.getSearchResults();
CompilationUnitRewrite currentCURewrite = getCachedCURewrite(group.getCompilationUnit());
for (int j = 0; j < searchResults.length; j++) {
SearchMatch match = searchResults[j];
if (match.isInsideDocComment())
continue;
IMember enclosingMember = (IMember) match.getElement();
ASTNode target = getSelectedNode(group.getCompilationUnit(), currentCURewrite.getRoot(), match.getOffset(), match.getLength());
if (target instanceof SuperMethodInvocation) {
// Cannot retarget calls to super - add a warning
result.merge(createWarningAboutCall(enclosingMember, target, RefactoringCoreMessages.IntroduceIndirectionRefactoring_call_warning_super_keyword));
continue;
}
//$NON-NLS-1$
Assert.isTrue(target instanceof MethodInvocation, "Element of call should be a MethodInvocation.");
MethodInvocation invocation = (MethodInvocation) target;
ITypeBinding typeBinding = getExpressionType(invocation);
if (fIntermediaryFirstParameterType == null) {
// no highest type yet
fIntermediaryFirstParameterType = typeBinding.getTypeDeclaration();
} else {
// check if current type is higher
result.merge(findCommonParent(typeBinding.getTypeDeclaration()));
}
if (result.hasFatalError())
return result;
// create an edit for this particular call
result.merge(updateMethodInvocation(invocation, enclosingMember, currentCURewrite));
// does call see the intermediary method?
// => increase visibility of the type of the intermediary method.
result.merge(adjustVisibility(fIntermediaryType, enclosingMember.getDeclaringType(), new NoOverrideProgressMonitor(monitor, 0)));
if (monitor.isCanceled())
throw new OperationCanceledException();
}
if (!isRewriteKept(group.getCompilationUnit()))
createChangeAndDiscardRewrite(group.getCompilationUnit());
monitor.worked(ticksPerCU);
}
monitor.done();
return result;
}
use of org.eclipse.jdt.core.search.SearchMatch in project AutoRefactor by JnRouvignac.
the class SuperCallRatherThanUselessOverridingRefactoring method isMethodUsedInItsPackage.
/**
* This method is extremely expensive.
*/
private boolean isMethodUsedInItsPackage(IMethodBinding methodBinding, MethodDeclaration node) {
final IPackageBinding methodPackage = methodBinding.getDeclaringClass().getPackage();
final AtomicBoolean methodIsUsedInPackage = new AtomicBoolean(false);
final SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) {
methodIsUsedInPackage.set(true);
}
};
try {
final SearchEngine searchEngine = new SearchEngine();
searchEngine.search(createPattern(methodBinding.getJavaElement(), REFERENCES, R_EXACT_MATCH), new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createJavaSearchScope(new IJavaElement[] { methodPackage.getJavaElement() }), requestor, ctx.getProgressMonitor());
return methodIsUsedInPackage.get();
} catch (CoreException e) {
throw new UnhandledException(node, e);
}
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class RefactoringSearchEngine method groupByCu.
/**
* @param matchList a List of SearchMatch
* @param status the status to report errors.
* @return a SearchResultGroup[], grouped by SearchMatch#getResource()
*/
public static SearchResultGroup[] groupByCu(List<SearchMatch> matchList, RefactoringStatus status) {
Map<IResource, List<SearchMatch>> grouped = new HashMap<IResource, List<SearchMatch>>();
boolean hasPotentialMatches = false;
boolean hasNonCuMatches = false;
for (Iterator<SearchMatch> iter = matchList.iterator(); iter.hasNext(); ) {
SearchMatch searchMatch = iter.next();
if (searchMatch.getAccuracy() == SearchMatch.A_INACCURATE)
hasPotentialMatches = true;
if (!grouped.containsKey(searchMatch.getResource()))
grouped.put(searchMatch.getResource(), new ArrayList<SearchMatch>(1));
grouped.get(searchMatch.getResource()).add(searchMatch);
}
for (Iterator<IResource> iter = grouped.keySet().iterator(); iter.hasNext(); ) {
IResource resource = iter.next();
IJavaElement element = JavaCore.create(resource);
if (!(element instanceof ICompilationUnit)) {
iter.remove();
hasNonCuMatches = true;
}
}
SearchResultGroup[] result = new SearchResultGroup[grouped.keySet().size()];
int i = 0;
for (Iterator<IResource> iter = grouped.keySet().iterator(); iter.hasNext(); ) {
IResource resource = iter.next();
List<SearchMatch> searchMatches = grouped.get(resource);
SearchMatch[] matchArray = searchMatches.toArray(new SearchMatch[searchMatches.size()]);
result[i] = new SearchResultGroup(resource, matchArray);
i++;
}
addStatusErrors(status, hasPotentialMatches, hasNonCuMatches);
return result;
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class RefactoringSearchEngine2 method getGroupedMatches.
/**
* Returns the found search matches in grouped by their containing resource.
*
* @return the found search matches
*/
private SearchResultGroup[] getGroupedMatches() {
final Map<IResource, List<SearchMatch>> grouped = new HashMap<IResource, List<SearchMatch>>();
List<SearchMatch> matches = null;
IResource resource = null;
SearchMatch match = null;
for (final Iterator<?> iterator = getSearchMatches().iterator(); iterator.hasNext(); ) {
match = (SearchMatch) iterator.next();
resource = match.getResource();
if (!grouped.containsKey(resource))
grouped.put(resource, new ArrayList<SearchMatch>(4));
matches = grouped.get(resource);
matches.add(match);
}
if (fBinary) {
final Collection<IResource> collection = getCollector().getBinaryResources();
for (final Iterator<IResource> iterator = grouped.keySet().iterator(); iterator.hasNext(); ) {
resource = iterator.next();
if (collection.contains(resource))
iterator.remove();
}
}
final SearchResultGroup[] result = new SearchResultGroup[grouped.keySet().size()];
int index = 0;
for (final Iterator<IResource> iterator = grouped.keySet().iterator(); iterator.hasNext(); ) {
resource = iterator.next();
matches = grouped.get(resource);
result[index++] = new SearchResultGroup(resource, matches.toArray(new SearchMatch[matches.size()]));
}
return result;
}
use of org.eclipse.jdt.core.search.SearchMatch in project che by eclipse.
the class RefactoringSearchEngine2 method getSearchMatches.
/**
* Returns the search matches filtered by their accuracy.
*
* @return the filtered search matches
*/
private Collection<?> getSearchMatches() {
Collection<?> results = null;
if (fInaccurate) {
results = new LinkedList<Object>(getCollector().getCollectedMatches());
final Collection<SearchMatch> collection = getCollector().getInaccurateMatches();
SearchMatch match = null;
for (final Iterator<?> iterator = results.iterator(); iterator.hasNext(); ) {
match = (SearchMatch) iterator.next();
if (collection.contains(match))
iterator.remove();
}
} else
results = getCollector().getCollectedMatches();
return results;
}
Aggregations