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);
}
}
}
}
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) };
}
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;
}
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;
}
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);
}
}
}
}
Aggregations