Search in sources :

Example 1 with SearchRequestor

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

the class RenameMethodProcessor method searchForDeclarationsOfClashingMethods.

private IMethod[] searchForDeclarationsOfClashingMethods(IProgressMonitor pm) throws CoreException {
    final List<IMethod> results = new ArrayList<IMethod>();
    SearchPattern pattern = createNewMethodPattern();
    IJavaSearchScope scope = RefactoringScopeFactory.create(getMethod().getJavaProject());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object method = match.getElement();
            if (// check for bug 90138: [refactoring] [rename] Renaming method throws internal exception
            method instanceof IMethod)
                results.add((IMethod) method);
            else
                //$NON-NLS-1$
                JavaPlugin.logErrorMessage("Unexpected element in search match: " + match.toString());
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return results.toArray(new IMethod[results.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) ArrayList(java.util.ArrayList) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod)

Example 2 with SearchRequestor

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

the class RefactoringSearchEngine method findAffectedCompilationUnits.

//TODO: throw CoreException
public static ICompilationUnit[] findAffectedCompilationUnits(SearchPattern pattern, IJavaSearchScope scope, final IProgressMonitor pm, RefactoringStatus status, final boolean tolerateInAccurateMatches) throws JavaModelException {
    boolean hasNonCuMatches = false;
    class ResourceSearchRequestor extends SearchRequestor {

        boolean hasPotentialMatches = false;

        Set<IResource> resources = new HashSet<IResource>(5);

        private IResource fLastResource;

        @Override
        public void acceptSearchMatch(SearchMatch match) {
            if (!tolerateInAccurateMatches && match.getAccuracy() == SearchMatch.A_INACCURATE) {
                hasPotentialMatches = true;
            }
            if (fLastResource != match.getResource()) {
                fLastResource = match.getResource();
                resources.add(fLastResource);
            }
        }
    }
    ResourceSearchRequestor requestor = new ResourceSearchRequestor();
    try {
        new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    } catch (CoreException e) {
        throw new JavaModelException(e);
    }
    List<IJavaElement> result = new ArrayList<IJavaElement>(requestor.resources.size());
    for (Iterator<IResource> iter = requestor.resources.iterator(); iter.hasNext(); ) {
        IResource resource = iter.next();
        IJavaElement element = JavaCore.create(resource);
        if (element instanceof ICompilationUnit) {
            result.add(element);
        } else {
            hasNonCuMatches = true;
        }
    }
    addStatusErrors(status, requestor.hasPotentialMatches, hasNonCuMatches);
    return result.toArray(new ICompilationUnit[result.size()]);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) JavaModelException(org.eclipse.jdt.core.JavaModelException) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IResource(org.eclipse.core.resources.IResource)

Example 3 with SearchRequestor

use of org.eclipse.jdt.core.search.SearchRequestor in project AutoRefactor by JnRouvignac.

the class SuperCallRatherThanUselessOverridingRefactoring method isMethodUsedInItsPackage.

/** This method is extremely expensive. */
@OnEclipseVersionUpgrade("Replace monitor.newChild(1) by monitor.split(1)")
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);
        }
    };
    final SubMonitor subMonitor = SubMonitor.convert(ctx.getProgressMonitor(), 1);
    final SubMonitor childMonitor = subMonitor.newChild(1);
    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, childMonitor);
        return methodIsUsedInPackage.get();
    } catch (CoreException e) {
        throw new UnhandledException(node, e);
    } finally {
        childMonitor.done();
    }
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) UnhandledException(org.autorefactor.util.UnhandledException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubMonitor(org.eclipse.core.runtime.SubMonitor) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) OnEclipseVersionUpgrade(org.autorefactor.util.OnEclipseVersionUpgrade)

Example 4 with SearchRequestor

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

the class RenameMethodProcessor method searchForOuterTypesOfReferences.

private IType[] searchForOuterTypesOfReferences(IMethod[] newNameMethods, IProgressMonitor pm) throws CoreException {
    final Set<IType> outerTypesOfReferences = new HashSet<IType>();
    SearchPattern pattern = RefactoringSearchEngine.createOrPattern(newNameMethods, IJavaSearchConstants.REFERENCES);
    IJavaSearchScope scope = createRefactoringScope(getMethod());
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (!(element instanceof IMember))
                // e.g. an IImportDeclaration for a static method import
                return;
            IMember member = (IMember) element;
            IType declaring = member.getDeclaringType();
            if (declaring == null)
                return;
            IType outer = declaring.getDeclaringType();
            if (outer != null)
                outerTypesOfReferences.add(declaring);
        }
    };
    new SearchEngine().search(pattern, SearchUtils.getDefaultSearchParticipants(), scope, requestor, pm);
    return outerTypesOfReferences.toArray(new IType[outerTypesOfReferences.size()]);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) RefactoringSearchEngine(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMember(org.eclipse.jdt.core.IMember) HashSet(java.util.HashSet) IType(org.eclipse.jdt.core.IType)

Example 5 with SearchRequestor

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

the class RippleMethodFinder2 method findAllDeclarations.

private void findAllDeclarations(IProgressMonitor monitor, WorkingCopyOwner owner) throws CoreException {
    fDeclarations = new ArrayList<IMethod>();
    class MethodRequestor extends SearchRequestor {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IMethod method = (IMethod) match.getElement();
            boolean isBinary = method.isBinary();
            if (fBinaryRefs != null || !(fExcludeBinaries && isBinary)) {
                fDeclarations.add(method);
            }
            if (isBinary && fBinaryRefs != null) {
                fDeclarationToMatch.put(method, match);
            }
        }
    }
    int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
    int matchRule = SearchPattern.R_ERASURE_MATCH | SearchPattern.R_CASE_SENSITIVE;
    SearchPattern pattern = SearchPattern.createPattern(fMethod, limitTo, matchRule);
    SearchParticipant[] participants = SearchUtils.getDefaultSearchParticipants();
    IJavaSearchScope scope = RefactoringScopeFactory.createRelatedProjectsScope(fMethod.getJavaProject(), IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES);
    MethodRequestor requestor = new MethodRequestor();
    SearchEngine searchEngine = owner != null ? new SearchEngine(owner) : new SearchEngine();
    searchEngine.search(pattern, participants, scope, requestor, monitor);
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)8 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)8 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)8 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)6 CoreException (org.eclipse.core.runtime.CoreException)4 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)4 ArrayList (java.util.ArrayList)3 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashSet (java.util.HashSet)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IMethod (org.eclipse.jdt.core.IMethod)2 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)2 IType (org.eclipse.jdt.core.IType)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)2 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)2 Comparator (java.util.Comparator)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1