PsiExpression (PsiTypeCastExpression castExpression) { if (castExpression == null) return null; PsiElement parent = castExpression.getParent(); PsiExpression operand = castExpression.getOperand(); if (operand instanceof PsiParenthesizedExpression parExpr) { PsiElement topParent = PsiUtil.skipParenthesizedExprUp(parent); if (!(topParent instanceof PsiExpression) || !PsiPrecedenceUtil.areParenthesesNeeded(parExpr.getExpression(), (PsiExpression)topParent, true)) { operand = parExpr.getExpression(); } } if (operand == null) return null; PsiExpression toBeReplaced = castExpression; while (parent instanceof PsiParenthesizedExpression) { toBeReplaced = (PsiExpression)parent; parent = parent.getParent(); } try { return (PsiExpression)new CommentTracker().replaceAndRestoreComments(toBeReplaced, operand); } catch (IncorrectOperationException e) { LOG.error(e); } return toBeReplaced; }