use of com.google.devtools.j2objc.ast.CastExpression in project j2objc by google.
the class TreeConverter method convertFunctionalExpression.
private static TreeNode convertFunctionalExpression(org.eclipse.jdt.core.dom.Expression node, FunctionalExpression newNode) {
convertExpression(node, newNode);
ITypeBinding typeBinding = node.resolveTypeBinding();
newNode.setTypeMirror(BindingConverter.getType(typeBinding));
ASTNode parent = node.getParent();
// all of the target types in the node's type binding, so we check for a parent CastExpression.
if (parent instanceof org.eclipse.jdt.core.dom.CastExpression) {
typeBinding = ((org.eclipse.jdt.core.dom.CastExpression) parent).resolveTypeBinding();
}
if (BindingUtil.isIntersectionType(typeBinding)) {
for (ITypeBinding i : typeBinding.getInterfaces()) {
newNode.addTargetType(BindingConverter.getType(i));
}
} else {
newNode.addTargetType(BindingConverter.getType(typeBinding));
}
return newNode;
}
use of com.google.devtools.j2objc.ast.CastExpression in project j2objc by google.
the class Autoboxer method endVisit.
@Override
public void endVisit(CastExpression node) {
TypeMirror castType = node.getTypeMirror();
Expression expr = node.getExpression();
TypeMirror exprType = expr.getTypeMirror();
if (castType.getKind().isPrimitive() && !exprType.getKind().isPrimitive()) {
if (typeUtil.isAssignable(exprType, typeUtil.getJavaNumber().asType())) {
// Casting a Number object to a primitive, convert to value method.
unbox(expr, (PrimitiveType) castType);
} else if (exprType == typeUtil.boxedClass(typeUtil.getChar()).asType()) {
// Unboxing and casting Character, which does not have number value functions.
unbox(expr);
if (castType.getKind() != TypeKind.CHAR) {
// If the resulting type is not char - keep the cast, to preserve type information in
// case of reboxing.
CastExpression castExpr = new CastExpression(castType, null);
Expression unboxedExpression = node.getExpression();
unboxedExpression.replaceWith(castExpr);
castExpr.setExpression(unboxedExpression);
}
} else {
// Casting an object to a primitive. Convert the cast type to the wrapper
// so that we do a proper cast check, as Java would.
castType = typeUtil.boxedClass((PrimitiveType) castType).asType();
node.setType(Type.newType(castType));
boxOrUnboxExpression(expr, castType);
}
} else {
boxOrUnboxExpression(expr, castType);
}
Expression newExpr = node.getExpression();
if (newExpr != expr) {
TreeNode parent = node.getParent();
if (parent instanceof ParenthesizedExpression) {
parent.replaceWith(TreeUtil.remove(newExpr));
} else {
node.replaceWith(TreeUtil.remove(newExpr));
}
}
}
use of com.google.devtools.j2objc.ast.CastExpression in project j2objc by google.
the class CastResolver method endVisit.
@Override
public void endVisit(ConditionalExpression node) {
Expression thenExpr = node.getThenExpression();
Expression elseExpr = node.getElseExpression();
if (incompatibleTypes(thenExpr, elseExpr)) {
// Add (id) cast to else expression.
node.setElseExpression(new CastExpression(TypeUtil.ID_TYPE, TreeUtil.remove(elseExpr)));
}
}
use of com.google.devtools.j2objc.ast.CastExpression in project j2objc by google.
the class CastResolver method addCast.
private void addCast(Expression expr) {
CastExpression castExpr = new CastExpression(expr.getTypeMirror(), null);
expr.replaceWith(ParenthesizedExpression.parenthesize(castExpr));
castExpr.setExpression(expr);
}
Aggregations