Search in sources :

Example 31 with MultiMap

use of com.intellij.util.containers.MultiMap 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 32 with MultiMap

use of com.intellij.util.containers.MultiMap 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 33 with MultiMap

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

the class ExtractMethodObjectDialog method doOKAction.

@Override
protected void doOKAction() {
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (myCreateInnerClassRb.isSelected()) {
        final PsiClass innerClass = myTargetClass.findInnerClassByName(myInnerClassName.getText(), false);
        if (innerClass != null) {
            conflicts.putValue(innerClass, "Inner class " + myInnerClassName.getText() + " already defined in class " + myTargetClass.getName());
        }
    }
    if (conflicts.size() > 0) {
        final ConflictsDialog conflictsDialog = new ConflictsDialog(myProject, conflicts);
        if (!conflictsDialog.showAndGet()) {
            if (conflictsDialog.isShowConflicts())
                close(CANCEL_EXIT_CODE);
            return;
        }
    }
    final JCheckBox makeVarargsCb = myCreateInnerClassRb.isSelected() ? myCbMakeVarargs : myCbMakeVarargsAnonymous;
    if (makeVarargsCb != null && makeVarargsCb.isSelected()) {
        final VariableData data = myInputVariables[myInputVariables.length - 1];
        if (data.type instanceof PsiArrayType) {
            data.type = new PsiEllipsisType(((PsiArrayType) data.type).getComponentType());
        }
    }
    super.doOKAction();
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) VariableData(com.intellij.refactoring.util.VariableData)

Example 34 with MultiMap

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

the class InlineConstantFieldProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    UsageInfo[] usagesIn = refUsages.get();
    MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    ReferencedElementsCollector collector = new ReferencedElementsCollector();
    PsiExpression initializer = InlineConstantFieldHandler.getInitializer(myField);
    LOG.assertTrue(initializer != null);
    initializer.accept(collector);
    HashSet<PsiMember> referencedWithVisibility = collector.myReferencedMembers;
    PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(myField.getProject()).getResolveHelper();
    for (UsageInfo info : usagesIn) {
        PsiElement element = info.getElement();
        if (element instanceof PsiExpression && (!myField.hasModifierProperty(PsiModifier.FINAL) || myInlineThisOnly) && isAccessedForWriting((PsiExpression) element)) {
            String message = RefactoringBundle.message("0.is.used.for.writing.in.1", RefactoringUIUtil.getDescription(myField, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
            conflicts.putValue(element, message);
        }
        for (PsiMember member : referencedWithVisibility) {
            if (!resolveHelper.isAccessible(member, element, null)) {
                String message = RefactoringBundle.message("0.will.not.be.accessible.from.1.after.inlining", RefactoringUIUtil.getDescription(member, true), RefactoringUIUtil.getDescription(ConflictsUtil.getContainer(element), true));
                conflicts.putValue(member, message);
            }
        }
    }
    if (!myInlineThisOnly) {
        for (UsageInfo info : usagesIn) {
            if (info instanceof UsageFromJavaDoc) {
                final PsiElement element = info.getElement();
                if (element instanceof PsiDocMethodOrFieldRef && !PsiTreeUtil.isAncestor(myField, element, false)) {
                    conflicts.putValue(element, "Inlined method is used in javadoc");
                }
            }
        }
    }
    return showConflicts(conflicts, usagesIn);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) PsiDocMethodOrFieldRef(com.intellij.psi.impl.source.javadoc.PsiDocMethodOrFieldRef) UsageInfo(com.intellij.usageView.UsageInfo)

Example 35 with MultiMap

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

the class InlineParameterExpressionProcessor method preprocessUsages.

@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    JavaSafeDeleteProcessor.collectMethodConflicts(conflicts, myMethod, myParameter);
    final UsageInfo[] usages = refUsages.get();
    final InaccessibleExpressionsDetector detector = new InaccessibleExpressionsDetector(conflicts);
    myInitializer.accept(detector);
    for (UsageInfo usage : usages) {
        if (usage instanceof LocalReplacementUsageInfo) {
            final PsiElement replacement = ((LocalReplacementUsageInfo) usage).getReplacement();
            if (replacement != null) {
                replacement.accept(detector);
            }
        }
    }
    final Set<PsiVariable> vars = new HashSet<>();
    for (UsageInfo usageInfo : usages) {
        if (usageInfo instanceof LocalReplacementUsageInfo) {
            final PsiVariable var = ((LocalReplacementUsageInfo) usageInfo).getVariable();
            if (var != null) {
                vars.add(var);
            }
        }
    }
    for (PsiVariable var : vars) {
        for (PsiReference ref : ReferencesSearch.search(var)) {
            final PsiElement element = ref.getElement();
            if (element instanceof PsiExpression && isAccessedForWriting((PsiExpression) element)) {
                conflicts.putValue(element, "Parameter initializer depends on value which is not available inside method and cannot be inlined");
                break;
            }
        }
    }
    return showConflicts(conflicts, usages);
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) UsageInfo(com.intellij.usageView.UsageInfo)

Aggregations

MultiMap (com.intellij.util.containers.MultiMap)141 NotNull (org.jetbrains.annotations.NotNull)38 UsageInfo (com.intellij.usageView.UsageInfo)26 VirtualFile (com.intellij.openapi.vfs.VirtualFile)25 Project (com.intellij.openapi.project.Project)19 PsiElement (com.intellij.psi.PsiElement)18 Collection (java.util.Collection)17 ConflictsDialog (com.intellij.refactoring.ui.ConflictsDialog)16 Nullable (org.jetbrains.annotations.Nullable)16 Map (java.util.Map)15 Module (com.intellij.openapi.module.Module)11 File (java.io.File)11 ArrayList (java.util.ArrayList)11 ContainerUtil (com.intellij.util.containers.ContainerUtil)10 HashSet (com.intellij.util.containers.HashSet)10 THashSet (gnu.trove.THashSet)9 java.util (java.util)9 Pair (com.intellij.openapi.util.Pair)8 com.intellij.psi (com.intellij.psi)8 HashSet (java.util.HashSet)8