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

The following examples show how to use org.codehaus.groovy.ast.ClassHelper#boolean_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 printDefaultValue(PrintWriter out, ClassNode type) {
    if (type.redirect() != ClassHelper.OBJECT_TYPE) {
        out.print("(");
        printType(type, out);
        out.print(")");
    }

    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.boolean_TYPE) {
            out.print("false");
        } else {
            out.print("0");
        }
    } else {
        out.print("null");
    }
}
 
Example 2
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 3
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void printDefaultValue(PrintWriter out, ClassNode type) {
    if (type.redirect() != ClassHelper.OBJECT_TYPE && type.redirect() != ClassHelper.boolean_TYPE) {
        out.print("(");
        printType(out, type);
        out.print(")");
    }

    if (ClassHelper.isPrimitiveType(type)) {
        if (type == ClassHelper.boolean_TYPE) {
            out.print("false");
        } else {
            out.print("0");
        }
    } else {
        out.print("null");
    }
}
 
Example 4
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 5
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 6
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static FieldNode checkFieldDoesNotExist(ClassNode node, String fieldName) {
    FieldNode ret = node.getDeclaredField(fieldName);
    if (ret != null) {
        if (isPublic(ret.getModifiers()) &&
                ret.getType().redirect() == ClassHelper.boolean_TYPE) {
            return ret;
        }
        throw new RuntimeParserException("The class " + node.getName() +
                " cannot declare field '" + fieldName + "' as this" +
                " field is needed for internal groovy purposes", ret);
    }
    return null;
}
 
Example 7
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void evaluateCompareExpression(final MethodCaller compareMethod, final BinaryExpression expression) {
    ClassNode classNode = controller.getClassNode();
    Expression leftExp = expression.getLeftExpression();
    Expression rightExp = expression.getRightExpression();
    ClassNode leftType = controller.getTypeChooser().resolveType(leftExp, classNode);
    ClassNode rightType = controller.getTypeChooser().resolveType(rightExp, classNode);

    boolean done = false;
    if (ClassHelper.isPrimitiveType(leftType) && ClassHelper.isPrimitiveType(rightType)) {
        BinaryExpressionMultiTypeDispatcher helper = new BinaryExpressionMultiTypeDispatcher(controller);
        done = helper.doPrimitiveCompare(leftType, rightType, expression);
    }
    if (!done) {
        AsmClassGenerator acg = controller.getAcg();
        OperandStack operandStack = controller.getOperandStack();

        leftExp.visit(acg);
        operandStack.box();
        rightExp.visit(acg);
        operandStack.box();

        compareMethod.call(controller.getMethodVisitor());
        ClassNode resType = ClassHelper.boolean_TYPE;
        if (compareMethod == findRegexMethod) {
            resType = ClassHelper.OBJECT_TYPE;
        }
        operandStack.replace(resType, 2);
    }
}
 
Example 8
Source File: OperandStack.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * ensure last marked parameter on the stack is a primitive boolean
 * if mark==stack size, we assume an empty expression or statement.
 * was used and we will use the value given in emptyDefault as boolean 
 * if mark==stack.size()-1 the top element will be cast to boolean using
 * Groovy truth.
 * In other cases we throw a GroovyBugError
 */
public void castToBool(int mark, boolean emptyDefault) {
    int size = stack.size();
    MethodVisitor mv = controller.getMethodVisitor();
    if (mark == size) {
        // no element, so use emptyDefault
        if (emptyDefault) {
            mv.visitIntInsn(BIPUSH, 1);
        } else {
            mv.visitIntInsn(BIPUSH, 0);
        }
        stack.add(null);
    } else if (mark == size - 1) {
        ClassNode last = stack.get(size - 1);
        // nothing to do in that case
        if (last == ClassHelper.boolean_TYPE) return;
        // not a primitive type, so call booleanUnbox
        if (!ClassHelper.isPrimitiveType(last)) {
            controller.getInvocationWriter().castNonPrimitiveToBool(last);
        } else {
            BytecodeHelper.convertPrimitiveToBoolean(mv, last);
        }
    } else {
        throw new GroovyBugError(
                "operand stack contains " + size +
                        " elements, but we expected only " + mark
        );
    }
    stack.set(mark, ClassHelper.boolean_TYPE);
}
 
Example 9
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 10
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 11
Source File: BinaryBooleanExpressionHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
protected ClassNode getArrayGetResultType() {
    return ClassHelper.boolean_TYPE;
}
 
Example 12
Source File: BinaryBooleanExpressionHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected ClassNode getNormalOpResultType() {
    return ClassHelper.boolean_TYPE;
}
 
Example 13
Source File: BinaryBooleanExpressionHelper.java    From groovy with Apache License 2.0 4 votes vote down vote up
protected ClassNode getDevisionOpResultType() {
    return ClassHelper.boolean_TYPE;
}
 
Example 14
Source File: JavaStubGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void printField(PrintWriter out, FieldNode fieldNode, boolean isInterface) {
    if ((fieldNode.getModifiers() & Opcodes.ACC_PRIVATE) != 0) return;
    printAnnotations(out, fieldNode);
    if (!isInterface) {
        printModifiers(out, fieldNode.getModifiers());
    }

    ClassNode type = fieldNode.getType();
    printType(out, type);

    out.print(" ");
    out.print(fieldNode.getName());
    if (isInterface || (fieldNode.getModifiers() & Opcodes.ACC_FINAL) != 0) {
        out.print(" = ");
        Expression valueExpr = fieldNode.getInitialValueExpression();
        if (valueExpr instanceof ConstantExpression) {
            valueExpr = Verifier.transformToPrimitiveConstantIfPossible((ConstantExpression) valueExpr);
        }
        if (valueExpr instanceof ConstantExpression
                && fieldNode.isStatic() && fieldNode.isFinal()
                && ClassHelper.isStaticConstantInitializerType(valueExpr.getType())
                && valueExpr.getType().equals(fieldNode.getType())) {
            // GROOVY-5150 : Initialize value with a dummy constant so that Java cross compiles correctly
            if (ClassHelper.STRING_TYPE.equals(valueExpr.getType())) {
                out.print(formatString(valueExpr.getText()));
            } else if (ClassHelper.char_TYPE.equals(valueExpr.getType())) {
                out.print("'"+valueExpr.getText()+"'");
            } else {
                ClassNode constantType = valueExpr.getType();
                out.print('(');
                printType(out, type);
                out.print(") ");
                out.print(valueExpr.getText());
                if (ClassHelper.Long_TYPE.equals(ClassHelper.getWrapper(constantType))) out.print('L');
            }
        } else if (ClassHelper.isPrimitiveType(type)) {
            String val = type == ClassHelper.boolean_TYPE ? "false" : "0";
            out.print("new " + ClassHelper.getWrapper(type) + "((" + type + ")" + val + ")");
        } else {
            out.print("null");
        }
    }
    out.println(";");
}