Search in sources :

Example 1 with LightRef

use of org.jetbrains.jps.backwardRefs.LightRef in project intellij-community by JetBrains.

the class CompilerReferenceReader method getDirectInheritors.

/**
   * @return two maps of classes grouped per file
   *
   * 1st map: inheritors. Can be used without explicit inheritance verification
   * 2nd map: candidates. One need to check that these classes are really direct inheritors
   */
@NotNull
Map<VirtualFile, Object[]> getDirectInheritors(@NotNull LightRef searchElement, @NotNull GlobalSearchScope searchScope, @NotNull GlobalSearchScope dirtyScope, @NotNull FileType fileType, @NotNull CompilerHierarchySearchType searchType) throws StorageException {
    GlobalSearchScope effectiveSearchScope = GlobalSearchScope.notScope(dirtyScope).intersectWith(searchScope);
    LanguageLightRefAdapter adapter = CompilerReferenceServiceImpl.findAdapterForFileType(fileType);
    LOG.assertTrue(adapter != null, "adapter is null for file type: " + fileType);
    Class<? extends LightRef> requiredLightRefClass = searchType.getRequiredClass(adapter);
    Map<VirtualFile, Object[]> candidatesPerFile = new HashMap<>();
    myIndex.get(CompilerIndices.BACK_HIERARCHY).getData(searchElement).forEach((fileId, defs) -> {
        final List<LightRef> requiredCandidates = defs.stream().filter(requiredLightRefClass::isInstance).collect(toList());
        if (requiredCandidates.isEmpty())
            return true;
        final VirtualFile file = findFile(fileId);
        if (file != null && effectiveSearchScope.contains(file)) {
            candidatesPerFile.put(file, searchType.convertToIds(requiredCandidates, myIndex.getByteSeqEum()));
        }
        return true;
    });
    return candidatesPerFile.isEmpty() ? Collections.emptyMap() : candidatesPerFile;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) LightRef(org.jetbrains.jps.backwardRefs.LightRef) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with LightRef

use of org.jetbrains.jps.backwardRefs.LightRef in project intellij-community by JetBrains.

the class CompilerReferenceReader method findReferentFileIds.

@Nullable
TIntHashSet findReferentFileIds(@NotNull LightRef ref, boolean checkBaseClassAmbiguity) throws StorageException {
    LightRef.LightClassHierarchyElementDef hierarchyElement = ref instanceof LightRef.LightClassHierarchyElementDef ? (LightRef.LightClassHierarchyElementDef) ref : ((LightRef.LightMember) ref).getOwner();
    TIntHashSet set = new TIntHashSet();
    final LightRef.NamedLightRef[] hierarchy = getWholeHierarchy(hierarchyElement, checkBaseClassAmbiguity);
    if (hierarchy == null)
        return null;
    for (LightRef.NamedLightRef aClass : hierarchy) {
        final LightRef overriderUsage = ref.override(aClass.getName());
        addUsages(overriderUsage, set);
    }
    return set;
}
Also used : LightRef(org.jetbrains.jps.backwardRefs.LightRef) TIntHashSet(gnu.trove.TIntHashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with LightRef

use of org.jetbrains.jps.backwardRefs.LightRef in project intellij-community by JetBrains.

the class CompilerReferenceReader method getWholeHierarchy.

@Nullable("return null if the class hierarchy contains ambiguous qualified names")
private LightRef.NamedLightRef[] getWholeHierarchy(LightRef.LightClassHierarchyElementDef hierarchyElement, boolean checkBaseClassAmbiguity) throws StorageException {
    Set<LightRef.NamedLightRef> result = new THashSet<>();
    Queue<LightRef.NamedLightRef> q = new Queue<>(10);
    q.addLast(hierarchyElement);
    while (!q.isEmpty()) {
        LightRef.NamedLightRef curClass = q.pullFirst();
        if (result.add(curClass)) {
            if (checkBaseClassAmbiguity || curClass != hierarchyElement) {
                DefCount count = getDefinitionCount(curClass);
                if (count == DefCount.NONE) {
                    //diagnostic
                    String baseHierarchyElement = getNameEnumerator().getName(hierarchyElement.getName());
                    String curHierarchyElement = getNameEnumerator().getName(curClass.getName());
                    LOG.error("Can't get definition files for: " + curHierarchyElement + " base class: " + baseHierarchyElement);
                }
                if (count != DefCount.ONE) {
                    return null;
                }
            }
            myIndex.get(CompilerIndices.BACK_HIERARCHY).getData(curClass).forEach((id, children) -> {
                for (LightRef child : children) {
                    if (child instanceof LightRef.LightClassHierarchyElementDef) {
                        q.addLast((LightRef.LightClassHierarchyElementDef) child);
                    }
                }
                return true;
            });
        }
    }
    return result.toArray(new LightRef.NamedLightRef[result.size()]);
}
Also used : LightRef(org.jetbrains.jps.backwardRefs.LightRef) Queue(com.intellij.util.containers.Queue) THashSet(gnu.trove.THashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with LightRef

use of org.jetbrains.jps.backwardRefs.LightRef in project intellij-community by JetBrains.

the class CompilerReferenceServiceImpl method getReferentFileIds.

@Nullable
private TIntHashSet getReferentFileIds(@NotNull PsiElement element) {
    final CompilerElementInfo compilerElementInfo = asCompilerElements(element, true);
    if (compilerElementInfo == null)
        return null;
    myReadDataLock.lock();
    try {
        if (myReader == null)
            return null;
        TIntHashSet referentFileIds = new TIntHashSet();
        for (LightRef ref : compilerElementInfo.searchElements) {
            try {
                final TIntHashSet referents = myReader.findReferentFileIds(ref, compilerElementInfo.place == ElementPlace.SRC);
                if (referents == null)
                    return null;
                referentFileIds.addAll(referents.toArray());
            } catch (StorageException e) {
                throw new RuntimeException(e);
            }
        }
        return referentFileIds;
    } finally {
        myReadDataLock.unlock();
    }
}
Also used : LightRef(org.jetbrains.jps.backwardRefs.LightRef) StorageException(com.intellij.util.indexing.StorageException) TIntHashSet(gnu.trove.TIntHashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with LightRef

use of org.jetbrains.jps.backwardRefs.LightRef in project intellij-community by JetBrains.

the class CompilerReferenceServiceImpl method asCompilerElements.

@Nullable
private CompilerElementInfo asCompilerElements(@NotNull PsiElement psiElement, boolean buildHierarchyForLibraryElements) {
    myReadDataLock.lock();
    try {
        if (myReader == null)
            return null;
        VirtualFile file = PsiUtilCore.getVirtualFile(psiElement);
        if (file == null)
            return null;
        ElementPlace place = ElementPlace.get(file, myProjectFileIndex);
        if (place == null || (place == ElementPlace.SRC && myDirtyScopeHolder.contains(file))) {
            return null;
        }
        final LanguageLightRefAdapter adapter = findAdapterForFileType(file.getFileType());
        if (adapter == null)
            return null;
        final LightRef ref = adapter.asLightUsage(psiElement, myReader.getNameEnumerator());
        if (ref == null)
            return null;
        if (place == ElementPlace.LIB && buildHierarchyForLibraryElements) {
            final List<LightRef> elements = adapter.getHierarchyRestrictedToLibraryScope(ref, psiElement, myReader.getNameEnumerator(), LibraryScopeCache.getInstance(myProject).getLibrariesOnlyScope());
            final LightRef[] fullHierarchy = new LightRef[elements.size() + 1];
            fullHierarchy[0] = ref;
            int i = 1;
            for (LightRef element : elements) {
                fullHierarchy[i++] = element;
            }
            return new CompilerElementInfo(place, fullHierarchy);
        } else {
            return new CompilerElementInfo(place, ref);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        myReadDataLock.unlock();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) LightRef(org.jetbrains.jps.backwardRefs.LightRef) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LightRef (org.jetbrains.jps.backwardRefs.LightRef)6 Nullable (org.jetbrains.annotations.Nullable)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 StorageException (com.intellij.util.indexing.StorageException)2 TIntHashSet (gnu.trove.TIntHashSet)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 Queue (com.intellij.util.containers.Queue)1 THashSet (gnu.trove.THashSet)1 IOException (java.io.IOException)1 NotNull (org.jetbrains.annotations.NotNull)1