PsiJavaModule (@NotNull Module module, boolean inTests) { Project project = module.getProject(); GlobalSearchScope moduleScope = module.getModuleScope(); String virtualAutoModuleName = VirtualManifestProvider.getAttributeValue(module, PsiJavaModule.AUTO_MODULE_NAME); if (!DumbService.isDumb(project) && FilenameIndex.getVirtualFilesByName(PsiJavaModule.MODULE_INFO_FILE, moduleScope).isEmpty() && FilenameIndex.getVirtualFilesByName("MANIFEST.MF", moduleScope).isEmpty() && virtualAutoModuleName == null) { return null; } JavaSourceRootType rootType = inTests ? JavaSourceRootType.TEST_SOURCE : JavaSourceRootType.SOURCE; ModuleRootManager rootManager = ModuleRootManager.getInstance(module); List<VirtualFile> sourceRoots = rootManager.getSourceRoots(rootType); List<VirtualFile> files = ContainerUtil.mapNotNull(sourceRoots, root -> root.findChild(PsiJavaModule.MODULE_INFO_FILE)); if (files.size() == 1) { PsiFile psiFile = PsiManager.getInstance(project).findFile(files.get(0)); if (psiFile instanceof PsiJavaFile) { return ((PsiJavaFile)psiFile).getModuleDeclaration(); } } else if (files.isEmpty()) { JavaResourceRootType resourceRootType = inTests ? JavaResourceRootType.TEST_RESOURCE : JavaResourceRootType.RESOURCE; List<VirtualFile> roots = new ArrayList<>(rootManager.getSourceRoots(resourceRootType)); roots.addAll(sourceRoots); files = ContainerUtil.mapNotNull(roots, root -> root.findFileByRelativePath(JarFile.MANIFEST_NAME)); if (files.size() == 1) { VirtualFile manifest = files.get(0); PsiFile manifestPsi = PsiManager.getInstance(project).findFile(manifest); assert manifestPsi != null : manifest; return CachedValuesManager.getCachedValue(manifestPsi, () -> { String name = LightJavaModule.claimedModuleName(manifest); LightJavaModule result = name != null ? LightJavaModule.create(PsiManager.getInstance(project), manifest.getParent().getParent(), name) : null; return Result.create(result, manifestPsi, ProjectRootModificationTracker.getInstance(project)); }); } List<VirtualFile> sourceSourceRoots = rootManager.getSourceRoots(JavaSourceRootType.SOURCE); if (virtualAutoModuleName != null && !sourceSourceRoots.isEmpty()) { return LightJavaModule.create(PsiManager.getInstance(project), sourceSourceRoots.get(0), virtualAutoModuleName); } } return null; }