Search in sources :

Example 1 with ContainerUtil

use of com.intellij.util.containers.ContainerUtil in project intellij-community by JetBrains.

the class JavaModuleGraphUtil method findCycles.

// Looks for cycles between Java modules in the project sources.
// Library/JDK modules are excluded - in assumption there can't be any lib -> src dependencies.
// Module references are resolved "globally" (i.e., without taking project dependencies into account).
private static List<Set<PsiJavaModule>> findCycles(Project project) {
    Set<PsiJavaModule> projectModules = ContainerUtil.newHashSet();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, MODULE_INFO_FILE, module.getModuleScope(false));
        // aborts the process when there are incorrect modules in the project
        if (files.size() > 1)
            return Collections.emptyList();
        Optional.ofNullable(ContainerUtil.getFirstItem(files)).map(PsiManager.getInstance(project)::findFile).map(f -> f instanceof PsiJavaFile ? ((PsiJavaFile) f).getModuleDeclaration() : null).ifPresent(projectModules::add);
    }
    if (!projectModules.isEmpty()) {
        MultiMap<PsiJavaModule, PsiJavaModule> relations = MultiMap.create();
        for (PsiJavaModule module : projectModules) {
            for (PsiRequiresStatement statement : module.getRequires()) {
                PsiJavaModule dependency = PsiJavaModuleReference.resolve(statement, statement.getModuleName(), true);
                if (dependency != null && projectModules.contains(dependency)) {
                    relations.putValue(module, dependency);
                }
            }
        }
        if (!relations.isEmpty()) {
            Graph<PsiJavaModule> graph = new ChameleonGraph<>(relations, false);
            DFSTBuilder<PsiJavaModule> builder = new DFSTBuilder<>(graph);
            Collection<Collection<PsiJavaModule>> components = builder.getComponents();
            if (!components.isEmpty()) {
                return components.stream().map(ContainerUtil::newLinkedHashSet).collect(Collectors.toList());
            }
        }
    }
    return Collections.emptyList();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Trinity(com.intellij.openapi.util.Trinity) java.util(java.util) ModuleManager(com.intellij.openapi.module.ModuleManager) VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilenameIndex(com.intellij.psi.search.FilenameIndex) BiFunction(java.util.function.BiFunction) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) THashSet(gnu.trove.THashSet) OUT_OF_CODE_BLOCK_MODIFICATION_COUNT(com.intellij.psi.util.PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT) ContainerUtil(com.intellij.util.containers.ContainerUtil) Collectors(java.util.stream.Collectors) MODULE_INFO_FILE(com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE) CachedValuesManager(com.intellij.psi.util.CachedValuesManager) Graph(com.intellij.util.graph.Graph) LightJavaModule(com.intellij.psi.impl.light.LightJavaModule) Nullable(org.jetbrains.annotations.Nullable) PsiJavaModuleReference(com.intellij.psi.impl.source.PsiJavaModuleReference) GraphGenerator(com.intellij.util.graph.GraphGenerator) Project(com.intellij.openapi.project.Project) Result(com.intellij.psi.util.CachedValueProvider.Result) com.intellij.psi(com.intellij.psi) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) MultiMap(com.intellij.util.containers.MultiMap) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) LightJavaModule(com.intellij.psi.impl.light.LightJavaModule) Module(com.intellij.openapi.module.Module)

Aggregations

Module (com.intellij.openapi.module.Module)1 ModuleManager (com.intellij.openapi.module.ModuleManager)1 Project (com.intellij.openapi.project.Project)1 Trinity (com.intellij.openapi.util.Trinity)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 com.intellij.psi (com.intellij.psi)1 MODULE_INFO_FILE (com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE)1 LightJavaModule (com.intellij.psi.impl.light.LightJavaModule)1 PsiJavaModuleReference (com.intellij.psi.impl.source.PsiJavaModuleReference)1 FilenameIndex (com.intellij.psi.search.FilenameIndex)1 Result (com.intellij.psi.util.CachedValueProvider.Result)1 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)1 OUT_OF_CODE_BLOCK_MODIFICATION_COUNT (com.intellij.psi.util.PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1 MultiMap (com.intellij.util.containers.MultiMap)1 DFSTBuilder (com.intellij.util.graph.DFSTBuilder)1 Graph (com.intellij.util.graph.Graph)1 GraphGenerator (com.intellij.util.graph.GraphGenerator)1 THashSet (gnu.trove.THashSet)1 java.util (java.util)1