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

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

Introduction

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

Prototype

public boolean performConcurrentJob(IJob searchJob, int waitingPolicy, IProgressMonitor monitor) 

Source Link

Document

This API is allowing to run one job in concurrence with background processing.

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.
 * /*w w  w  .j  a va  2  s. co 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

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 {/*from   w ww  .  ja  v  a 2s.  c o  m*/
            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);
        }
    }
}