use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project che by eclipse.
the class RenameMethodProcessor method addOccurrences.
/**
* Add occurrences
*
* @param manager the text change manager
* @param pm the progress monitor
* @param status the status
* @throws CoreException if change creation failed
*/
protected void addOccurrences(TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException /*thrown in subtype*/
{
//$NON-NLS-1$
pm.beginTask("", fOccurrences.length);
for (int i = 0; i < fOccurrences.length; i++) {
ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
if (cu == null)
continue;
SearchMatch[] results = fOccurrences[i].getSearchResults();
// Split matches into declaration and non-declaration matches
List<SearchMatch> declarationsInThisCu = new ArrayList<SearchMatch>();
List<SearchMatch> referencesInThisCu = new ArrayList<SearchMatch>();
for (int j = 0; j < results.length; j++) {
if (results[j] instanceof MethodDeclarationMatch)
declarationsInThisCu.add(results[j]);
else
referencesInThisCu.add(results[j]);
}
// First, handle the declarations
if (declarationsInThisCu.size() > 0) {
if (fDelegateUpdating) {
// Update with delegates
CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu);
rewrite.setResolveBindings(true);
for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
SearchMatch element = iter.next();
MethodDeclaration method = ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) element.getElement(), rewrite.getRoot());
DelegateCreator creator = new DelegateMethodCreator();
creator.setDeclareDeprecated(fDelegateDeprecation);
creator.setDeclaration(method);
creator.setSourceRewrite(rewrite);
creator.setNewElementName(getNewElementName());
creator.prepareDelegate();
creator.createEdit();
}
// Need to handle all delegates first as this
// creates a completely new change object.
TextChange changeForThisCu = rewrite.createChange(true);
changeForThisCu.setKeepPreviewEdits(true);
manager.manage(cu, changeForThisCu);
}
// Update the normal methods
for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
SearchMatch element = iter.next();
simpleUpdate(element, cu, manager.get(cu));
}
}
// Second, handle references
if (fUpdateReferences) {
for (Iterator<SearchMatch> iter = referencesInThisCu.iterator(); iter.hasNext(); ) {
SearchMatch element = iter.next();
simpleUpdate(element, cu, manager.get(cu));
}
}
pm.worked(1);
if (pm.isCanceled())
throw new OperationCanceledException();
}
pm.done();
}
use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project che by eclipse.
the class MethodOccurenceCollector method acceptSearchMatch.
@Override
public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
if (match instanceof MethodReferenceMatch && ((MethodReferenceMatch) match).isSuperInvocation() && match.getAccuracy() == SearchMatch.A_INACCURATE) {
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491
return;
}
if (match.isImplicit()) {
// see bug 94062
collectMatch(match);
return;
}
int start = match.getOffset();
int length = match.getLength();
String matchText = unit.getBuffer().getText(start, length);
//direct match:
if (fName.equals(matchText)) {
collectMatch(match);
return;
}
// lambda expression
if (match instanceof MethodDeclarationMatch && match.getElement() instanceof IMethod && ((IMethod) match.getElement()).isLambdaMethod()) {
// don't touch the lambda
return;
}
//Not a standard reference -- use scanner to find last identifier token before left parenthesis:
IScanner scanner = getScanner(unit);
scanner.setSource(matchText.toCharArray());
int simpleNameStart = -1;
int simpleNameEnd = -1;
try {
int token = scanner.getNextToken();
while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) {
// reference in code includes arguments in parentheses
if (token == ITerminalSymbols.TokenNameIdentifier) {
simpleNameStart = scanner.getCurrentTokenStartPosition();
simpleNameEnd = scanner.getCurrentTokenEndPosition();
}
token = scanner.getNextToken();
}
} catch (InvalidInputException e) {
//ignore
}
if (simpleNameStart != -1) {
match.setOffset(start + simpleNameStart);
match.setLength(simpleNameEnd + 1 - simpleNameStart);
}
collectMatch(match);
}
use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project che by eclipse.
the class RenameAnalyzeUtil method addReferenceShadowedError.
private static void addReferenceShadowedError(ICompilationUnit cu, SearchMatch newMatch, String newElementName, RefactoringStatus result) {
//TODO: should not have to filter declarations:
if (newMatch instanceof MethodDeclarationMatch || newMatch instanceof FieldDeclarationMatch)
return;
ISourceRange range = getOldSourceRange(newMatch);
RefactoringStatusContext context = JavaStatusContext.create(cu, range);
String message = Messages.format(RefactoringCoreMessages.RenameAnalyzeUtil_reference_shadowed, new String[] { BasicElementLabels.getFileName(cu), BasicElementLabels.getJavaElementName(newElementName) });
result.addError(message, context);
}
use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project che by eclipse.
the class RenameAnalyzeUtil method addShadowsError.
private static void addShadowsError(ICompilationUnit cu, SearchMatch oldMatch, RefactoringStatus result) {
//TODO: should not have to filter declarations:
if (oldMatch instanceof MethodDeclarationMatch || oldMatch instanceof FieldDeclarationMatch)
return;
ISourceRange range = new SourceRange(oldMatch.getOffset(), oldMatch.getLength());
RefactoringStatusContext context = JavaStatusContext.create(cu, range);
String message = Messages.format(RefactoringCoreMessages.RenameAnalyzeUtil_shadows, BasicElementLabels.getFileName(cu));
result.addError(message, context);
}
use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project che by eclipse.
the class RenameNonVirtualMethodProcessor method addReferenceUpdates.
private void addReferenceUpdates(TextChangeManager manager, IProgressMonitor pm) {
SearchResultGroup[] grouped = getOccurrences();
for (int i = 0; i < grouped.length; i++) {
SearchResultGroup group = grouped[i];
SearchMatch[] results = group.getSearchResults();
ICompilationUnit cu = group.getCompilationUnit();
TextChange change = manager.get(cu);
for (int j = 0; j < results.length; j++) {
SearchMatch match = results[j];
if (!(match instanceof MethodDeclarationMatch)) {
ReplaceEdit replaceEdit = createReplaceEdit(match, cu);
String editName = RefactoringCoreMessages.RenamePrivateMethodRefactoring_update;
addTextEdit(change, editName, replaceEdit);
}
}
}
pm.done();
}
Aggregations