void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element instanceof PsiTypeElement typeElement)) { return; } final PsiElement parent = typeElement.getParent(); if (!(parent instanceof PsiMethod)) { return; } final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiTypeElement newTypeElement = factory.createTypeElementFromText(myClassName, element); final PsiType newType = newTypeElement.getType(); parent.accept(new JavaRecursiveElementVisitor() { @Override public void visitClass(@NotNull PsiClass aClass) {} @Override public void visitLambdaExpression(@NotNull PsiLambdaExpression expression) {} @Override public void visitReturnStatement(@NotNull PsiReturnStatement statement) { super.visitReturnStatement(statement); final PsiExpression returnValue = PsiUtil.deparenthesizeExpression(statement.getReturnValue()); if (returnValue == null) { return; } final PsiType type = returnValue.getType(); if (newType.equals(type) || PsiTypes.nullType().equals(type)) { return; } final CommentTracker commentTracker = new CommentTracker(); PsiReplacementUtil.replaceStatement(statement, "return (" + myClassName + ')' + commentTracker.text(returnValue) + ';', commentTracker); } }); typeElement.getFirstChild().replace(newTypeElement.getFirstChild()); }