PsiClass (String qualifiedName, PsiDirectory baseDirectory, Project project, Module module) { GlobalSearchScope scope = GlobalSearchScope.projectScope(project); PsiClass targetClass = JavaPsiFacade.getInstance(project).findClass(qualifiedName, scope); if (targetClass != null) return targetClass; final String packageName = StringUtil.getPackageName(qualifiedName); PsiPackage psiPackage = JavaPsiFacade.getInstance(project).findPackage(packageName); final PsiDirectory psiDirectory; if (psiPackage != null) { final PsiDirectory[] directories = psiPackage.getDirectories(GlobalSearchScope.allScope(project)); psiDirectory = directories.length > 1 ? DirectoryChooserUtil .chooseDirectory(directories, null, project, new HashMap<>()) : directories[0]; } else { psiDirectory = PackageUtil.findOrCreateDirectoryForPackage(module, packageName, baseDirectory, false); } if (psiDirectory == null) return null; final String shortName = StringUtil.getShortName(qualifiedName); final String fileName = shortName + NewGroovyActionBase.GROOVY_EXTENSION; return WriteAction.compute(() -> { final GroovyFile file = (GroovyFile)GroovyTemplatesFactory.createFromTemplate(psiDirectory, shortName, fileName, GroovyTemplates.GROOVY_CLASS, true); return file.getTypeDefinitions()[0]; }); }