Java Code Examples for org.codehaus.groovy.ast.ClassHelper#long_TYPE()

The following examples show how to use org.codehaus.groovy.ast.ClassHelper#long_TYPE() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroovyVirtualSourceProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void printTypeName(ClassNode type, PrintWriter out) {
    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.boolean_TYPE) {
            out.print("boolean");
        } else if (type == ClassHelper.char_TYPE) {
            out.print("char");
        } else if (type == ClassHelper.int_TYPE) {
            out.print("int");
        } else if (type == ClassHelper.short_TYPE) {
            out.print("short");
        } else if (type == ClassHelper.long_TYPE) {
            out.print("long");
        } else if (type == ClassHelper.float_TYPE) {
            out.print("float");
        } else if (type == ClassHelper.double_TYPE) {
            out.print("double");
        } else if (type == ClassHelper.byte_TYPE) {
            out.print("byte");
        } else {
            out.print("void");
        }
    } else {
        out.print(type.redirect().getName().replace('$', '.'));
    }
}
 
Example 2
Source File: OperandStack.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean convertFromInt(ClassNode target) {
    int convertCode;
    if (target==ClassHelper.char_TYPE){
        convertCode = I2C;
    } else if (target==ClassHelper.byte_TYPE){
        convertCode = I2B;
    } else if (target==ClassHelper.short_TYPE){
        convertCode = I2S;
    } else if (target==ClassHelper.long_TYPE){
        convertCode = I2L;
    } else if (target==ClassHelper.float_TYPE){
        convertCode = I2F;
    } else if (target==ClassHelper.double_TYPE){
        convertCode = I2D;
    } else {
        return false;
    }
    controller.getMethodVisitor().visitInsn(convertCode);
    return true;
}
 
Example 3
Source File: OperandStack.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean convertFromDouble(ClassNode target) {
    MethodVisitor mv = controller.getMethodVisitor();
    if (target==ClassHelper.int_TYPE){
        mv.visitInsn(D2I);
        return true;
    } else if ( target==ClassHelper.char_TYPE ||
                target==ClassHelper.byte_TYPE ||
                target==ClassHelper.short_TYPE)
    {
        mv.visitInsn(D2I);
        return convertFromInt(target);
    } else if (target==ClassHelper.long_TYPE){
        mv.visitInsn(D2L);
        return true;
    } else if (target==ClassHelper.float_TYPE){
        mv.visitInsn(D2F);
        return true;
    } 
    return false;
}
 
Example 4
Source File: OperandStack.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean convertFromFloat(ClassNode target) {
    MethodVisitor mv = controller.getMethodVisitor();
    if (target==ClassHelper.int_TYPE){
        mv.visitInsn(F2I);
        return true;
    } else if ( target==ClassHelper.char_TYPE ||
                target==ClassHelper.byte_TYPE ||
                target==ClassHelper.short_TYPE)
    {
        mv.visitInsn(F2I);
        return convertFromInt(target);
    } else if (target==ClassHelper.long_TYPE){
        mv.visitInsn(F2L);
        return true;
    } else if (target==ClassHelper.double_TYPE){
        mv.visitInsn(F2D);
        return true;
    } 
    return false;
}
 
Example 5
Source File: OperandStack.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean convertPrimitive(ClassNode top, ClassNode target) {
    if (top==target) return true;
    if (top==ClassHelper.int_TYPE) {
        return convertFromInt(target);
    } else if ( top==ClassHelper.char_TYPE || 
                top==ClassHelper.byte_TYPE ||
                top==ClassHelper.short_TYPE)
    {
        return target == ClassHelper.int_TYPE || convertFromInt(target);
    } else if ( top==ClassHelper.float_TYPE) {
        return convertFromFloat(target);
    } else if ( top==ClassHelper.double_TYPE) {
        return convertFromDouble(target);
    } else if ( top==ClassHelper.long_TYPE) {
        return convertFromLong(target);
    }
    return false;
}
 
Example 6
Source File: ExternalizeMethodsASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static String suffixForField(FieldNode fNode) {
        // use primitives for efficiency
        if (fNode.getType() == ClassHelper.int_TYPE) return "Int";
        if (fNode.getType() == ClassHelper.boolean_TYPE) return "Boolean";
//        currently char isn't found due to a bug, so go with Object
//        if (fNode.getType() == ClassHelper.char_TYPE) return "Char";
        if (fNode.getType() == ClassHelper.long_TYPE) return "Long";
        if (fNode.getType() == ClassHelper.short_TYPE) return "Short";
        if (fNode.getType() == ClassHelper.byte_TYPE) return "Byte";
        if (fNode.getType() == ClassHelper.float_TYPE) return "Float";
        if (fNode.getType() == ClassHelper.double_TYPE) return "Double";
        return "Object";
    }
 
Example 7
Source File: PrimitiveHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static Expression getDefaultValueForPrimitive(ClassNode type) {
    if (type == ClassHelper.int_TYPE) {
        return new ConstantExpression(0);
    }
    if (type == ClassHelper.long_TYPE) {
        return new ConstantExpression(0L);
    }
    if (type == ClassHelper.double_TYPE) {
        return new ConstantExpression(0.0);
    }
    if (type == ClassHelper.float_TYPE) {
        return new ConstantExpression(0.0F);
    }
    if (type == ClassHelper.boolean_TYPE) {
        return ConstantExpression.FALSE;
    }
    if (type == ClassHelper.short_TYPE) {
        return new ConstantExpression((short) 0);
    }
    if (type == ClassHelper.byte_TYPE) {
        return new ConstantExpression((byte) 0);
    }
    if (type == ClassHelper.char_TYPE) {
        return new ConstantExpression((char) 0);
    }
    return null;
}
 
Example 8
Source File: OperandStack.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * duplicate top element
 */
public void dup() {
    ClassNode type = getTopOperand();
    stack.add(type);
    MethodVisitor mv = controller.getMethodVisitor();
    if (type == ClassHelper.double_TYPE || type == ClassHelper.long_TYPE) {
        mv.visitInsn(DUP2);
    } else {
        mv.visitInsn(DUP);
    }
}
 
Example 9
Source File: MopWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Generates a Meta Object Protocol method, that is used to call a non public
 * method, or to make a call to super.
 *
 * @param mopCalls list of methods a mop call method should be generated for
 * @param useThis  true if "this" should be used for the naming
 */
protected void generateMopCalls(LinkedList<MethodNode> mopCalls, boolean useThis) {
    for (MethodNode method : mopCalls) {
        String name = getMopMethodName(method, useThis);
        Parameter[] parameters = method.getParameters();
        String methodDescriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameters());
        MethodVisitor mv = controller.getClassVisitor().visitMethod(ACC_PUBLIC | ACC_SYNTHETIC, name, methodDescriptor, null, null);
        controller.setMethodVisitor(mv);
        mv.visitVarInsn(ALOAD, 0);
        int newRegister = 1;
        OperandStack operandStack = controller.getOperandStack();
        for (Parameter parameter : parameters) {
            ClassNode type = parameter.getType();
            operandStack.load(parameter.getType(), newRegister);
            newRegister += 1; // increment to next register; double/long are using two places
            if (type == ClassHelper.double_TYPE || type == ClassHelper.long_TYPE) newRegister += 1;
        }
        operandStack.remove(parameters.length);
        ClassNode declaringClass = method.getDeclaringClass();
        // JDK 8 support for default methods in interfaces
        // TODO: this should probably be strengthened when we support the A.super.foo() syntax
        int opcode = declaringClass.isInterface() ? INVOKEINTERFACE : INVOKESPECIAL;
        mv.visitMethodInsn(opcode, BytecodeHelper.getClassInternalName(declaringClass), method.getName(), methodDescriptor, declaringClass.isInterface());
        BytecodeHelper.doReturn(mv, method.getReturnType());
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        controller.getClassNode().addMethod(name, ACC_PUBLIC | ACC_SYNTHETIC, method.getReturnType(), parameters, null, null);
    }
}
 
Example 10
Source File: CompileStack.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void pushInitValue(final ClassNode type, final MethodVisitor mv) {
    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.long_TYPE) {
            mv.visitInsn(LCONST_0);
        } else if (type == ClassHelper.double_TYPE) {
            mv.visitInsn(DCONST_0);
        } else if (type == ClassHelper.float_TYPE) {
            mv.visitInsn(FCONST_0);
        } else {
            mv.visitLdcInsn(0);
        }
    } else {
        mv.visitInsn(ACONST_NULL);
    }
}
 
Example 11
Source File: CompileStack.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the index of the next free register stores it
 * and sets the current variable index to the old value
 */
private void makeNextVariableID(final ClassNode type, final boolean useReferenceDirectly) {
    currentVariableIndex = nextVariableIndex;
    if ((type == ClassHelper.long_TYPE || type == ClassHelper.double_TYPE) && !useReferenceDirectly) {
        nextVariableIndex += 1;
    }
    nextVariableIndex += 1;
}
 
Example 12
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void printTypeName(PrintWriter out, ClassNode type) {
    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.boolean_TYPE) {
            out.print("boolean");
        } else if (type == ClassHelper.char_TYPE) {
            out.print("char");
        } else if (type == ClassHelper.int_TYPE) {
            out.print("int");
        } else if (type == ClassHelper.short_TYPE) {
            out.print("short");
        } else if (type == ClassHelper.long_TYPE) {
            out.print("long");
        } else if (type == ClassHelper.float_TYPE) {
            out.print("float");
        } else if (type == ClassHelper.double_TYPE) {
            out.print("double");
        } else if (type == ClassHelper.byte_TYPE) {
            out.print("byte");
        } else {
            out.print("void");
        }
    } else {
        String name = type.getName();
        // check for an alias
        ClassNode alias = currentModule.getImportType(name);
        if (alias != null) name = alias.getName();
        out.print(name.replace('$', '.'));
    }
}
 
Example 13
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public void visitArrayExpression(final ArrayExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    ClassNode elementType = expression.getElementType();
    String arrayTypeName = BytecodeHelper.getClassInternalName(elementType);
    List<Expression> sizeExpression = expression.getSizeExpression();

    int size = 0;
    int dimensions = 0;
    if (sizeExpression != null) {
        for (Expression element : sizeExpression) {
            if (element == ConstantExpression.EMPTY_EXPRESSION) break;
            dimensions += 1;
            // let's convert to an int
            element.visit(this);
            controller.getOperandStack().doGroovyCast(ClassHelper.int_TYPE);
        }
        controller.getOperandStack().remove(dimensions);
    } else {
        size = expression.getExpressions().size();
        BytecodeHelper.pushConstant(mv, size);
    }

    int storeIns = AASTORE;
    if (sizeExpression != null) {
        arrayTypeName = BytecodeHelper.getTypeDescription(expression.getType());
        mv.visitMultiANewArrayInsn(arrayTypeName, dimensions);
    } else if (ClassHelper.isPrimitiveType(elementType)) {
        int primType = 0;
        if (elementType == ClassHelper.boolean_TYPE) {
            primType = T_BOOLEAN;
            storeIns = BASTORE;
        } else if (elementType == ClassHelper.char_TYPE) {
            primType = T_CHAR;
            storeIns = CASTORE;
        } else if (elementType == ClassHelper.float_TYPE) {
            primType = T_FLOAT;
            storeIns = FASTORE;
        } else if (elementType == ClassHelper.double_TYPE) {
            primType = T_DOUBLE;
            storeIns = DASTORE;
        } else if (elementType == ClassHelper.byte_TYPE) {
            primType = T_BYTE;
            storeIns = BASTORE;
        } else if (elementType == ClassHelper.short_TYPE) {
            primType = T_SHORT;
            storeIns = SASTORE;
        } else if (elementType == ClassHelper.int_TYPE) {
            primType = T_INT;
            storeIns = IASTORE;
        } else if (elementType == ClassHelper.long_TYPE) {
            primType = T_LONG;
            storeIns = LASTORE;
        }
        mv.visitIntInsn(NEWARRAY, primType);
    } else {
        mv.visitTypeInsn(ANEWARRAY, arrayTypeName);
    }

    for (int i = 0; i < size; i += 1) {
        mv.visitInsn(DUP);
        BytecodeHelper.pushConstant(mv, i);
        Expression elementExpression = expression.getExpression(i);
        if (elementExpression == null) {
            ConstantExpression.NULL.visit(this);
        } else {
            elementExpression.visit(this);
            controller.getOperandStack().doGroovyCast(elementType);
        }
        mv.visitInsn(storeIns);
        controller.getOperandStack().remove(1);
    }

    controller.getOperandStack().push(expression.getType());
}
 
Example 14
Source File: OperandStack.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * returns true for long and double
 */
private static boolean isTwoSlotType(ClassNode type) {
    return type==ClassHelper.long_TYPE || type==ClassHelper.double_TYPE;
}
 
Example 15
Source File: BinaryLongExpressionHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected ClassNode getNormalOpResultType() {
    return ClassHelper.long_TYPE;
}
 
Example 16
Source File: BinaryLongExpressionHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected ClassNode getDevisionOpResultType() {
    return ClassHelper.long_TYPE;
}