MethodVisitor mv = controller.getMethodVisitor();
if (coerce) {
if (top.isDerivedFrom(targetType)) return;
box();
(new ClassExpression(targetType)).visit(controller.getAcg());
remove(1);
asTypeMethod.call(mv);
BytecodeHelper.doCast(mv,targetType);
replace(targetType);
return;
}
boolean primTarget = ClassHelper.isPrimitiveType(targetType);
boolean primTop = ClassHelper.isPrimitiveType(top);
if (primTop && primTarget) {
//TODO: use jvm primitive conversions
// here we box and unbox to get the goal type
if (convertPrimitive(top, targetType)) {
replace(targetType);
return;
}
box();
} else if (primTop) {
// top is primitive, target is not
// so box and do groovy cast
box();
(new ClassExpression(targetType)).visit(controller.getAcg());
remove(1);
castToTypeMethod.call(mv);
} else if (primTarget) {
// top is not primitive so unbox
// leave that BH#doCast later
} else if (!(top.isDerivedFrom(targetType))) {
// top and target are not primitive and top is not derived from target
(new ClassExpression(targetType)).visit(controller.getAcg());
remove(1);
castToTypeMethod.call(mv);
}
BytecodeHelper.doCast(mv,targetType);
replace(targetType);