Search in sources :

Example 16 with SearchPattern

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

Example 17 with SearchPattern

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

the class MoveCuUpdateCreator method getReferences.

private static SearchResultGroup[] getReferences(ICompilationUnit unit, IProgressMonitor pm, RefactoringStatus status) throws CoreException {
    final SearchPattern pattern = RefactoringSearchEngine.createOrPattern(unit.getTypes(), IJavaSearchConstants.REFERENCES);
    if (pattern != null) {
        String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getFileName(unit));
        ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
        Collector requestor = new Collector(((IPackageFragment) unit.getParent()), binaryRefs);
        IJavaSearchScope scope = RefactoringScopeFactory.create(unit, true, false);
        SearchResultGroup[] result = RefactoringSearchEngine.search(pattern, scope, requestor, new SubProgressMonitor(pm, 1), status);
        binaryRefs.addErrorIfNecessary(status);
        return result;
    }
    return new SearchResultGroup[] {};
}
Also used : ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 18 with SearchPattern

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

use of org.eclipse.jdt.core.search.SearchPattern in project tdi-studio-se by Talend.

the class OpenDeclarationAction method doSearchSource.

/**
     * Searches the source for the given class name.
     * 
     * @param name The class name
     * @return The source
     * @throws CoreException
     */
IType doSearchSource(String name) throws CoreException {
    final List<IType> results = new ArrayList<IType>();
    // create requester
    SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            Object element = match.getElement();
            if (element instanceof IType) {
                results.add((IType) element);
            }
        }
    };
    String baseName = name.replace('$', '.');
    // create search engine and pattern
    SearchEngine engine = new SearchEngine();
    SearchPattern pattern = SearchPattern.createPattern(baseName, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
    // search the source for the given name
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(), requestor, null);
    if (results.size() > 0) {
        // at most one source should be found
        return results.get(0);
    }
    return null;
}
Also used : SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ArrayList(java.util.ArrayList) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IType(org.eclipse.jdt.core.IType)

Example 20 with SearchPattern

use of org.eclipse.jdt.core.search.SearchPattern in project bndtools by bndtools.

the class PkgPatternsProposalProvider method doGenerateProposals.

@Override
protected Collection<? extends IContentProposal> doGenerateProposals(String contents, int position) {
    String prefix = contents.substring(0, position);
    final int replaceFromPos;
    if (prefix.startsWith("!")) {
        //$NON-NLS-1$
        prefix = prefix.substring(1);
        replaceFromPos = 1;
    } else {
        replaceFromPos = 0;
    }
    Comparator<PkgPatternProposal> comparator = new Comparator<PkgPatternProposal>() {

        public int compare(PkgPatternProposal o1, PkgPatternProposal o2) {
            int result = o1.getPackageFragment().getElementName().compareTo(o2.getPackageFragment().getElementName());
            if (result == 0) {
                result = Boolean.valueOf(o1.isWildcard()).compareTo(Boolean.valueOf(o2.isWildcard()));
            }
            return result;
        }
    };
    final TreeSet<PkgPatternProposal> result = new TreeSet<PkgPatternProposal>(comparator);
    final IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { searchContext.getJavaProject() });
    final SearchPattern pattern = SearchPattern.createPattern("*" + prefix + "*", IJavaSearchConstants.PACKAGE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
    final SearchRequestor requestor = new SearchRequestor() {

        @Override
        public void acceptSearchMatch(SearchMatch match) throws CoreException {
            IPackageFragment pkg = (IPackageFragment) match.getElement();
            // "java." since these cannot be imported
            if (pkg.isDefaultPackage() || pkg.getElementName().startsWith("java."))
                return;
            result.add(new PkgPatternProposal(pkg, false, replaceFromPos));
            result.add(new PkgPatternProposal(pkg, true, replaceFromPos));
        }
    };
    IRunnableWithProgress runnable = new IRunnableWithProgress() {

        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
            try {
                new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor);
            } catch (CoreException e) {
                throw new InvocationTargetException(e);
            }
        }
    };
    try {
        IRunnableContext runContext = searchContext.getRunContext();
        if (runContext != null) {
            runContext.run(false, false, runnable);
        } else {
            runnable.run(new NullProgressMonitor());
        }
        return result;
    } catch (InvocationTargetException e) {
        logger.logError("Error searching for packages.", e);
        return Collections.emptyList();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return Collections.emptyList();
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InvocationTargetException(java.lang.reflect.InvocationTargetException) Comparator(java.util.Comparator) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) TreeSet(java.util.TreeSet) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Aggregations

SearchPattern (org.eclipse.jdt.core.search.SearchPattern)21 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)13 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)12 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)9 CoreException (org.eclipse.core.runtime.CoreException)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)7 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)6 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)6 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)5 ArrayList (java.util.ArrayList)4 IMethod (org.eclipse.jdt.core.IMethod)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 HashSet (java.util.HashSet)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 IType (org.eclipse.jdt.core.IType)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 IField (org.eclipse.jdt.core.IField)2