Example usage for org.eclipse.jdt.internal.core.search.indexing IndexManager awaitingJobsCount

List of usage examples for org.eclipse.jdt.internal.core.search.indexing IndexManager awaitingJobsCount

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.search.indexing IndexManager awaitingJobsCount.

Prototype

public synchronized int awaitingJobsCount() 

Source Link

Document

Answer the amount of awaiting jobs.

Usage

From source file:org.eclipse.xtext.common.types.access.jdt.JdtTypeProvider.java

License:Open Source License

/**
 * Searches a secondary type with the given name and package.
 * /*from   w ww. ja v  a 2s .c  o  m*/
 * 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(), CharOperation.NO_CHAR_CHAR, // top level type - no enclosing type names
            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)) {
                    return true; // filter out *the* working copy
                }
                break;
            default:
                if (workingCopyPaths.contains(documentPath)) {
                    return true; // filter out working copies
                }
                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, BasicSearchEngine.getDefaultSearchParticipant(), // Java search only
                        BasicSearchEngine.createJavaSearchScope(sourceFolders), searchRequestor),
                IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, null);
    } catch (OperationCanceledException oce) {
        // do nothing
    }
    return result.get();
}

From source file:org.eclipse.xtext.xbase.ui.validation.ProjectAwareUniqueClassNameValidator.java

License:Open Source License

private ProjectAwareUniqueClassNameValidator.SourceTraversal doCheckUniqueInProjectSource(
        final String packageName, final String typeName, final JvmDeclaredType type,
        final IPackageFragmentRoot[] sourceFolders) throws JavaModelException {
    final IndexManager indexManager = JavaModelManager.getIndexManager();
    for (final IPackageFragmentRoot sourceFolder : sourceFolders) {
        int _awaitingJobsCount = indexManager.awaitingJobsCount();
        boolean _greaterThan = (_awaitingJobsCount > 0);
        if (_greaterThan) {
            boolean _isDerived = this.isDerived(sourceFolder.getResource());
            boolean _not = (!_isDerived);
            if (_not) {
                IPackageFragment packageFragment = sourceFolder.getPackageFragment(packageName);
                boolean _exists = packageFragment.exists();
                if (_exists) {
                    ICompilationUnit[] units = packageFragment.getCompilationUnits();
                    for (final ICompilationUnit unit : units) {
                        {/*from   w  w  w  .  ja va 2 s.  c o  m*/
                            final IResource resource = unit.getResource();
                            boolean _isDerived_1 = this.isDerived(resource);
                            boolean _not_1 = (!_isDerived_1);
                            if (_not_1) {
                                IType javaType = unit.getType(typeName);
                                boolean _exists_1 = javaType.exists();
                                if (_exists_1) {
                                    this.addIssue(type, unit.getElementName());
                                    return ProjectAwareUniqueClassNameValidator.SourceTraversal.DUPLICATE;
                                }
                            }
                        }
                    }
                }
            }
        } else {
            return ProjectAwareUniqueClassNameValidator.SourceTraversal.INTERRUPT;
        }
    }
    return ProjectAwareUniqueClassNameValidator.SourceTraversal.UNIQUE;
}

From source file:org.eclipse.xtext.xbase.ui.validation.ProjectAwareUniqueClassNameValidator.java

License:Open Source License

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 {//ww w .jav a2  s. c  om
            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) {
            final OperationCanceledException oce = (OperationCanceledException) _t;
            return false;
        } else {
            throw Exceptions.sneakyThrow(_t);
        }
    }
}