Search in sources :

Example 66 with SearchEngine

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

the class HoverInfoProvider method isResolved.

private boolean isResolved(IJavaElement element, IProgressMonitor monitor) throws CoreException {
    if (!(unit instanceof ICompilationUnit)) {
        return true;
    }
    if (element == null) {
        return false;
    }
    if (element.getElementType() != IJavaElement.TYPE) {
        return true;
    }
    if (unit.getResource() != null && !unit.getResource().exists()) {
        return true;
    }
    SearchPattern pattern = SearchPattern.createPattern(element, IJavaSearchConstants.ALL_OCCURRENCES);
    final boolean[] res = new boolean[1];
    res[0] = false;
    SearchEngine engine = new SearchEngine();
    IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { unit }, IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES);
    try {
        engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, new SearchRequestor() {

            @Override
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                if (match.getAccuracy() == SearchMatch.A_INACCURATE) {
                    return;
                }
                Object o = match.getElement();
                if (o instanceof IJavaElement) {
                    IJavaElement element = (IJavaElement) o;
                    if (element.getElementType() == IJavaElement.TYPE) {
                        res[0] = true;
                        return;
                    }
                    ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
                    if (compilationUnit == null) {
                        return;
                    }
                    res[0] = true;
                    throw new HoverException();
                }
            }
        }, monitor);
    } catch (HoverException | OperationCanceledException e) {
    // ignore
    }
    return res[0];
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern)

Example 67 with SearchEngine

use of org.eclipse.jdt.core.search.SearchEngine in project egradle by de-jcup.

the class JDTDataAccess method findType.

/**
 * Find type
 *
 * @param className
 * @param monitor
 * @return type or <code>null</code>
 */
public IType findType(String className, IProgressMonitor monitor) {
    final IType[] result = { null };
    TypeNameMatchRequestor nameMatchRequestor = new TypeNameMatchRequestor() {

        @Override
        public void acceptTypeNameMatch(TypeNameMatch match) {
            result[0] = match.getType();
        }
    };
    int lastDot = className.lastIndexOf('.');
    char[] packageName = lastDot >= 0 ? className.substring(0, lastDot).toCharArray() : null;
    char[] typeName = (lastDot >= 0 ? className.substring(lastDot + 1) : className).toCharArray();
    SearchEngine engine = new SearchEngine();
    int packageMatchRule = SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE;
    try {
        engine.searchAllTypeNames(packageName, packageMatchRule, typeName, packageMatchRule, IJavaSearchConstants.TYPE, SearchEngine.createWorkspaceScope(), nameMatchRequestor, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, monitor);
    } catch (JavaModelException e) {
        EditorUtil.INSTANCE.logError("Was not able to search all type names", e);
    }
    return result[0];
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) TypeNameMatch(org.eclipse.jdt.core.search.TypeNameMatch) TypeNameMatchRequestor(org.eclipse.jdt.core.search.TypeNameMatchRequestor) IType(org.eclipse.jdt.core.IType)

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

Example 69 with SearchEngine

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

the class RefactoringSearchEngine2 method searchPattern.

/**
	 * Performs the search according to the specified pattern.
	 *
	 * @param monitor the progress monitor, or <code>null</code>
	 * @throws JavaModelException if an error occurs during search
	 */
public final void searchPattern(IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(fPattern);
    if (monitor == null)
        monitor = new NullProgressMonitor();
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_occurrences);
        try {
            SearchEngine engine = null;
            if (fOwner != null)
                engine = new SearchEngine(fOwner);
            else
                engine = new SearchEngine(fWorkingCopies);
            engine.search(fPattern, SearchUtils.getDefaultSearchParticipants(), fScope, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 70 with SearchEngine

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

the class RefactoringSearchEngine2 method searchReferencedFields.

/**
	 * Performs the search of referenced fields.
	 *
	 * @param element the java element whose referenced fields have to be found
	 * @param monitor the progress monitor, or <code>null</code>
	 * @throws JavaModelException if an error occurs during search
	 */
public final void searchReferencedFields(final IJavaElement element, IProgressMonitor monitor) throws JavaModelException {
    Assert.isNotNull(element);
    if (monitor == null)
        monitor = new NullProgressMonitor();
    try {
        //$NON-NLS-1$
        monitor.beginTask("", 1);
        monitor.setTaskName(RefactoringCoreMessages.RefactoringSearchEngine_searching_referenced_fields);
        try {
            SearchEngine engine = null;
            if (fOwner != null)
                engine = new SearchEngine(fOwner);
            else
                engine = new SearchEngine(fWorkingCopies);
            engine.searchDeclarationsOfAccessedFields(element, getCollector(), new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        } catch (CoreException exception) {
            throw new JavaModelException(exception);
        }
    } finally {
        monitor.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Aggregations

SearchEngine (org.eclipse.jdt.core.search.SearchEngine)131 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)73 CoreException (org.eclipse.core.runtime.CoreException)71 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)61 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)61 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)58 ArrayList (java.util.ArrayList)52 JavaModelException (org.eclipse.jdt.core.JavaModelException)48 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)38 IType (org.eclipse.jdt.core.IType)38 IJavaElement (org.eclipse.jdt.core.IJavaElement)37 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)24 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)24 TypeNameMatch (org.eclipse.jdt.core.search.TypeNameMatch)21 HashSet (java.util.HashSet)20 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IMethod (org.eclipse.jdt.core.IMethod)20 TypeNameMatchRequestor (org.eclipse.jdt.core.search.TypeNameMatchRequestor)16 IJavaProject (org.eclipse.jdt.core.IJavaProject)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)12