Search in sources :

Example 1 with IndexQueryRequestor

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

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

the class ProjectAwareUniqueClassNameValidator method doCheckUniqueInProject.

public boolean doCheckUniqueInProject(final QualifiedName name, final JvmDeclaredType type) throws JavaModelException {
    final IJavaProject javaProject = this.javaProjectProvider.getJavaProject(type.eResource().getResourceSet());
    this.getContext().put(ProjectAwareUniqueClassNameValidator.OUTPUT_CONFIGS, this.outputConfigurationProvider.getOutputConfigurations(type.eResource()));
    final String packageName = type.getPackageName();
    final String typeName = type.getSimpleName();
    final Function1<IPackageFragmentRoot, Boolean> _function = (IPackageFragmentRoot it) -> {
        try {
            int _kind = it.getKind();
            return Boolean.valueOf((_kind == IPackageFragmentRoot.K_SOURCE));
        } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
        }
    };
    final Iterable<IPackageFragmentRoot> sourceFolders = IterableExtensions.<IPackageFragmentRoot>filter(((Iterable<IPackageFragmentRoot>) Conversions.doWrapArray(javaProject.getPackageFragmentRoots())), _function);
    IndexManager indexManager = JavaModelManager.getIndexManager();
    if (((((Object[]) Conversions.unwrapArray(sourceFolders, Object.class)).length == 0) || (indexManager.awaitingJobsCount() > 0))) {
        String _elvis = null;
        if (packageName != null) {
            _elvis = packageName;
        } else {
            _elvis = "";
        }
        ProjectAwareUniqueClassNameValidator.SourceTraversal _doCheckUniqueInProjectSource = this.doCheckUniqueInProjectSource(_elvis, typeName, type, ((IPackageFragmentRoot[]) Conversions.unwrapArray(sourceFolders, IPackageFragmentRoot.class)));
        if (_doCheckUniqueInProjectSource != null) {
            switch(_doCheckUniqueInProjectSource) {
                case DUPLICATE:
                    return false;
                case UNIQUE:
                    return true;
                default:
                    break;
            }
        } else {
        }
    }
    final HashSet<String> workingCopyPaths = CollectionLiterals.<String>newHashSet();
    ICompilationUnit[] copies = this.getWorkingCopies(type);
    if ((copies != null)) {
        for (final ICompilationUnit workingCopy : copies) {
            {
                final IPath path = workingCopy.getPath();
                if ((javaProject.getPath().isPrefixOf(path) && (!this.isDerived(workingCopy.getResource())))) {
                    boolean _exists = workingCopy.getPackageDeclaration(packageName).exists();
                    if (_exists) {
                        IType result = workingCopy.getType(typeName);
                        boolean _exists_1 = result.exists();
                        if (_exists_1) {
                            this.addIssue(type, workingCopy.getElementName());
                            return false;
                        }
                    }
                    workingCopyPaths.add(workingCopy.getPath().toString());
                }
            }
        }
    }
    char[] _xifexpression = null;
    if ((packageName == null)) {
        _xifexpression = CharOperation.NO_CHAR;
    } else {
        _xifexpression = packageName.toCharArray();
    }
    char[] _charArray = typeName.toCharArray();
    final TypeDeclarationPattern pattern = new TypeDeclarationPattern(_xifexpression, CharOperation.NO_CHAR_CHAR, _charArray, IIndexConstants.TYPE_SUFFIX, (SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE));
    final IndexQueryRequestor _function_1 = new IndexQueryRequestor() {

        @Override
        public boolean acceptIndexMatch(final String documentPath, final SearchPattern indexRecord, final SearchParticipant participant, final AccessRuleSet access) {
            boolean _contains = workingCopyPaths.contains(documentPath);
            if (_contains) {
                return true;
            }
            IWorkspaceRoot _root = ResourcesPlugin.getWorkspace().getRoot();
            Path _path = new Path(documentPath);
            IFile file = _root.getFile(_path);
            boolean _isDerived = ProjectAwareUniqueClassNameValidator.this.isDerived(file);
            boolean _not = (!_isDerived);
            if (_not) {
                ProjectAwareUniqueClassNameValidator.this.addIssue(type, file.getName());
                return false;
            }
            return true;
        }
    };
    IndexQueryRequestor searchRequestor = _function_1;
    try {
        SearchParticipant _defaultSearchParticipant = BasicSearchEngine.getDefaultSearchParticipant();
        IJavaSearchScope _createJavaSearchScope = BasicSearchEngine.createJavaSearchScope(((IJavaElement[]) Conversions.unwrapArray(sourceFolders, IJavaElement.class)));
        PatternSearchJob _patternSearchJob = new PatternSearchJob(pattern, _defaultSearchParticipant, _createJavaSearchScope, searchRequestor);
        indexManager.performConcurrentJob(_patternSearchJob, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
        return true;
    } catch (final Throwable _t) {
        if (_t instanceof OperationCanceledException) {
            return false;
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) 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) 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) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot)

Aggregations

IFile (org.eclipse.core.resources.IFile)2 IPath (org.eclipse.core.runtime.IPath)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 Path (org.eclipse.core.runtime.Path)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)2 IType (org.eclipse.jdt.core.IType)2 SearchParticipant (org.eclipse.jdt.core.search.SearchParticipant)2 SearchPattern (org.eclipse.jdt.core.search.SearchPattern)2 AccessRuleSet (org.eclipse.jdt.internal.compiler.env.AccessRuleSet)2 IndexQueryRequestor (org.eclipse.jdt.internal.core.search.IndexQueryRequestor)2 PatternSearchJob (org.eclipse.jdt.internal.core.search.PatternSearchJob)2 IndexManager (org.eclipse.jdt.internal.core.search.indexing.IndexManager)2 TypeDeclarationPattern (org.eclipse.jdt.internal.core.search.matching.TypeDeclarationPattern)2 HashSet (java.util.HashSet)1 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IJavaSearchScope (org.eclipse.jdt.core.search.IJavaSearchScope)1