Search in sources :

Example 36 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project intellij-community by JetBrains.

the class UnnecessaryModuleDependencyInspection method checkElement.

@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull final GlobalInspectionContext globalContext) {
    if (refEntity instanceof RefModule) {
        final RefModule refModule = (RefModule) refEntity;
        final Module module = refModule.getModule();
        if (module.isDisposed() || !scope.containsModule(module))
            return CommonProblemDescriptor.EMPTY_ARRAY;
        final ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);
        final OrderEntry[] declaredDependencies = moduleRootManager.getOrderEntries();
        final Module[] declaredModuleDependencies = moduleRootManager.getDependencies();
        List<CommonProblemDescriptor> descriptors = new ArrayList<>();
        final Set<Module> modules = refModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);
        Graph<Module> graph = myGraph.get();
        if (graph == null) {
            graph = ModuleManager.getInstance(globalContext.getProject()).moduleGraph();
            myGraph = new SoftReference<>(graph);
        }
        final RefManager refManager = globalContext.getRefManager();
        for (final OrderEntry entry : declaredDependencies) {
            if (entry instanceof ModuleOrderEntry) {
                final Module dependency = ((ModuleOrderEntry) entry).getModule();
                if (dependency != null) {
                    if (modules == null || !modules.contains(dependency)) {
                        List<String> dependenciesThroughExported = null;
                        if (((ModuleOrderEntry) entry).isExported()) {
                            final Iterator<Module> iterator = graph.getOut(module);
                            while (iterator.hasNext()) {
                                final Module dep = iterator.next();
                                final RefModule depRefModule = refManager.getRefModule(dep);
                                if (depRefModule != null) {
                                    final Set<Module> neededModules = depRefModule.getUserData(UnnecessaryModuleDependencyAnnotator.DEPENDENCIES);
                                    if (neededModules != null && neededModules.contains(dependency)) {
                                        if (dependenciesThroughExported == null) {
                                            dependenciesThroughExported = new ArrayList<>();
                                        }
                                        dependenciesThroughExported.add(dep.getName());
                                    }
                                }
                            }
                        }
                        if (modules != null) {
                            List<String> transitiveDependencies = new ArrayList<>();
                            final OrderEntry[] dependenciesOfDependencies = ModuleRootManager.getInstance(dependency).getOrderEntries();
                            for (OrderEntry secondDependency : dependenciesOfDependencies) {
                                if (secondDependency instanceof ModuleOrderEntry && ((ModuleOrderEntry) secondDependency).isExported()) {
                                    final Module mod = ((ModuleOrderEntry) secondDependency).getModule();
                                    if (mod != null && modules.contains(mod) && ArrayUtil.find(declaredModuleDependencies, mod) < 0) {
                                        transitiveDependencies.add(mod.getName());
                                    }
                                }
                            }
                            if (!transitiveDependencies.isEmpty()) {
                                final String exported = StringUtil.join(transitiveDependencies, ", ");
                                descriptors.add(manager.createProblemDescriptor(InspectionsBundle.message("unnecessary.module.dependency.exported.problem.descriptor1", module.getName(), dependency.getName(), exported)));
                                continue;
                            }
                        }
                        descriptors.add(createDescriptor(scope, manager, module, dependency, dependenciesThroughExported));
                    }
                }
            }
        }
        return descriptors.isEmpty() ? null : descriptors.toArray(new CommonProblemDescriptor[descriptors.size()]);
    }
    return null;
}
Also used : RefManager(com.intellij.codeInspection.reference.RefManager) ArrayList(java.util.ArrayList) ModuleRootManager(com.intellij.openapi.roots.ModuleRootManager) RefModule(com.intellij.codeInspection.reference.RefModule) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) OrderEntry(com.intellij.openapi.roots.OrderEntry) ModuleOrderEntry(com.intellij.openapi.roots.ModuleOrderEntry) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module)

Example 37 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project intellij-community by JetBrains.

the class MavenProjectImporter method removeUnusedProjectLibraries.

private boolean removeUnusedProjectLibraries() {
    Set<Library> unusedLibraries = new HashSet<>();
    Collections.addAll(unusedLibraries, myModelsProvider.getAllLibraries());
    for (ModuleRootModel eachModel : collectModuleModels()) {
        for (OrderEntry eachEntry : eachModel.getOrderEntries()) {
            if (eachEntry instanceof LibraryOrderEntry) {
                unusedLibraries.remove(((LibraryOrderEntry) eachEntry).getLibrary());
            }
        }
    }
    boolean removed = false;
    for (Library each : unusedLibraries) {
        if (!isDisposed(each) && MavenRootModelAdapter.isMavenLibrary(each) && !MavenRootModelAdapter.isChangedByUser(each)) {
            myModelsProvider.removeLibrary(each);
            removed = true;
        }
    }
    return removed;
}
Also used : OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) ModuleRootModel(com.intellij.openapi.roots.ModuleRootModel) Library(com.intellij.openapi.roots.libraries.Library) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) THashSet(gnu.trove.THashSet)

Example 38 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project intellij-community by JetBrains.

the class MvcUpgradeAction method removeOldMvcSdk.

public static void removeOldMvcSdk(MvcFramework framework, ModifiableRootModel model) {
    final LibraryPresentationManager presentationManager = LibraryPresentationManager.getInstance();
    for (OrderEntry entry : model.getOrderEntries()) {
        if (entry instanceof LibraryOrderEntry) {
            final Library library = ((LibraryOrderEntry) entry).getLibrary();
            final LibrariesContainer container = LibrariesContainerFactory.createContainer(model);
            if (library != null) {
                final VirtualFile[] files = container.getLibraryFiles(library, OrderRootType.CLASSES);
                if (presentationManager.isLibraryOfKind(Arrays.asList(files), framework.getLibraryKind())) {
                    model.removeOrderEntry(entry);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) LibrariesContainer(com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Library(com.intellij.openapi.roots.libraries.Library) LibraryPresentationManager(com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager)

Example 39 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project intellij-community by JetBrains.

the class MavenAttachSourcesProvider method getMavenProjects.

private static Collection<MavenProject> getMavenProjects(PsiFile psiFile) {
    Project project = psiFile.getProject();
    Collection<MavenProject> result = new ArrayList<>();
    for (OrderEntry each : ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(psiFile.getVirtualFile())) {
        MavenProject mavenProject = MavenProjectsManager.getInstance(project).findProject(each.getOwnerModule());
        if (mavenProject != null)
            result.add(mavenProject);
    }
    return result;
}
Also used : Project(com.intellij.openapi.project.Project) MavenProject(org.jetbrains.idea.maven.project.MavenProject) OrderEntry(com.intellij.openapi.roots.OrderEntry) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) MavenProject(org.jetbrains.idea.maven.project.MavenProject) ArrayList(java.util.ArrayList)

Example 40 with OrderEntry

use of com.intellij.openapi.roots.OrderEntry in project intellij-community by JetBrains.

the class PsiFileNode method navigate.

@Override
public void navigate(boolean requestFocus) {
    final VirtualFile jarRoot = getJarRoot();
    final Project project = getProject();
    if (requestFocus && jarRoot != null && project != null && ProjectRootsUtil.isLibraryRoot(jarRoot, project)) {
        final OrderEntry orderEntry = LibraryUtil.findLibraryEntry(jarRoot, project);
        if (orderEntry != null) {
            ProjectSettingsService.getInstance(project).openLibraryOrSdkSettings(orderEntry);
            return;
        }
    }
    super.navigate(requestFocus);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) OrderEntry(com.intellij.openapi.roots.OrderEntry)

Aggregations

OrderEntry (com.intellij.openapi.roots.OrderEntry)54 Module (com.intellij.openapi.module.Module)24 VirtualFile (com.intellij.openapi.vfs.VirtualFile)23 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)18 Project (com.intellij.openapi.project.Project)14 Library (com.intellij.openapi.roots.libraries.Library)12 ArrayList (java.util.ArrayList)11 JdkOrderEntry (com.intellij.openapi.roots.JdkOrderEntry)10 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)10 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)8 ModuleOrderEntry (com.intellij.openapi.roots.ModuleOrderEntry)8 Nullable (org.jetbrains.annotations.Nullable)8 NotNull (org.jetbrains.annotations.NotNull)7 ModuleLibraryOrderEntryImpl (com.intellij.openapi.roots.impl.ModuleLibraryOrderEntryImpl)5 List (java.util.List)5 Sdk (com.intellij.openapi.projectRoots.Sdk)4 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)4 AccessToken (com.intellij.openapi.application.AccessToken)3 LibraryEx (com.intellij.openapi.roots.impl.libraries.LibraryEx)3 LibraryTable (com.intellij.openapi.roots.libraries.LibraryTable)3