Search in sources :

Example 11 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy by apache.

the class IfElseTest method testLoop.

public void testLoop() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
    BooleanExpression expression = new BooleanExpression(new BinaryExpression(new FieldExpression(new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc")));
    Statement trueStatement = new ExpressionStatement(new BinaryExpression(new FieldExpression(new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked")));
    Statement falseStatement = createPrintlnStatement(new ConstantExpression("false"));
    IfStatement statement = new IfStatement(expression, trueStatement, falseStatement);
    classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.newInstance();
    assertTrue("Managed to create bean", bean != null);
    assertSetProperty(bean, "bar", "abc");
    System.out.println("################ Now about to invoke method");
    Object[] array = {};
    InvokerHelper.invokeMethod(bean, "ifDemo", array);
    System.out.println("################ Done");
    assertGetProperty(bean, "result", "worked");
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 12 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy by apache.

the class BinaryExpressionTransformer method convertInOperatorToTernary.

private Expression convertInOperatorToTernary(final BinaryExpression bin, final Expression rightExpression, final Expression leftExpression) {
    MethodCallExpression call = new MethodCallExpression(rightExpression, "isCase", leftExpression);
    call.setMethodTarget((MethodNode) bin.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET));
    call.setSourcePosition(bin);
    call.copyNodeMetaData(bin);
    TernaryExpression tExp = new TernaryExpression(new BooleanExpression(new BinaryExpression(rightExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null))), new BinaryExpression(leftExpression, Token.newSymbol("==", -1, -1), new ConstantExpression(null)), call);
    return staticCompilationTransformer.transform(tExp);
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 13 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project gcontracts by andresteingress.

the class Assertion method or.

public void or(T other) {
    Validate.notNull(other);
    BooleanExpression newBooleanExpression = new BooleanExpression(new BinaryExpression(booleanExpression(), Token.newSymbol(Types.LOGICAL_OR, -1, -1), other.booleanExpression()));
    newBooleanExpression.setSourcePosition(booleanExpression());
    renew(newBooleanExpression);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression)

Example 14 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project gcontracts by andresteingress.

the class AssertStatementCreationUtility method getAssertionStatemens.

/**
     * Reusable method for creating assert statements for the given <tt>booleanExpression</tt>.
     *
     * @param booleanExpressions the assertion's {@link org.codehaus.groovy.ast.expr.BooleanExpression} instances
     *
     * @return a newly created {@link org.codehaus.groovy.ast.stmt.AssertStatement}
     */
public static BlockStatement getAssertionStatemens(final List<BooleanExpression> booleanExpressions) {
    List<AssertStatement> assertStatements = new ArrayList<AssertStatement>();
    for (BooleanExpression booleanExpression : booleanExpressions) {
        assertStatements.add(getAssertionStatement(booleanExpression));
    }
    final BlockStatement blockStatement = new BlockStatement();
    blockStatement.getStatements().addAll(assertStatements);
    return blockStatement;
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ArrayList(java.util.ArrayList)

Example 15 with BooleanExpression

use of org.codehaus.groovy.ast.expr.BooleanExpression in project groovy-core by groovy.

the class AssertionWriter method writeSourcelessAssertText.

private void writeSourcelessAssertText(AssertStatement statement) {
    MethodVisitor mv = controller.getMethodVisitor();
    OperandStack operandStack = controller.getOperandStack();
    BooleanExpression booleanExpression = statement.getBooleanExpression();
    // push expression string onto stack
    String expressionText = booleanExpression.getText();
    List<String> list = new ArrayList<String>();
    addVariableNames(booleanExpression, list);
    if (list.isEmpty()) {
        mv.visitLdcInsn(expressionText);
    } else {
        boolean first = true;
        // let's create a new expression
        mv.visitTypeInsn(NEW, "java/lang/StringBuffer");
        mv.visitInsn(DUP);
        mv.visitLdcInsn(expressionText + ". Values: ");
        mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuffer", "<init>", "(Ljava/lang/String;)V", false);
        //TODO: maybe use more special type StringBuffer here
        operandStack.push(ClassHelper.OBJECT_TYPE);
        int tempIndex = controller.getCompileStack().defineTemporaryVariable("assert", true);
        for (String name : list) {
            String text = name + " = ";
            if (first) {
                first = false;
            } else {
                text = ", " + text;
            }
            mv.visitVarInsn(ALOAD, tempIndex);
            mv.visitLdcInsn(text);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/Object;)Ljava/lang/StringBuffer;", false);
            mv.visitInsn(POP);
            mv.visitVarInsn(ALOAD, tempIndex);
            new VariableExpression(name).visit(controller.getAcg());
            operandStack.box();
            mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/InvokerHelper", "toString", "(Ljava/lang/Object;)Ljava/lang/String;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuffer", "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;", false);
            mv.visitInsn(POP);
            operandStack.remove(1);
        }
        mv.visitVarInsn(ALOAD, tempIndex);
        controller.getCompileStack().removeVar(tempIndex);
    }
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)27 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)21 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)18 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)17 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)14 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)13 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)13 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)12 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)11 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)10 Expression (org.codehaus.groovy.ast.expr.Expression)10 Statement (org.codehaus.groovy.ast.stmt.Statement)10 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)9 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)9 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)9 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)8 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)8 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)8 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)8 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)7