Search in sources :

Example 11 with MultiMap

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

the class MavenDuplicatePluginInspection method checkFileElement.

@Override
public void checkFileElement(DomFileElement<MavenDomProjectModel> domFileElement, DomElementAnnotationHolder holder) {
    MavenDomProjectModel projectModel = domFileElement.getRootElement();
    MultiMap<Pair<String, String>, MavenDomPlugin> duplicates = MultiMap.createSet();
    for (MavenDomPlugin plugin : projectModel.getBuild().getPlugins().getPlugins()) {
        String groupId = plugin.getGroupId().getStringValue();
        String artifactId = plugin.getArtifactId().getStringValue();
        if (StringUtil.isEmptyOrSpaces(artifactId))
            continue;
        if ("".equals(groupId) || "org.apache.maven.plugins".equals(groupId) || "org.codehaus.mojo".equals(groupId)) {
            groupId = null;
        }
        duplicates.putValue(Pair.create(groupId, artifactId), plugin);
    }
    for (Map.Entry<Pair<String, String>, Collection<MavenDomPlugin>> entry : duplicates.entrySet()) {
        Collection<MavenDomPlugin> set = entry.getValue();
        if (set.size() <= 1)
            continue;
        for (MavenDomPlugin dependency : set) {
            holder.createProblem(dependency, HighlightSeverity.WARNING, "Duplicated plugin declaration");
        }
    }
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenDomPlugin(org.jetbrains.idea.maven.dom.model.MavenDomPlugin) Collection(java.util.Collection) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) Pair(com.intellij.openapi.util.Pair)

Example 12 with MultiMap

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

the class ConfigFilesTreeBuilder method buildTree.

public Set<PsiFile> buildTree(DefaultMutableTreeNode root, ConfigFileSearcher... searchers) {
    final Set<PsiFile> psiFiles = new com.intellij.util.containers.HashSet<>();
    final MultiMap<Module, PsiFile> files = new MultiMap<>();
    final MultiMap<VirtualFile, PsiFile> jars = new MultiMap<>();
    final MultiMap<VirtualFile, PsiFile> virtualFiles = new MultiMap<>();
    for (ConfigFileSearcher searcher : searchers) {
        files.putAllValues(searcher.getFilesByModules());
        jars.putAllValues(searcher.getJars());
        virtualFiles.putAllValues(searcher.getVirtualFiles());
    }
    psiFiles.addAll(buildModuleNodes(files, jars, root));
    for (Map.Entry<VirtualFile, Collection<PsiFile>> entry : virtualFiles.entrySet()) {
        DefaultMutableTreeNode node = createFileNode(entry.getKey());
        List<PsiFile> list = new ArrayList<>(entry.getValue());
        Collections.sort(list, FILE_COMPARATOR);
        for (PsiFile file : list) {
            node.add(createFileNode(file));
        }
        root.add(node);
    }
    return psiFiles;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) MultiMap(com.intellij.util.containers.MultiMap) java.util(java.util) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) MultiMap(com.intellij.util.containers.MultiMap)

Example 13 with MultiMap

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

the class ConfigFilesTreeBuilder method buildModuleNodes.

public Set<PsiFile> buildModuleNodes(final MultiMap<Module, PsiFile> files, final MultiMap<VirtualFile, PsiFile> jars, DefaultMutableTreeNode root) {
    final HashSet<PsiFile> psiFiles = new HashSet<>();
    final List<Module> modules = new ArrayList<>(files.keySet());
    Collections.sort(modules, ModulesAlphaComparator.INSTANCE);
    for (Module module : modules) {
        DefaultMutableTreeNode moduleNode = createFileNode(module);
        root.add(moduleNode);
        if (files.containsKey(module)) {
            List<PsiFile> moduleFiles = new ArrayList<>(files.get(module));
            MultiMap<FileType, PsiFile> filesByType = new MultiMap<>();
            for (PsiFile file : moduleFiles) {
                filesByType.putValue(file.getFileType(), file);
            }
            if (hasNonEmptyGroups(filesByType)) {
                for (Map.Entry<FileType, Collection<PsiFile>> entry : filesByType.entrySet()) {
                    DefaultMutableTreeNode fileTypeNode = createFileNode(entry.getKey());
                    moduleNode.add(fileTypeNode);
                    addChildrenFiles(psiFiles, fileTypeNode, new ArrayList<>(entry.getValue()));
                }
            } else {
                addChildrenFiles(psiFiles, moduleNode, moduleFiles);
            }
        }
    }
    List<VirtualFile> sortedJars = new ArrayList<>(jars.keySet());
    Collections.sort(sortedJars, (o1, o2) -> StringUtil.naturalCompare(o1.getName(), o2.getName()));
    for (VirtualFile file : sortedJars) {
        if (!file.isValid())
            continue;
        final List<PsiFile> list = new ArrayList<>(jars.get(file));
        final PsiFile jar = list.get(0).getManager().findFile(file);
        if (jar != null) {
            final DefaultMutableTreeNode jarNode = createFileNode(jar);
            root.add(jarNode);
            Collections.sort(list, FILE_COMPARATOR);
            for (PsiFile psiFile : list) {
                jarNode.add(createFileNode(psiFile));
                psiFiles.add(psiFile);
            }
        }
    }
    return psiFiles;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) MultiMap(com.intellij.util.containers.MultiMap) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) MultiMap(com.intellij.util.containers.MultiMap)

Example 14 with MultiMap

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

the class GrPullUpConflictsUtil method checkConflicts.

public static MultiMap<PsiElement, String> checkConflicts(final MemberInfoBase<? extends GrMember>[] infos, @NotNull final PsiClass subclass, @Nullable PsiClass superClass, @NotNull final PsiPackage targetPackage, @NotNull PsiDirectory targetDirectory, final InterfaceContainmentVerifier interfaceContainmentVerifier, boolean movedMembers2Super) {
    final PsiElement targetRepresentativeElement;
    final boolean isInterfaceTarget;
    if (superClass != null) {
        isInterfaceTarget = superClass.isInterface();
        targetRepresentativeElement = superClass;
    } else {
        isInterfaceTarget = false;
        targetRepresentativeElement = targetDirectory;
    }
    final Set<GrMember> movedMembers = ContainerUtil.newHashSet();
    final Set<GrMethod> abstractMethods = ContainerUtil.newHashSet();
    for (MemberInfoBase<? extends GrMember> info : infos) {
        GrMember member = info.getMember();
        if (member instanceof GrMethod) {
            if (!info.isToAbstract() && !isInterfaceTarget) {
                movedMembers.add(member);
            } else {
                abstractMethods.add((GrMethod) member);
            }
        } else {
            movedMembers.add(member);
        }
    }
    final Set<PsiMethod> allAbstractMethods = new HashSet<>(abstractMethods);
    if (superClass != null) {
        for (PsiMethod method : subclass.getMethods()) {
            if (!movedMembers.contains(method) && !method.hasModifierProperty(PsiModifier.PRIVATE)) {
                if (method.findSuperMethods(superClass).length > 0) {
                    allAbstractMethods.add(method);
                }
            }
        }
    }
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    GrRefactoringConflictsUtil.analyzeAccessibilityConflicts(movedMembers, superClass, conflicts, VisibilityUtil.ESCALATE_VISIBILITY, targetRepresentativeElement, allAbstractMethods);
    if (superClass != null) {
        if (movedMembers2Super) {
            checkSuperclassMembers(superClass, infos, conflicts);
            if (isInterfaceTarget) {
                checkInterfaceTarget(infos, conflicts);
            }
        } else {
            final String qualifiedName = superClass.getQualifiedName();
            assert qualifiedName != null;
            if (superClass.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
                if (!Comparing.strEqual(StringUtil.getPackageName(qualifiedName), targetPackage.getQualifiedName())) {
                    conflicts.putValue(superClass, RefactoringUIUtil.getDescription(superClass, true) + " won't be accessible from " + RefactoringUIUtil.getDescription(targetPackage, true));
                }
            }
        }
    }
    // check if moved methods use other members in the classes between Subclass and Superclass
    List<PsiElement> checkModuleConflictsList = new ArrayList<>();
    for (PsiMember member : movedMembers) {
        if (member instanceof PsiMethod || member instanceof PsiClass && !(member instanceof PsiCompiledElement)) {
            GrClassMemberReferenceVisitor visitor = movedMembers2Super ? new ConflictingUsagesOfSubClassMembers(member, movedMembers, abstractMethods, subclass, superClass, superClass != null ? null : targetPackage, conflicts, interfaceContainmentVerifier) : new ConflictingUsagesOfSuperClassMembers(member, subclass, targetPackage, movedMembers, conflicts);
            ((GroovyPsiElement) member).accept(visitor);
        }
        checkModuleConflictsList.add(member);
    }
    for (final PsiMethod method : abstractMethods) {
        ContainerUtil.addIfNotNull(checkModuleConflictsList, method.getParameterList());
        ContainerUtil.addIfNotNull(checkModuleConflictsList, method.getReturnTypeElement());
        ContainerUtil.addIfNotNull(checkModuleConflictsList, method.getTypeParameterList());
    }
    GrRefactoringConflictsUtil.analyzeModuleConflicts(subclass.getProject(), checkModuleConflictsList, UsageInfo.EMPTY_ARRAY, targetRepresentativeElement, conflicts);
    final String fqName = subclass.getQualifiedName();
    final String packageName;
    if (fqName != null) {
        packageName = StringUtil.getPackageName(fqName);
    } else {
        final PsiFile psiFile = PsiTreeUtil.getParentOfType(subclass, PsiFile.class);
        if (psiFile instanceof PsiClassOwner) {
            packageName = ((PsiClassOwner) psiFile).getPackageName();
        } else {
            packageName = null;
        }
    }
    final boolean toDifferentPackage = !Comparing.strEqual(targetPackage.getQualifiedName(), packageName);
    for (final GrMethod abstractMethod : abstractMethods) {
        abstractMethod.accept(new GrClassMemberReferenceVisitor(subclass) {

            @Override
            protected void visitClassMemberReferenceElement(GrMember classMember, GrReferenceElement classMemberReference) {
                if (classMember != null && willBeMoved(classMember, movedMembers)) {
                    boolean isAccessible = false;
                    if (classMember.hasModifierProperty(PsiModifier.PRIVATE)) {
                        isAccessible = true;
                    } else if (classMember.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && toDifferentPackage) {
                        isAccessible = true;
                    }
                    if (isAccessible) {
                        String message = RefactoringUIUtil.getDescription(abstractMethod, false) + " uses " + RefactoringUIUtil.getDescription(classMember, true) + " which won't be accessible from the subclass.";
                        message = CommonRefactoringUtil.capitalize(message);
                        conflicts.putValue(classMember, message);
                    }
                }
            }
        });
        if (abstractMethod.hasModifierProperty(PsiModifier.PACKAGE_LOCAL) && toDifferentPackage) {
            if (!isInterfaceTarget) {
                String message = "Can't make " + RefactoringUIUtil.getDescription(abstractMethod, false) + " abstract as it won't be accessible from the subclass.";
                message = CommonRefactoringUtil.capitalize(message);
                conflicts.putValue(abstractMethod, message);
            }
        }
    }
    return conflicts;
}
Also used : GrClassMemberReferenceVisitor(org.jetbrains.plugins.groovy.refactoring.classMembers.GrClassMemberReferenceVisitor) ArrayList(java.util.ArrayList) MultiMap(com.intellij.util.containers.MultiMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) HashSet(java.util.HashSet) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 15 with MultiMap

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

the class GrPullUpHandler method checkConflicts.

@Override
public boolean checkConflicts(final GrPullUpDialog dialog) {
    /*                         todo */
    List<GrMemberInfo> _infos = dialog.getSelectedMemberInfos();
    final GrMemberInfo[] infos = _infos.toArray(new GrMemberInfo[_infos.size()]);
    final PsiClass superClass = dialog.getSuperClass();
    if (!checkWritable(superClass, infos))
        return false;
    final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
    if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> ApplicationManager.getApplication().runReadAction(() -> {
        final PsiDirectory targetDirectory = superClass.getContainingFile().getContainingDirectory();
        final PsiPackage targetPackage = targetDirectory != null ? JavaDirectoryService.getInstance().getPackage(targetDirectory) : null;
        conflicts.putAllValues(GrPullUpConflictsUtil.checkConflicts(infos, mySubclass, superClass, targetPackage, targetDirectory, dialog.getContainmentVerifier()));
    }), RefactoringBundle.message("detecting.possible.conflicts"), true, myProject)) {
        return false;
    }
    if (!conflicts.isEmpty()) {
        ConflictsDialog conflictsDialog = new ConflictsDialog(myProject, conflicts);
        conflictsDialog.show();
        final boolean ok = conflictsDialog.isOK();
        if (!ok && conflictsDialog.isShowConflicts())
            dialog.close(DialogWrapper.CANCEL_EXIT_CODE);
        return ok;
    }
    return true;
}
Also used : MultiMap(com.intellij.util.containers.MultiMap) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) GrMemberInfo(org.jetbrains.plugins.groovy.refactoring.classMembers.GrMemberInfo)

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