PsiField (String fieldName, PsiType type, PsiExpression initializerExpr, boolean includeInitializer, final PsiClass parentClass) { @NonNls StringBuilder pattern = new StringBuilder(); pattern.append("private int "); pattern.append(fieldName); if (includeInitializer) { pattern.append("=0"); } pattern.append(";"); PsiManager psiManager = parentClass.getManager(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(psiManager.getProject()); try { PsiField field = factory.createFieldFromText(pattern.toString(), null); final PsiTypeElement typeElement = factory.createTypeElement(type); field.getTypeElement().replace(typeElement); field = (PsiField)CodeStyleManager.getInstance(psiManager.getProject()).reformat(field); if (includeInitializer) { field.getInitializer().replace(initializerExpr); } return field; } catch (IncorrectOperationException e) { LOG.error(e); return null; } }