GrExpression (GrVariable variable, GrExpression initializer) { PsiType ltype = findLValueType(initializer); PsiType rtype = initializer.getType(); GrExpression rawExpr = (GrExpression)PsiUtil.skipParentheses(initializer, false); if (ltype == null || TypesUtil.isAssignableWithoutConversions(ltype, rtype) || !TypesUtil.isAssignable(ltype, rtype, initializer)) { return rawExpr; } else { // implicit coercion should be replaced with explicit cast GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(variable.getProject()); GrSafeCastExpression cast = (GrSafeCastExpression)factory.createExpressionFromText("a as B"); cast.getOperand().replaceWithExpression(rawExpr, false); cast.getCastTypeElement().replace(factory.createTypeElement(ltype)); return cast; } }