PsiClass (final PsiJavaCodeReferenceElement referenceElement, final CreateClassKind classKind, final String superClassName) { assert !ApplicationManager.getApplication().isWriteAccessAllowed() : "You must not run createClass() from under write action"; final String name = referenceElement.getReferenceName(); String qualifierName; final PsiElement qualifierElement; PsiElement qualifier = referenceElement.getQualifier(); if (qualifier instanceof PsiJavaCodeReferenceElement) { qualifierName = ((PsiJavaCodeReferenceElement)qualifier).getQualifiedName(); qualifierElement = ((PsiJavaCodeReferenceElement)qualifier).resolve(); if (qualifierElement instanceof PsiClass) { if (!FileModificationService.getInstance().preparePsiElementForWrite(qualifierElement)) return null; return WriteAction.compute(() -> createClassInQualifier((PsiClass)qualifierElement, classKind, name, referenceElement, superClassName)); } } else { qualifierName = null; qualifierElement = null; } final PsiManager manager = referenceElement.getManager(); final PsiFile sourceFile = referenceElement.getContainingFile(); final Module module = ModuleUtilCore.findModuleForPsiElement(sourceFile); if (qualifierName == null) { PsiPackage aPackage = findTargetPackage(qualifierElement, manager, sourceFile); if (aPackage == null) return null; qualifierName = aPackage.getQualifiedName(); } final PsiDirectory targetDirectory; if (!ApplicationManager.getApplication().isUnitTestMode() && !ScratchUtil.isScratch(referenceElement.getContainingFile().getVirtualFile())) { Project project = manager.getProject(); String title = CommonQuickFixBundle.message("fix.create.title", StringUtil.capitalize(classKind.getDescriptionAccusative())); CreateClassDialog dialog = new CreateClassDialog(project, title, name, qualifierName, classKind, false, module){ @Override protected @Nullable PsiDirectory getBaseDir(String packageName) { return sourceFile.getContainingDirectory(); } @Override protected boolean reportBaseInSourceSelectionInTest() { return true; } }; dialog.show(); if (dialog.getExitCode() != DialogWrapper.OK_EXIT_CODE) return null; targetDirectory = dialog.getTargetDirectory(); if (targetDirectory == null) return null; } else { if (!FileModificationService.getInstance().prepareFileForWrite(sourceFile)) { return null; } targetDirectory = null; } return createClass(classKind, targetDirectory, name, manager, referenceElement, sourceFile, superClassName); }