Search in sources :

Example 1 with MethodDeclarationMatch

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();
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) DelegateCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateCreator)

Example 2 with MethodDeclarationMatch

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);
}
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)

Example 3 with MethodDeclarationMatch

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);
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) FieldDeclarationMatch(org.eclipse.jdt.core.search.FieldDeclarationMatch) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 4 with MethodDeclarationMatch

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);
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) ISourceRange(org.eclipse.jdt.core.ISourceRange) SourceRange(org.eclipse.jdt.core.SourceRange) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) FieldDeclarationMatch(org.eclipse.jdt.core.search.FieldDeclarationMatch) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 5 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)

Aggregations

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