PsiClass (@NotNull PsiClass generatedInnerClass, @NotNull PsiElementFactory elementFactory, @NotNull PsiElement anchor) { Project project = generatedInnerClass.getProject(); PsiClass extractedClass = elementFactory.createClass("GeneratedEvaluationClass"); for (PsiField field : generatedInnerClass.getAllFields()) { extractedClass.add(elementFactory.createFieldFromText(field.getText(), anchor)); // TODO: check if null is OK } for (PsiMethod psiMethod : generatedInnerClass.getMethods()) { extractedClass.add(elementFactory.createMethodFromText(psiMethod.getText(), anchor)); } PsiJavaFile generatedFile = (PsiJavaFile)PsiFileFactory.getInstance(project) .createFileFromText(extractedClass.getName() + ".java", JavaFileType.INSTANCE, extractedClass.getContainingFile().getText()); // copy.getModificationStamp(), //false, false); generatedFile.setPackageName(GENERATED_CLASS_PACKAGE); extractedClass = PsiTreeUtil.findChildOfType(generatedFile, PsiClass.class); copyStaticImports(generatedInnerClass, generatedFile, elementFactory); assert extractedClass != null; PsiElement codeBlock = PsiTreeUtil.findFirstParent(anchor, false, element -> element instanceof PsiCodeBlock); if (codeBlock == null) { codeBlock = anchor.getParent(); } addGeneratedClassInfo(codeBlock, generatedInnerClass, extractedClass); return extractedClass; }