Search in sources :

Example 6 with MethodDeclarationMatch

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();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch)

Example 7 with MethodDeclarationMatch

use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project eclipse.jdt.ls by eclipse.

the class RenameProcessor method collectMatch.

private TextEdit collectMatch(SearchMatch match, IJavaElement element, ICompilationUnit unit, String newName) throws IndexOutOfBoundsException, JavaModelException {
    if (match instanceof MethodReferenceMatch && ((MethodReferenceMatch) match).isSuperInvocation() && match.getAccuracy() == SearchMatch.A_INACCURATE) {
        return null;
    }
    if (!(element instanceof IMethod) || match.isImplicit()) {
        return new ReplaceEdit(match.getOffset(), match.getLength(), newName);
    }
    int start = match.getOffset();
    int length = match.getLength();
    String matchText = unit.getBuffer().getText(start, length);
    // direct match:
    if (newName.equals(matchText)) {
        return new ReplaceEdit(match.getOffset(), match.getLength(), newName);
    }
    // lambda expression
    if (match instanceof MethodDeclarationMatch && match.getElement() instanceof IMethod && ((IMethod) match.getElement()).isLambdaMethod()) {
        // don't touch the lambda
        return null;
    }
    // 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);
    }
    return new ReplaceEdit(match.getOffset(), match.getLength(), newName);
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException) MethodReferenceMatch(org.eclipse.jdt.core.search.MethodReferenceMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IMethod(org.eclipse.jdt.core.IMethod) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch)

Example 8 with MethodDeclarationMatch

use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project eclipse.jdt.ls 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();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch)

Example 9 with MethodDeclarationMatch

use of org.eclipse.jdt.core.search.MethodDeclarationMatch in project eclipse.jdt.ls 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 == InternalTokenNameIdentifier) {
                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);
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException) MethodReferenceMatch(org.eclipse.jdt.core.search.MethodReferenceMatch) IMethod(org.eclipse.jdt.core.IMethod) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch)

Aggregations

MethodDeclarationMatch (org.eclipse.jdt.core.search.MethodDeclarationMatch)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 IMethod (org.eclipse.jdt.core.IMethod)3 IScanner (org.eclipse.jdt.core.compiler.IScanner)3 InvalidInputException (org.eclipse.jdt.core.compiler.InvalidInputException)3 MethodReferenceMatch (org.eclipse.jdt.core.search.MethodReferenceMatch)3 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)3 ArrayList (java.util.ArrayList)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 ISourceRange (org.eclipse.jdt.core.ISourceRange)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 FieldDeclarationMatch (org.eclipse.jdt.core.search.FieldDeclarationMatch)2 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)2 DelegateCreator (org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateCreator)2 DelegateMethodCreator (org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator)2 CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)2 RefactoringStatusContext (org.eclipse.ltk.core.refactoring.RefactoringStatusContext)2 SourceRange (org.eclipse.jdt.core.SourceRange)1