Search in sources :

Example 1 with MODULE_INFO_FILE

use of com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE 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)

Example 2 with MODULE_INFO_FILE

use of com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE in project intellij-community by JetBrains.

the class JavaModuleGraphUtil method buildRequiresGraph.

// Starting from source modules, collects all module dependencies in the project.
// The resulting graph is used for tracing readability and checking package conflicts.
private static RequiresGraph buildRequiresGraph(Project project) {
    MultiMap<PsiJavaModule, PsiJavaModule> relations = MultiMap.create();
    Set<String> transitiveEdges = ContainerUtil.newTroveSet();
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        Collection<VirtualFile> files = FilenameIndex.getVirtualFilesByName(project, MODULE_INFO_FILE, module.getModuleScope(false));
        Optional.ofNullable(ContainerUtil.getFirstItem(files)).map(PsiManager.getInstance(project)::findFile).map(f -> f instanceof PsiJavaFile ? ((PsiJavaFile) f).getModuleDeclaration() : null).ifPresent(m -> visit(m, relations, transitiveEdges));
    }
    Graph<PsiJavaModule> graph = GraphGenerator.generate(new ChameleonGraph<>(relations, true));
    return new RequiresGraph(graph, transitiveEdges);
}
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) LightJavaModule(com.intellij.psi.impl.light.LightJavaModule) Module(com.intellij.openapi.module.Module)

Example 3 with MODULE_INFO_FILE

use of com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE in project intellij-community by JetBrains.

the class ModuleHighlightUtil method checkFileDuplicates.

@Nullable
static HighlightInfo checkFileDuplicates(@NotNull PsiJavaModule element, @NotNull PsiFile file) {
    Module module = findModule(file);
    if (module != null) {
        Project project = file.getProject();
        Collection<VirtualFile> others = FilenameIndex.getVirtualFilesByName(project, MODULE_INFO_FILE, module.getModuleScope(false));
        if (others.size() > 1) {
            String message = JavaErrorMessages.message("module.file.duplicate");
            HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range(element)).descriptionAndTooltip(message).create();
            others.stream().map(f -> PsiManager.getInstance(project).findFile(f)).filter(f -> f != file).findFirst().ifPresent(duplicate -> QuickFixAction.registerQuickFixAction(info, new GoToSymbolFix(duplicate, JavaErrorMessages.message("module.open.duplicate.text"))));
            return info;
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) JavaErrorMessages(com.intellij.codeInsight.daemon.JavaErrorMessages) Trinity(com.intellij.openapi.util.Trinity) java.util(java.util) JBIterable(com.intellij.util.containers.JBIterable) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) FilenameIndex(com.intellij.psi.search.FilenameIndex) ContainerUtil(com.intellij.util.containers.ContainerUtil) Function(java.util.function.Function) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) InheritanceUtil(com.intellij.psi.util.InheritanceUtil) Project(com.intellij.openapi.project.Project) PsiUtil(com.intellij.psi.util.PsiUtil) PropertyKey(org.jetbrains.annotations.PropertyKey) Module(com.intellij.openapi.module.Module) QuickFixBundle(com.intellij.codeInsight.daemon.QuickFixBundle) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) StringUtil(com.intellij.openapi.util.text.StringUtil) QuickFixFactory(com.intellij.codeInsight.intention.QuickFixFactory) HighlightInfoType(com.intellij.codeInsight.daemon.impl.HighlightInfoType) TextRange(com.intellij.openapi.util.TextRange) Collectors(java.util.stream.Collectors) MODULE_INFO_FILE(com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE) com.intellij.codeInsight.daemon.impl.quickfix(com.intellij.codeInsight.daemon.impl.quickfix) LightJavaModule(com.intellij.psi.impl.light.LightJavaModule) Nullable(org.jetbrains.annotations.Nullable) Stream(java.util.stream.Stream) ClassUtil(com.intellij.psi.util.ClassUtil) JarFileSystem(com.intellij.openapi.vfs.JarFileSystem) ApplicationManager(com.intellij.openapi.application.ApplicationManager) Role(com.intellij.psi.PsiPackageAccessibilityStatement.Role) ObjectUtils(com.intellij.util.ObjectUtils) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) Module(com.intellij.openapi.module.Module) LightJavaModule(com.intellij.psi.impl.light.LightJavaModule) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with MODULE_INFO_FILE

use of com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE in project intellij-community by JetBrains.

the class CreateModuleInfoAction method update.

@Override
public void update(@NotNull AnActionEvent e) {
    DataContext ctx = e.getDataContext();
    boolean available = Optional.ofNullable(LangDataKeys.IDE_VIEW.getData(ctx)).map(view -> getTargetDirectory(ctx, view)).filter(PsiUtil::isLanguageLevel9OrHigher).map(ModuleUtilCore::findModuleForPsiElement).map(module -> FilenameIndex.getVirtualFilesByName(module.getProject(), MODULE_INFO_FILE, module.getModuleScope(false)).isEmpty()).orElse(false);
    e.getPresentation().setEnabledAndVisible(available);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) AllIcons(com.intellij.icons.AllIcons) DataContext(com.intellij.openapi.actionSystem.DataContext) VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProjectFileIndex(com.intellij.openapi.roots.ProjectFileIndex) FilenameIndex(com.intellij.psi.search.FilenameIndex) IdeView(com.intellij.ide.IdeView) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) PsiUtil(com.intellij.psi.util.PsiUtil) Module(com.intellij.openapi.module.Module) ProjectRootManager(com.intellij.openapi.roots.ProjectRootManager) INTERNAL_MODULE_INFO_TEMPLATE_NAME(com.intellij.ide.fileTemplates.JavaTemplateUtil.INTERNAL_MODULE_INFO_TEMPLATE_NAME) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) JavaSourceRootType(org.jetbrains.jps.model.java.JavaSourceRootType) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) AttributesDefaults(com.intellij.ide.fileTemplates.actions.AttributesDefaults) MODULE_INFO_FILE(com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE) IdeBundle(com.intellij.ide.IdeBundle) LightJavaModule(com.intellij.psi.impl.light.LightJavaModule) Nullable(org.jetbrains.annotations.Nullable) CreateFromTemplateActionBase(com.intellij.ide.fileTemplates.actions.CreateFromTemplateActionBase) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) Optional(java.util.Optional) PsiDirectory(com.intellij.psi.PsiDirectory) LangDataKeys(com.intellij.openapi.actionSystem.LangDataKeys) NotNull(org.jetbrains.annotations.NotNull) MODULE_INFO_CLASS(com.intellij.psi.PsiJavaModule.MODULE_INFO_CLASS) Collections(java.util.Collections) DataContext(com.intellij.openapi.actionSystem.DataContext) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore)

Aggregations

Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 MODULE_INFO_FILE (com.intellij.psi.PsiJavaModule.MODULE_INFO_FILE)4 LightJavaModule (com.intellij.psi.impl.light.LightJavaModule)4 FilenameIndex (com.intellij.psi.search.FilenameIndex)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 Trinity (com.intellij.openapi.util.Trinity)3 com.intellij.psi (com.intellij.psi)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 ModuleManager (com.intellij.openapi.module.ModuleManager)2 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)2 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)2 PsiJavaModuleReference (com.intellij.psi.impl.source.PsiJavaModuleReference)2 Result (com.intellij.psi.util.CachedValueProvider.Result)2 CachedValuesManager (com.intellij.psi.util.CachedValuesManager)2 OUT_OF_CODE_BLOCK_MODIFICATION_COUNT (com.intellij.psi.util.PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT)2