Search in sources :

Example 46 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)

Example 47 with BooleanExpression

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

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 48 with BooleanExpression

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

the class ControllerActionTransformer method initializeStringParameter.

protected void initializeStringParameter(final ClassNode classNode, final BlockStatement wrapper, final Parameter param, final String requestParameterName) {
    final ClassNode paramTypeClassNode = param.getType();
    final String methodParamName = param.getName();
    Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
    final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(new ConstantExpression(requestParameterName));
    final BooleanExpression containsKeyExpression = new BooleanExpression(applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
    final Statement initializeParameterStatement = new ExpressionStatement(new DeclarationExpression(new VariableExpression(methodParamName, paramTypeClassNode), Token.newSymbol(Types.EQUALS, 0, 0), new TernaryExpression(containsKeyExpression, buildGetMapExpression(getParamsExpression, requestParameterName), new ConstantExpression(null))));
    wrapper.addStatement(initializeParameterStatement);
}
Also used : TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GrailsASTUtils.buildGetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetPropertyExpression) GrailsASTUtils.buildSetPropertyExpression(org.grails.compiler.injection.GrailsASTUtils.buildSetPropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) GrailsASTUtils.buildGetMapExpression(org.grails.compiler.injection.GrailsASTUtils.buildGetMapExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Map(java.util.Map) TypeConvertingMap(grails.util.TypeConvertingMap) HashMap(java.util.HashMap)

Aggregations

BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)48 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)30 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)24 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)24 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)23 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)23 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)18 Expression (org.codehaus.groovy.ast.expr.Expression)18 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)16 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)16 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)15 Statement (org.codehaus.groovy.ast.stmt.Statement)14 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)13 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)12 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)12 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)12 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)12 TernaryExpression (org.codehaus.groovy.ast.expr.TernaryExpression)12 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)12