Search in sources :

Example 6 with SearchParticipant

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

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

the class IndexManager method addBinary.

/**
     * Trigger addition of a resource to an index
     * Note: the actual operation is performed in background
     */
public void addBinary(IFile resource, IPath containerPath) {
    //        if (JavaCore.getPlugin() == null) return;
    SearchParticipant participant = SearchEngine.getDefaultSearchParticipant();
    SearchDocument document = participant.getDocument(resource.getFullPath().toString());
    IndexLocation indexLocation = computeIndexLocation(containerPath);
    scheduleDocumentIndexing(document, containerPath, indexLocation, participant);
}
Also used : FileIndexLocation(org.eclipse.jdt.internal.core.index.FileIndexLocation) IndexLocation(org.eclipse.jdt.internal.core.index.IndexLocation) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchDocument(org.eclipse.jdt.core.search.SearchDocument)

Example 8 with SearchParticipant

use of org.eclipse.jdt.core.search.SearchParticipant in project liferay-ide by liferay.

the class PortletURLHyperlinkDetector method _findPortletMethods.

private IMethod[] _findPortletMethods(IDocument document, String nameValue) {
    IMethod[] retval = null;
    IFile file = DOMUtils.getFile(document);
    if ((file != null) && file.exists()) {
        IJavaProject project = JavaCore.create(file.getProject());
        if ((project != null) && project.exists()) {
            try {
                IType portlet = project.findType("javax.portlet.Portlet");
                if (portlet != null) {
                    List<IMethod> methods = new ArrayList<>();
                    SearchRequestor requestor = new ActionMethodCollector(methods);
                    IJavaSearchScope scope = SearchEngine.createStrictHierarchyScope(project, portlet, true, false, null);
                    SearchPattern search = SearchPattern.createPattern(nameValue, IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
                    SearchParticipant[] searchParticipant = { SearchEngine.getDefaultSearchParticipant() };
                    new SearchEngine().search(search, searchParticipant, scope, requestor, new NullProgressMonitor());
                    retval = methods.toArray(new IMethod[0]);
                }
            } catch (JavaModelException jme) {
            } catch (CoreException ce) {
            }
        }
    }
    return retval;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IJavaProject(org.eclipse.jdt.core.IJavaProject) 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) IMethod(org.eclipse.jdt.core.IMethod)

Example 9 with SearchParticipant

use of org.eclipse.jdt.core.search.SearchParticipant in project xtext-eclipse by eclipse.

the class JdtTypeProvider method findSecondaryType.

/**
 * Searches a secondary type with the given name and package.
 *
 * Secondary types are toplevel types with a name that does not match the name of the compilation unit.
 * @since 2.9
 */
protected IType findSecondaryType(String packageName, final String typeName) throws JavaModelException {
    IPackageFragmentRoot[] sourceFolders = getSourceFolders();
    IndexManager indexManager = JavaModelManager.getIndexManager();
    if (indexManager.awaitingJobsCount() > 0) {
        // still indexing - don't enter a busy wait loop but ask the source folders directly
        return findSecondaryTypeInSourceFolders(packageName, typeName, sourceFolders);
    }
    // code below is adapted from BasicSearchEnginge.searchAllSecondaryTypes
    // index is ready, query it for a secondary type
    final TypeDeclarationPattern pattern = new TypeDeclarationPattern(packageName == null ? CharOperation.NO_CHAR : packageName.toCharArray(), // top level type - no enclosing type names
    CharOperation.NO_CHAR_CHAR, typeName.toCharArray(), IIndexConstants.SECONDARY_SUFFIX, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    // Get working copy path(s). Store in a single string in case of only one to optimize comparison in requestor
    final HashSet<String> workingCopyPaths = new HashSet<String>();
    String workingCopyPath = null;
    ICompilationUnit[] copies = getWorkingCopies();
    final int copiesLength = copies == null ? 0 : copies.length;
    if (copies != null) {
        if (copiesLength == 1) {
            ICompilationUnit singleWC = copies[0];
            if (singleWC.getPackageDeclaration(packageName).exists()) {
                IType result = singleWC.getType(typeName);
                if (result.exists()) {
                    return result;
                }
            }
            workingCopyPath = copies[0].getPath().toString();
        } else {
            for (int i = 0; i < copiesLength; i++) {
                ICompilationUnit workingCopy = copies[i];
                if (workingCopy.getPackageDeclaration(packageName).exists()) {
                    IType result = workingCopy.getType(typeName);
                    if (result.exists()) {
                        return result;
                    }
                }
                workingCopyPaths.add(workingCopy.getPath().toString());
            }
        }
    }
    final String singleWkcpPath = workingCopyPath;
    final Wrapper<IType> result = Wrapper.forType(IType.class);
    IndexQueryRequestor searchRequestor = new IndexQueryRequestor() {

        @Override
        public boolean acceptIndexMatch(String documentPath, SearchPattern indexRecord, SearchParticipant participant, AccessRuleSet access) {
            // Filter unexpected types
            switch(copiesLength) {
                case 0:
                    break;
                case 1:
                    if (singleWkcpPath == null) {
                        throw new IllegalStateException();
                    }
                    if (singleWkcpPath.equals(documentPath)) {
                        // filter out *the* working copy
                        return true;
                    }
                    break;
                default:
                    if (workingCopyPaths.contains(documentPath)) {
                        // filter out working copies
                        return true;
                    }
                    break;
            }
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(documentPath));
            ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file);
            IType type = unit.getType(typeName);
            result.set(type);
            return false;
        }
    };
    try {
        indexManager.performConcurrentJob(new PatternSearchJob(pattern, // Java search only
        BasicSearchEngine.getDefaultSearchParticipant(), BasicSearchEngine.createJavaSearchScope(sourceFolders), searchRequestor), IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
    } catch (OperationCanceledException oce) {
    // do nothing
    }
    return result.get();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) PatternSearchJob(org.eclipse.jdt.internal.core.search.PatternSearchJob) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) TypeDeclarationPattern(org.eclipse.jdt.internal.core.search.matching.TypeDeclarationPattern) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IndexQueryRequestor(org.eclipse.jdt.internal.core.search.IndexQueryRequestor) HashSet(java.util.HashSet)

Example 10 with SearchParticipant

use of org.eclipse.jdt.core.search.SearchParticipant in project xtext-eclipse by eclipse.

the class ProjectAwareUniqueClassNameValidator method doCheckUniqueInProject.

public boolean doCheckUniqueInProject(QualifiedName name, JvmDeclaredType type) throws JavaModelException {
    IJavaProject javaProject = javaProjectProvider.getJavaProject(type.eResource().getResourceSet());
    getContext().put(ProjectAwareUniqueClassNameValidator.OUTPUT_CONFIGS, outputConfigurationProvider.getOutputConfigurations(type.eResource()));
    String packageName = type.getPackageName();
    String typeName = type.getSimpleName();
    IndexManager indexManager = JavaModelManager.getIndexManager();
    List<IPackageFragmentRoot> sourceFolders = new ArrayList<>();
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
            sourceFolders.add(root);
        }
    }
    if (sourceFolders.isEmpty() || indexManager.awaitingJobsCount() > 0) {
        // still indexing - don't enter a busy wait loop but ask the source folders directly
        SourceTraversal sourceTraversal = doCheckUniqueInProjectSource(packageName != null ? packageName : "", typeName, type, sourceFolders);
        if (sourceTraversal == SourceTraversal.DUPLICATE) {
            return false;
        } else if (sourceTraversal == SourceTraversal.UNIQUE) {
            return true;
        }
    }
    Set<String> workingCopyPaths = new HashSet<>();
    ICompilationUnit[] copies = getWorkingCopies(type);
    if (copies != null) {
        for (ICompilationUnit workingCopy : copies) {
            IPath path = workingCopy.getPath();
            if (javaProject.getPath().isPrefixOf(path) && !isDerived(workingCopy.getResource())) {
                if (workingCopy.getPackageDeclaration(packageName).exists()) {
                    IType result = workingCopy.getType(typeName);
                    if (result.exists()) {
                        addIssue(type, workingCopy.getElementName());
                        return false;
                    }
                }
                workingCopyPaths.add(workingCopy.getPath().toString());
            }
        }
    }
    // The code below is adapted from BasicSearchEnginge.searchAllSecondaryTypes
    // The Index is ready, query it for a secondary type
    char[] pkg = packageName == null ? CharOperation.NO_CHAR : packageName.toCharArray();
    TypeDeclarationPattern pattern = new // 
    TypeDeclarationPattern(// 
    pkg, // top level type - no enclosing type names
    CharOperation.NO_CHAR_CHAR, // 
    typeName.toCharArray(), // 
    IIndexConstants.TYPE_SUFFIX, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
    IndexQueryRequestor searchRequestor = new IndexQueryRequestor() {

        @Override
        public boolean acceptIndexMatch(String documentPath, SearchPattern indexRecord, SearchParticipant participant, AccessRuleSet access) {
            if (workingCopyPaths.contains(documentPath)) {
                // filter out working copies
                return true;
            }
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(documentPath));
            if (!isDerived(file) && file.exists()) {
                addIssue(type, file.getName());
                return false;
            }
            return true;
        }
    };
    try {
        // Java search only
        SearchParticipant searchParticipant = BasicSearchEngine.getDefaultSearchParticipant();
        IJavaSearchScope javaSearchScope = BasicSearchEngine.createJavaSearchScope(sourceFolders.toArray(new IJavaElement[0]));
        PatternSearchJob patternSearchJob = new PatternSearchJob(pattern, searchParticipant, javaSearchScope, searchRequestor);
        indexManager.performConcurrentJob(patternSearchJob, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
        return true;
    } catch (Throwable OperationCanceledException) {
        return false;
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IType(org.eclipse.jdt.core.IType) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) TypeDeclarationPattern(org.eclipse.jdt.internal.core.search.matching.TypeDeclarationPattern) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IndexQueryRequestor(org.eclipse.jdt.internal.core.search.IndexQueryRequestor) HashSet(java.util.HashSet) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPath(org.eclipse.core.runtime.IPath) PatternSearchJob(org.eclipse.jdt.internal.core.search.PatternSearchJob) AccessRuleSet(org.eclipse.jdt.internal.compiler.env.AccessRuleSet) IndexManager(org.eclipse.jdt.internal.core.search.indexing.IndexManager) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Aggregations

SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)11 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)9 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)7 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)7 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)6 ArrayList (java.util.ArrayList)5 IMethod (org.eclipse.jdt.core.IMethod)5 IType (org.eclipse.jdt.core.IType)5 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)5 HashSet (java.util.HashSet)4 CoreException (org.eclipse.core.runtime.CoreException)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IFile (org.eclipse.core.resources.IFile)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 IPath (org.eclipse.core.runtime.IPath)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Path (org.eclipse.core.runtime.Path)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2