@Override
public void visit(final GroovyCodeVisitor visitor) {
if (visitor instanceof AsmClassGenerator) {
AsmClassGenerator acg = (AsmClassGenerator) visitor;
WriterController controller = acg.getController();
OperandStack os = controller.getOperandStack();
if (type.equals(ClassHelper.boolean_TYPE)) {
expression.visit(visitor);
os.doGroovyCast(ClassHelper.boolean_TYPE);
return;
}
if (type.equals(ClassHelper.Boolean_TYPE)) {
MethodVisitor mv = controller.getMethodVisitor();
expression.visit(visitor);
Label unbox = new Label();
Label exit = new Label();
// check for null
mv.visitInsn(DUP);
mv.visitJumpInsn(IFNONNULL, unbox);
mv.visitInsn(POP);
mv.visitInsn(ICONST_0);
mv.visitJumpInsn(GOTO, exit);
mv.visitLabel(unbox);
// unbox
// GROOVY-6270
if (!os.getTopOperand().equals(type)) BytecodeHelper.doCast(mv, type);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Boolean", "booleanValue", "()Z", false);
mv.visitLabel(exit);
os.replace(ClassHelper.boolean_TYPE);
return;
}
ClassNode top = type;
if (ClassHelper.isPrimitiveType(top)) {
expression.visit(visitor);
// in case of null safe invocation, it is possible that what was supposed to be a primitive type
// becomes the "null" constant, so we need to recheck
top = controller.getOperandStack().getTopOperand();
if (ClassHelper.isPrimitiveType(top)) {
if (top.equals(ClassHelper.int_TYPE) || top.equals(ClassHelper.byte_TYPE)
|| top.equals(ClassHelper.short_TYPE) || top.equals(ClassHelper.char_TYPE)) {
// int on stack
} else if (top.equals(ClassHelper.long_TYPE)) {
MethodVisitor mv = controller.getMethodVisitor();
mv.visitInsn(L2I);
controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
} else if (top.equals(ClassHelper.float_TYPE)) {
MethodVisitor mv = controller.getMethodVisitor();
mv.visitInsn(F2I);
controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
} else if (top.equals(ClassHelper.double_TYPE)) {
MethodVisitor mv = controller.getMethodVisitor();
mv.visitInsn(D2I);
controller.getOperandStack().replace(ClassHelper.boolean_TYPE);
}
return;
}
}
List<MethodNode> asBoolean = findDGMMethodsByNameAndArguments(controller.getSourceUnit().getClassLoader(), top, "asBoolean", ClassNode.EMPTY_ARRAY);
if (asBoolean.size() == 1) {
MethodNode node = asBoolean.get(0);
if (node instanceof ExtensionMethodNode) {
MethodNode dgmNode = ((ExtensionMethodNode) node).getExtensionMethodNode();
ClassNode owner = dgmNode.getParameters()[0].getType();