Search in sources :

Example 1 with ClassGeneratorException

use of org.codehaus.groovy.classgen.ClassGeneratorException in project groovy by apache.

the class OperandStack method pushConstant.

/**
     * load the constant on the operand stack. 
     */
public void pushConstant(ConstantExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    Object value = expression.getValue();
    ClassNode origType = expression.getType().redirect();
    ClassNode type = ClassHelper.getUnwrapper(origType);
    boolean boxing = origType != type;
    boolean asPrimitive = boxing || ClassHelper.isPrimitiveType(type);
    if (value == null) {
        mv.visitInsn(ACONST_NULL);
    } else if (boxing && value instanceof Boolean) {
        // special path for boxed boolean
        Boolean bool = (Boolean) value;
        String text = bool ? "TRUE" : "FALSE";
        mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", text, "Ljava/lang/Boolean;");
        boxing = false;
        type = origType;
    } else if (asPrimitive) {
        pushPrimitiveConstant(mv, value, type);
    } else if (value instanceof BigDecimal) {
        String className = BytecodeHelper.getClassInternalName(value.getClass().getName());
        mv.visitTypeInsn(NEW, className);
        mv.visitInsn(DUP);
        mv.visitLdcInsn(value.toString());
        mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "(Ljava/lang/String;)V", false);
    } else if (value instanceof BigInteger) {
        String className = BytecodeHelper.getClassInternalName(value.getClass().getName());
        mv.visitTypeInsn(NEW, className);
        mv.visitInsn(DUP);
        mv.visitLdcInsn(value.toString());
        mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "(Ljava/lang/String;)V", false);
    } else if (value instanceof String) {
        mv.visitLdcInsn(value);
    } else {
        throw new ClassGeneratorException("Cannot generate bytecode for constant: " + value + " of type: " + type.getName());
    }
    push(type);
    if (boxing)
        box();
}
Also used : BigInteger(java.math.BigInteger) ClassGeneratorException(org.codehaus.groovy.classgen.ClassGeneratorException) BigDecimal(java.math.BigDecimal) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 2 with ClassGeneratorException

use of org.codehaus.groovy.classgen.ClassGeneratorException in project groovy-core by groovy.

the class OperandStack method pushConstant.

/**
     * load the constant on the operand stack. 
     */
public void pushConstant(ConstantExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    Object value = expression.getValue();
    ClassNode origType = expression.getType().redirect();
    ClassNode type = ClassHelper.getUnwrapper(origType);
    boolean boxing = origType != type;
    boolean asPrimitive = boxing || ClassHelper.isPrimitiveType(type);
    if (value == null) {
        mv.visitInsn(ACONST_NULL);
    } else if (boxing && value instanceof Boolean) {
        // special path for boxed boolean
        Boolean bool = (Boolean) value;
        String text = bool ? "TRUE" : "FALSE";
        mv.visitFieldInsn(GETSTATIC, "java/lang/Boolean", text, "Ljava/lang/Boolean;");
        boxing = false;
        type = origType;
    } else if (asPrimitive) {
        pushPrimitiveConstant(mv, value, type);
    } else if (value instanceof BigDecimal) {
        String className = BytecodeHelper.getClassInternalName(value.getClass().getName());
        mv.visitTypeInsn(NEW, className);
        mv.visitInsn(DUP);
        mv.visitLdcInsn(value.toString());
        mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "(Ljava/lang/String;)V", false);
    } else if (value instanceof BigInteger) {
        String className = BytecodeHelper.getClassInternalName(value.getClass().getName());
        mv.visitTypeInsn(NEW, className);
        mv.visitInsn(DUP);
        mv.visitLdcInsn(value.toString());
        mv.visitMethodInsn(INVOKESPECIAL, className, "<init>", "(Ljava/lang/String;)V", false);
    } else if (value instanceof String) {
        mv.visitLdcInsn(value);
    } else {
        throw new ClassGeneratorException("Cannot generate bytecode for constant: " + value + " of type: " + type.getName());
    }
    push(type);
    if (boxing)
        box();
}
Also used : BigInteger(java.math.BigInteger) ClassGeneratorException(org.codehaus.groovy.classgen.ClassGeneratorException) BigDecimal(java.math.BigDecimal) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 ClassGeneratorException (org.codehaus.groovy.classgen.ClassGeneratorException)2 MethodVisitor (org.objectweb.asm.MethodVisitor)2