PsiDirectory ( @NotNull Module module, @NotNull String packageName, boolean preferNonGeneratedRoots ) { final Project project = module.getProject(); PsiDirectory psiDirectory = null; if (!StringUtil.isEmptyOrSpaces(packageName)) { PsiPackage rootPackage = findLongestExistingPackage(project, packageName); if (rootPackage != null) { final PsiDirectory[] psiDirectories = getPackageDirectoriesInModule(rootPackage, module); if (psiDirectories.length > 0) { psiDirectory = psiDirectories[0]; // If we prefer to find a non-generated PsiDirectory for the given package name, search through all // the directories for the first dir not marked as generated and use that one instead if (preferNonGeneratedRoots && psiDirectories.length > 1) { for (PsiDirectory dir : psiDirectories) { if (!GeneratedSourcesFilter.isGeneratedSourceByAnyFilter(dir.getVirtualFile(), project)) { psiDirectory = dir; break; } } } } } } if (psiDirectory == null) { if (checkSourceRootsConfigured(module)) { final List<VirtualFile> sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots(JavaModuleSourceRootTypes.SOURCES); for (VirtualFile sourceRoot : sourceRoots) { final PsiDirectory directory = PsiManager.getInstance(project).findDirectory(sourceRoot); if (directory != null) { psiDirectory = directory; break; } } } } return psiDirectory; }