Example usage for org.eclipse.jdt.internal.core.search.indexing IIndexConstants SECONDARY_SUFFIX

List of usage examples for org.eclipse.jdt.internal.core.search.indexing IIndexConstants SECONDARY_SUFFIX

Introduction

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

Prototype

char SECONDARY_SUFFIX

To view the source code for org.eclipse.jdt.internal.core.search.indexing IIndexConstants SECONDARY_SUFFIX.

Click Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.SecondaryTypeDeclarationPattern.java

License:Open Source License

public SecondaryTypeDeclarationPattern() {
    super(null, null, null, IIndexConstants.SECONDARY_SUFFIX, R_EXACT_MATCH | R_CASE_SENSITIVE);
}

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.  j  a va2s .  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();
}