Search in sources :

Example 1 with RefModule

use of com.intellij.codeInspection.reference.RefModule in project intellij-community by JetBrains.

the class UnnecessaryModuleDependencyAnnotator method onMarkReferenced.

@Override
public void onMarkReferenced(RefElement refWhat, RefElement refFrom, boolean referencedFromClassInitializer) {
    final PsiElement onElement = refWhat.getElement();
    final PsiElement fromElement = refFrom.getElement();
    if (onElement != null && fromElement != null) {
        final Module onModule = ModuleUtilCore.findModuleForPsiElement(onElement);
        final Module fromModule = ModuleUtilCore.findModuleForPsiElement(fromElement);
        if (onModule != null && fromModule != null && onModule != fromModule) {
            final RefModule refModule = myManager.getRefModule(fromModule);
            if (refModule != null) {
                Set<Module> modules = refModule.getUserData(DEPENDENCIES);
                if (modules == null) {
                    modules = new HashSet<>();
                    refModule.putUserData(DEPENDENCIES, modules);
                }
                modules.add(onModule);
            }
        }
    }
}
Also used : RefModule(com.intellij.codeInspection.reference.RefModule) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Example 2 with RefModule

use of com.intellij.codeInspection.reference.RefModule in project intellij-community by JetBrains.

the class PackageInMultipleModulesInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefPackage)) {
        return null;
    }
    final List<RefEntity> children = refEntity.getChildren();
    final Set<RefModule> modules = new HashSet<>();
    for (RefEntity child : children) {
        if (!(child instanceof RefClass)) {
            continue;
        }
        final RefClass refClass = (RefClass) child;
        final RefModule module = refClass.getModule();
        modules.add(module);
    }
    if (modules.size() <= 1) {
        return null;
    }
    final String errorString = InspectionGadgetsBundle.message("package.in.multiple.modules.problem.descriptor", refEntity.getQualifiedName());
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : RefModule(com.intellij.codeInspection.reference.RefModule) RefClass(com.intellij.codeInspection.reference.RefClass) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) RefPackage(com.intellij.codeInspection.reference.RefPackage) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RefModule

use of com.intellij.codeInspection.reference.RefModule 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 4 with RefModule

use of com.intellij.codeInspection.reference.RefModule in project intellij-community by JetBrains.

the class UnusedLibrariesInspection method checkElement.

@Nullable
@Override
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor processor) {
    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 Set<VirtualFile> usedRoots = refModule.getUserData(UnusedLibraryGraphAnnotator.USED_LIBRARY_ROOTS);
        final List<CommonProblemDescriptor> result = new ArrayList<>();
        for (OrderEntry entry : moduleRootManager.getOrderEntries()) {
            if (entry instanceof LibraryOrderEntry && !((LibraryOrderEntry) entry).isExported()) {
                if (usedRoots == null) {
                    String message = InspectionsBundle.message("unused.library.problem.descriptor", entry.getPresentableName());
                    result.add(manager.createProblemDescriptor(message, new RemoveUnusedLibrary(refModule, entry, null)));
                } else {
                    final Set<VirtualFile> files = new HashSet<>(Arrays.asList(((LibraryOrderEntry) entry).getRootFiles(OrderRootType.CLASSES)));
                    files.removeAll(usedRoots);
                    if (!files.isEmpty()) {
                        final String unusedLibraryRoots = StringUtil.join(files, file -> file.getPresentableName(), ",");
                        String message = InspectionsBundle.message("unused.library.roots.problem.descriptor", unusedLibraryRoots, entry.getPresentableName());
                        processor.addProblemElement(refModule, manager.createProblemDescriptor(message, new RemoveUnusedLibrary(refModule, entry, files)));
                    }
                }
            }
        }
        return result.isEmpty() ? null : result.toArray(new CommonProblemDescriptor[result.size()]);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RefModule(com.intellij.codeInspection.reference.RefModule) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RefModule

use of com.intellij.codeInspection.reference.RefModule in project intellij-community by JetBrains.

the class InconsistentLanguageLevelInspection method runInspection.

@Override
public void runInspection(@NotNull AnalysisScope scope, @NotNull InspectionManager manager, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemProcessor) {
    final Set<Module> modules = new THashSet<>();
    scope.accept(new PsiElementVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            final Module module = ModuleUtilCore.findModuleForPsiElement(element);
            if (module != null) {
                modules.add(module);
            }
        }
    });
    LanguageLevel projectLanguageLevel = LanguageLevelProjectExtension.getInstance(manager.getProject()).getLanguageLevel();
    for (Module module : modules) {
        LanguageLevel languageLevel = LanguageLevelModuleExtensionImpl.getInstance(module).getLanguageLevel();
        if (languageLevel == null) {
            languageLevel = projectLanguageLevel;
        }
        RefManager refManager = globalContext.getRefManager();
        final RefModule refModule = refManager.getRefModule(module);
        for (OrderEntry entry : ModuleRootManager.getInstance(module).getOrderEntries()) {
            if (!(entry instanceof ModuleOrderEntry))
                continue;
            final Module dependantModule = ((ModuleOrderEntry) entry).getModule();
            if (dependantModule == null)
                continue;
            LanguageLevel dependantLanguageLevel = LanguageLevelModuleExtensionImpl.getInstance(dependantModule).getLanguageLevel();
            if (dependantLanguageLevel == null) {
                dependantLanguageLevel = projectLanguageLevel;
            }
            if (languageLevel.compareTo(dependantLanguageLevel) < 0) {
                final CommonProblemDescriptor problemDescriptor = manager.createProblemDescriptor("Inconsistent language level settings: module " + module.getName() + " with language level " + languageLevel + " depends on module " + dependantModule.getName() + " with language level " + dependantLanguageLevel, new UnnecessaryModuleDependencyInspection.RemoveModuleDependencyFix(module, dependantModule), (QuickFix) QuickFixFactory.getInstance().createShowModulePropertiesFix(module));
                problemProcessor.addProblemElement(refModule, problemDescriptor);
            }
        }
    }
}
Also used : RefManager(com.intellij.codeInspection.reference.RefManager) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) THashSet(gnu.trove.THashSet) RefModule(com.intellij.codeInspection.reference.RefModule) UnnecessaryModuleDependencyInspection(com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection) LanguageLevel(com.intellij.pom.java.LanguageLevel) RefModule(com.intellij.codeInspection.reference.RefModule) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Aggregations

RefModule (com.intellij.codeInspection.reference.RefModule)8 Module (com.intellij.openapi.module.Module)5 Nullable (org.jetbrains.annotations.Nullable)5 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)4 RefClass (com.intellij.codeInspection.reference.RefClass)4 RefEntity (com.intellij.codeInspection.reference.RefEntity)4 RefManager (com.intellij.codeInspection.reference.RefManager)2 RefPackage (com.intellij.codeInspection.reference.RefPackage)2 PsiElement (com.intellij.psi.PsiElement)2 UnnecessaryModuleDependencyInspection (com.intellij.codeInspection.unnecessaryModuleDependency.UnnecessaryModuleDependencyInspection)1 Project (com.intellij.openapi.project.Project)1 ModuleOrderEntry (com.intellij.openapi.roots.ModuleOrderEntry)1 ModuleRootManager (com.intellij.openapi.roots.ModuleRootManager)1 OrderEntry (com.intellij.openapi.roots.OrderEntry)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 PsiClass (com.intellij.psi.PsiClass)1 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)1 PsiIdentifier (com.intellij.psi.PsiIdentifier)1 THashSet (gnu.trove.THashSet)1