Search in sources :

Example 1 with MethodReferenceMatch

use of org.eclipse.jdt.core.search.MethodReferenceMatch 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 2 with MethodReferenceMatch

use of org.eclipse.jdt.core.search.MethodReferenceMatch in project che by eclipse.

the class NewSearchResultCollector method acceptSearchMatch.

@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
    IJavaElement enclosingElement = (IJavaElement) match.getElement();
    if (enclosingElement != null) {
        if (fIgnorePotentials && (match.getAccuracy() == SearchMatch.A_INACCURATE))
            return;
        boolean isWriteAccess = false;
        boolean isReadAccess = false;
        if (match instanceof FieldReferenceMatch) {
            FieldReferenceMatch fieldRef = ((FieldReferenceMatch) match);
            isWriteAccess = fieldRef.isWriteAccess();
            isReadAccess = fieldRef.isReadAccess();
        } else if (match instanceof FieldDeclarationMatch) {
            isWriteAccess = true;
        } else if (match instanceof LocalVariableReferenceMatch) {
            LocalVariableReferenceMatch localVarRef = ((LocalVariableReferenceMatch) match);
            isWriteAccess = localVarRef.isWriteAccess();
            isReadAccess = localVarRef.isReadAccess();
        } else if (match instanceof LocalVariableDeclarationMatch) {
            isWriteAccess = true;
        }
        boolean isSuperInvocation = false;
        if (match instanceof MethodReferenceMatch) {
            MethodReferenceMatch methodRef = (MethodReferenceMatch) match;
            isSuperInvocation = methodRef.isSuperInvocation();
        }
        fSearch.addMatch(new JavaElementMatch(enclosingElement, match.getRule(), match.getOffset(), match.getLength(), match.getAccuracy(), isReadAccess, isWriteAccess, match.isInsideDocComment(), isSuperInvocation));
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) MethodReferenceMatch(org.eclipse.jdt.core.search.MethodReferenceMatch) FieldReferenceMatch(org.eclipse.jdt.core.search.FieldReferenceMatch) LocalVariableDeclarationMatch(org.eclipse.jdt.core.search.LocalVariableDeclarationMatch) LocalVariableReferenceMatch(org.eclipse.jdt.core.search.LocalVariableReferenceMatch) FieldDeclarationMatch(org.eclipse.jdt.core.search.FieldDeclarationMatch)

Example 3 with MethodReferenceMatch

use of org.eclipse.jdt.core.search.MethodReferenceMatch 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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) JavaModelException(org.eclipse.jdt.core.JavaModelException) MethodReferenceMatch(org.eclipse.jdt.core.search.MethodReferenceMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) CuCollectingSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.CuCollectingSearchRequestor) CoreException(org.eclipse.core.runtime.CoreException) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 4 with MethodReferenceMatch

use of org.eclipse.jdt.core.search.MethodReferenceMatch 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)

Aggregations

MethodReferenceMatch (org.eclipse.jdt.core.search.MethodReferenceMatch)4 IMethod (org.eclipse.jdt.core.IMethod)2 IScanner (org.eclipse.jdt.core.compiler.IScanner)2 InvalidInputException (org.eclipse.jdt.core.compiler.InvalidInputException)2 MethodDeclarationMatch (org.eclipse.jdt.core.search.MethodDeclarationMatch)2 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 FieldDeclarationMatch (org.eclipse.jdt.core.search.FieldDeclarationMatch)1 FieldReferenceMatch (org.eclipse.jdt.core.search.FieldReferenceMatch)1 LocalVariableDeclarationMatch (org.eclipse.jdt.core.search.LocalVariableDeclarationMatch)1 LocalVariableReferenceMatch (org.eclipse.jdt.core.search.LocalVariableReferenceMatch)1 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)1 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)1 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)1 CuCollectingSearchRequestor (org.eclipse.jdt.internal.corext.refactoring.CuCollectingSearchRequestor)1 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1