void () { final PsiManager manager = sourceClass.getManager(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(manager.getProject()); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); @NonNls final StringBuilder fieldBuffer = new StringBuilder(); final String delegateVisibility = calculateDelegateVisibility(); if (delegateVisibility.length() > 0) fieldBuffer.append(delegateVisibility).append(' '); fieldBuffer.append("final "); final String fullyQualifiedName = getQualifiedName(); fieldBuffer.append(fullyQualifiedName); if (!typeParams.isEmpty()) { fieldBuffer.append('<'); fieldBuffer.append(StringUtil.join(typeParams, param -> param.getName(), ", ")); fieldBuffer.append('>'); } fieldBuffer.append(' '); fieldBuffer.append(delegateFieldName); fieldBuffer.append(" = new ").append(fullyQualifiedName); if (!typeParams.isEmpty()) { fieldBuffer.append('<'); fieldBuffer.append(StringUtil.join(typeParams, param -> param.getName(), ", ")); fieldBuffer.append('>'); } fieldBuffer.append('('); if (requiresBackpointer) { fieldBuffer.append("this"); } fieldBuffer.append(");"); try { final String fieldString = fieldBuffer.toString(); final PsiField field = factory.createFieldFromText(fieldString, sourceClass); final PsiElement newField = sourceClass.add(field); codeStyleManager.reformat(JavaCodeStyleManager.getInstance(myProject).shortenClassReferences(newField)); } catch (IncorrectOperationException e) { LOG.error(e); } }