Search in sources :

Example 11 with SearchEngine

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

the class ReferenceFinderUtil method getFieldReferencesIn.

private static List<SearchMatch> getFieldReferencesIn(IJavaElement element, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException {
    CollectingSearchRequestor requestor = new CollectingSearchRequestor();
    SearchEngine engine = owner != null ? new SearchEngine(owner) : new SearchEngine();
    engine.searchDeclarationsOfAccessedFields(element, requestor, pm);
    return requestor.getResults();
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CollectingSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor)

Example 12 with SearchEngine

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

the class JavaContext method findAllTypes.

/*
	 * Finds a type by the simple name. From AddImportsOperation
	 */
private TypeNameMatch[] findAllTypes(String simpleTypeName, IJavaSearchScope searchScope, SimpleName nameNode, IProgressMonitor monitor, ICompilationUnit cu) throws JavaModelException {
    boolean is50OrHigher = JavaModelUtil.is50OrHigher(cu.getJavaProject());
    int typeKinds = SimilarElementsRequestor.ALL_TYPES;
    if (nameNode != null) {
        typeKinds = ASTResolving.getPossibleTypeKinds(nameNode, is50OrHigher);
    }
    ArrayList<TypeNameMatch> typeInfos = new ArrayList<TypeNameMatch>();
    TypeNameMatchCollector requestor = new TypeNameMatchCollector(typeInfos);
    new SearchEngine().searchAllTypeNames(null, 0, simpleTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE, getSearchForConstant(typeKinds), searchScope, requestor, IJavaSearchConstants.FORCE_IMMEDIATE_SEARCH, monitor);
    ArrayList<TypeNameMatch> typeRefsFound = new ArrayList<TypeNameMatch>(typeInfos.size());
    for (int i = 0, len = typeInfos.size(); i < len; i++) {
        TypeNameMatch curr = typeInfos.get(i);
        if (curr.getPackageName().length() > 0) {
            // do not suggest imports from the default package
            if (isOfKind(curr, typeKinds, is50OrHigher) && isVisible(curr, cu)) {
                typeRefsFound.add(curr);
            }
        }
    }
    return typeRefsFound.toArray(new TypeNameMatch[typeRefsFound.size()]);
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) ArrayList(java.util.ArrayList) TypeNameMatchCollector(org.eclipse.jdt.internal.corext.util.TypeNameMatchCollector)

Example 13 with SearchEngine

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

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

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

the class JavaSearchQuery method run.

public IStatus run(IProgressMonitor monitor) {
    final JavaSearchResult textResult = (JavaSearchResult) getSearchResult();
    textResult.removeAll();
    // Don't need to pass in working copies in 3.0 here
    SearchEngine engine = new SearchEngine();
    try {
        int totalTicks = 1000;
        IProject[] projects = JavaSearchScopeFactory.getInstance().getProjects(fPatternData.getScope());
        final SearchParticipantRecord[] participantDescriptors = SearchParticipantsExtensionPoint.getInstance().getSearchParticipants(projects);
        final int[] ticks = new int[participantDescriptors.length];
        for (int i = 0; i < participantDescriptors.length; i++) {
            final int iPrime = i;
            ISafeRunnable runnable = new ISafeRunnable() {

                public void handleException(Throwable exception) {
                    ticks[iPrime] = 0;
                    String message = SearchMessages.JavaSearchQuery_error_participant_estimate;
                    JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, exception));
                }

                public void run() throws Exception {
                    ticks[iPrime] = participantDescriptors[iPrime].getParticipant().estimateTicks(fPatternData);
                }
            };
            SafeRunner.run(runnable);
            totalTicks += ticks[i];
        }
        SearchPattern pattern;
        String stringPattern;
        if (fPatternData instanceof ElementQuerySpecification) {
            IJavaElement element = ((ElementQuerySpecification) fPatternData).getElement();
            stringPattern = JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT);
            if (!element.exists()) {
                return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(SearchMessages.JavaSearchQuery_error_element_does_not_exist, stringPattern), null);
            }
            pattern = SearchPattern.createPattern(element, fPatternData.getLimitTo(), SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
        } else {
            PatternQuerySpecification patternSpec = (PatternQuerySpecification) fPatternData;
            stringPattern = patternSpec.getPattern();
            int matchMode = getMatchMode(stringPattern) | SearchPattern.R_ERASURE_MATCH;
            if (patternSpec.isCaseSensitive())
                matchMode |= SearchPattern.R_CASE_SENSITIVE;
            pattern = SearchPattern.createPattern(patternSpec.getPattern(), patternSpec.getSearchFor(), patternSpec.getLimitTo(), matchMode);
        }
        if (pattern == null) {
            return new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, Messages.format(SearchMessages.JavaSearchQuery_error_unsupported_pattern, stringPattern), null);
        }
        monitor.beginTask(Messages.format(SearchMessages.JavaSearchQuery_task_label, stringPattern), totalTicks);
        IProgressMonitor mainSearchPM = new SubProgressMonitor(monitor, 1000);
        boolean ignorePotentials = NewSearchUI.arePotentialMatchesIgnored();
        NewSearchResultCollector collector = new NewSearchResultCollector(textResult, ignorePotentials);
        engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, fPatternData.getScope(), collector, mainSearchPM);
        for (int i = 0; i < participantDescriptors.length; i++) {
            final ISearchRequestor requestor = new SearchRequestor(participantDescriptors[i].getParticipant(), textResult);
            final IProgressMonitor participantPM = new SubProgressMonitor(monitor, ticks[i]);
            final int iPrime = i;
            ISafeRunnable runnable = new ISafeRunnable() {

                public void handleException(Throwable exception) {
                    participantDescriptors[iPrime].getDescriptor().disable();
                    String message = SearchMessages.JavaSearchQuery_error_participant_search;
                    JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, message, exception));
                }

                public void run() throws Exception {
                    final IQueryParticipant participant = participantDescriptors[iPrime].getParticipant();
                    final PerformanceStats stats = PerformanceStats.getStats(PERF_SEARCH_PARTICIPANT, participant);
                    stats.startRun();
                    participant.search(requestor, fPatternData, participantPM);
                    stats.endRun();
                }
            };
            SafeRunner.run(runnable);
        }
    } catch (CoreException e) {
        return e.getStatus();
    }
    String message = Messages.format(SearchMessages.JavaSearchQuery_status_ok_message, String.valueOf(textResult.getMatchCount()));
    return new Status(IStatus.OK, JavaPlugin.getPluginId(), 0, message, null);
}
Also used : SearchEngine(org.eclipse.jdt.core.search.SearchEngine) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IJavaElement(org.eclipse.jdt.core.IJavaElement) IQueryParticipant(org.eclipse.jdt.ui.search.IQueryParticipant) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) PatternQuerySpecification(org.eclipse.jdt.ui.search.PatternQuerySpecification) IProject(org.eclipse.core.resources.IProject) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ISearchRequestor(org.eclipse.jdt.ui.search.ISearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ISearchRequestor(org.eclipse.jdt.ui.search.ISearchRequestor) PerformanceStats(org.eclipse.core.runtime.PerformanceStats) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)21 CoreException (org.eclipse.core.runtime.CoreException)11 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)10 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)9 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)8 ArrayList (java.util.ArrayList)7 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 JavaModelException (org.eclipse.jdt.core.JavaModelException)7 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)6 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)5 IJavaElement (org.eclipse.jdt.core.IJavaElement)4 RefactoringSearchEngine (org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine)4 HashSet (java.util.HashSet)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IType (org.eclipse.jdt.core.IType)3 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)3 CollectingSearchRequestor (org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 IMethod (org.eclipse.jdt.core.IMethod)2