void (Project project, PsiType type, GrExpression expr) { if (!type.isValid()) return; final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project); final GrTypeCastExpression cast = (GrTypeCastExpression)factory.createExpressionFromText("(String)foo"); final GrTypeElement typeElement = factory.createTypeElement(type); GrExpression operand = cast.getOperand(); if (operand == null) return; operand.replaceWithExpression(expr, true); cast.getCastTypeElement().replace(typeElement); final GrExpression replaced = expr.replaceWithExpression(cast, true); JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced); }