void (PsiType type) { if (myAnchor instanceof PsiStatement && CommonJavaRefactoringUtil.isLoopOrIf(myAnchor.getParent())) { myAnchor = BlockUtils.expandSingleStatementToBlockStatement((PsiStatement)myAnchor); } if (myAnchor instanceof PsiInstanceOfExpression && PsiUtil.skipParenthesizedExprDown(myExpression) instanceof PsiTypeCastExpression) { return; } if (myAnchor instanceof PsiExpression) { CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression((PsiExpression)myAnchor); boolean addedCast = false; if (surrounder == null) { PsiExpression typedAnchor = JavaPsiFacade.getElementFactory(myProject) .createExpressionFromText("(" + type.getCanonicalText() + ")" + myAnchor.getText(), null); myAnchor = myAnchor.replace(typedAnchor); addedCast = true; surrounder = CodeBlockSurrounder.forExpression((PsiExpression)myAnchor); } if (surrounder == null) { throw new RuntimeExceptionWithAttachments( "Cannot ensure code block: myAnchor type is " + myAnchor.getClass() + "; parent type is " + myAnchor.getParent().getClass(), new Attachment("context.txt", myContainer.getText())); } CodeBlockSurrounder.SurroundResult result = surrounder.surround(); myAnchor = result.getAnchor(); PsiExpression expression = result.getExpression(); if (addedCast && expression instanceof PsiTypeCastExpression) { expression.replace(Objects.requireNonNull(((PsiTypeCastExpression)expression).getOperand())); } } }