@ApiStatus.Internal
  public static void doSafeCast(@NotNull Project project, @NotNull PsiType type, @NotNull GrExpression expr) {
    if (!type.isValid()) return;

    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final GrSafeCastExpression cast = (GrSafeCastExpression)factory.createExpressionFromText("foo as String");
    final GrTypeElement typeElement = factory.createTypeElement(type);
    cast.getOperand().replaceWithExpression(expr, true);
    GrTypeElement castTypeElement = cast.getCastTypeElement();
    if (castTypeElement != null) {
      castTypeElement.replace(typeElement);
    }
    final GrExpression replaced = expr.replaceWithExpression(cast, true);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
  }
@ApiStatus.Internal
  public static void doSafeCast(@NotNull Project project, @NotNull PsiType type, @NotNull GrExpression expr)