Search in sources :

Example 21 with BooleanExpression

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

the class PreconditionGenerator method generateDefaultPreconditionStatement.

/**
     * Generates the default precondition statement for {@link org.codehaus.groovy.ast.MethodNode} instances with
     * the {@link org.gcontracts.annotations.meta.Precondition} annotation.
     *
     * @param type the current {@link org.codehaus.groovy.ast.ClassNode}
     * @param methodNode the {@link org.codehaus.groovy.ast.MethodNode} with a {@link org.gcontracts.annotations.meta.Precondition} annotation
     */
public void generateDefaultPreconditionStatement(final ClassNode type, final MethodNode methodNode) {
    // if another precondition is available we'll evaluate to false
    boolean isAnotherPreconditionAvailable = AnnotationUtils.getAnnotationNodeInHierarchyWithMetaAnnotation(type.getSuperClass(), methodNode, ClassHelper.makeWithoutCaching(Precondition.class)).size() > 0;
    if (!isAnotherPreconditionAvailable)
        return;
    // if there is another preconditio up the inheritance path, we need a default precondition with FALSE
    // e.g. C1 <no precondition> : C2 <item != null> == false || item != null
    BooleanExpression preconditionBooleanExpression = new BooleanExpression(ConstantExpression.FALSE);
    preconditionBooleanExpression = addCallsToSuperMethodNodeAnnotationClosure(type, methodNode, Precondition.class, preconditionBooleanExpression, false);
    // if precondition could not be found in parent class, let's return
    if (preconditionBooleanExpression.getExpression() == ConstantExpression.FALSE)
        return;
    final BlockStatement blockStatement = wrapAssertionBooleanExpression(type, methodNode, preconditionBooleanExpression, "precondition");
    addPrecondition(methodNode, blockStatement);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) Precondition(org.gcontracts.annotations.meta.Precondition) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 22 with BooleanExpression

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

the class Assertion method and.

public void and(T other) {
    Validate.notNull(other);
    BooleanExpression newBooleanExpression = new BooleanExpression(new BinaryExpression(booleanExpression(), Token.newSymbol(Types.LOGICAL_AND, -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 23 with BooleanExpression

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

the class AssertionWriter method addVariableNames.

private void addVariableNames(Expression expression, List<String> list) {
    if (expression instanceof BooleanExpression) {
        BooleanExpression boolExp = (BooleanExpression) expression;
        addVariableNames(boolExp.getExpression(), list);
    } else if (expression instanceof BinaryExpression) {
        BinaryExpression binExp = (BinaryExpression) expression;
        addVariableNames(binExp.getLeftExpression(), list);
        addVariableNames(binExp.getRightExpression(), list);
    } else if (expression instanceof VariableExpression) {
        VariableExpression varExp = (VariableExpression) expression;
        list.add(varExp.getName());
    }
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 24 with BooleanExpression

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

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

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

the class TraitComposer method createSuperFallback.

private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) {
    ArgumentListExpression args = new ArgumentListExpression();
    Parameter[] forwarderMethodParameters = forwarderMethod.getParameters();
    for (final Parameter forwarderMethodParameter : forwarderMethodParameters) {
        args.addExpression(new VariableExpression(forwarderMethodParameter));
    }
    BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE));
    MethodCallExpression superCall = new MethodCallExpression(new VariableExpression("super"), forwarderMethod.getName(), args);
    superCall.setImplicitThis(false);
    CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this"));
    MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS);
    getProxy.setImplicitThis(true);
    StaticMethodCallExpression proxyCall = new StaticMethodCallExpression(ClassHelper.make(InvokerHelper.class), "invokeMethod", new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions())));
    IfStatement stmt = new IfStatement(new BooleanExpression(instanceOfExpr), new ExpressionStatement(new CastExpression(returnType, proxyCall)), new ExpressionStatement(superCall));
    return stmt;
}
Also used : InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

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