void () { final Project project = myContainingClass.getProject(); final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory(); final PsiTypeElement newTypeElement = elementFactory.createTypeElementFromText(myObservableType.myText, myField); myTypeElement.replace(newTypeElement); final PsiExpression initializer = myField.getInitializer(); final String propertyName = JavaCodeStyleManager.getInstance(project).variableNameToPropertyName(myFieldName, VariableKind.FIELD); final String initializerArgs = "this,\"" + propertyName + "\"" + (initializer == null ? "" : "," + initializer.getText()); String initializerText = "new " + myObservableType.myText + "(" + initializerArgs + ")"; final PsiNewExpression newInitializer = (PsiNewExpression)elementFactory.createExpressionFromText(initializerText, myField); myField.setInitializer(newInitializer); final PsiType fieldType = myField.getType(); if (PsiDiamondTypeUtil.canCollapseToDiamond(newInitializer, (PsiNewExpression)myField.getInitializer(), fieldType)) { final PsiJavaCodeReferenceElement classReference = newInitializer.getClassOrAnonymousClassReference(); if (classReference != null) { RemoveRedundantTypeArgumentsUtil.replaceExplicitWithDiamond(classReference.getParameterList()); } } myField.setInitializer(newInitializer); final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); codeStyleManager.reformat(javaCodeStyleManager.shortenClassReferences(myField)); }