void (@NotNull Project project, String methodRefText, PsiType castType, PsiElement replacementTarget) { final Collection<PsiComment> comments = ContainerUtil.map(PsiTreeUtil.findChildrenOfType(replacementTarget, PsiComment.class), comment -> (PsiComment)comment.copy()); if (methodRefText != null) { final String canonicalText = castType.getCanonicalText(); final PsiExpression psiExpression = JavaPsiFacade .getElementFactory(project).createExpressionFromText("(" + canonicalText + ")" + methodRefText, replacementTarget); PsiElement castExpr = replacementTarget.replace(psiExpression); if (RedundantCastUtil.isCastRedundant((PsiTypeCastExpression)castExpr)) { final PsiExpression operand = ((PsiTypeCastExpression)castExpr).getOperand(); LOG.assertTrue(operand != null); castExpr = castExpr.replace(operand); } PsiElement anchor = PsiTreeUtil.getParentOfType(castExpr, PsiStatement.class); if (anchor == null) { anchor = castExpr; } for (PsiComment comment : comments) { anchor.getParent().addBefore(comment, anchor); } JavaCodeStyleManager.getInstance(project).shortenClassReferences(castExpr); } }