Search in sources :

Example 36 with BooleanExpression

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

the class PostconditionGenerator method generateDefaultPostconditionStatement.

/**
 * Adds a default postcondition if a postcondition has already been defined for this {@link org.codehaus.groovy.ast.MethodNode}
 * in a super-class.
 *
 * @param type   the current {@link org.codehaus.groovy.ast.ClassNode} of the given <tt>methodNode</tt>
 * @param method the {@link org.codehaus.groovy.ast.MethodNode} to create the default postcondition for
 */
public void generateDefaultPostconditionStatement(final ClassNode type, final MethodNode method) {
    // if another precondition is available we'll evaluate to false
    boolean isAnotherPostconditionAvailable = AnnotationUtils.getAnnotationNodeInHierarchyWithMetaAnnotation(type.getSuperClass(), method, ClassHelper.makeWithoutCaching(Postcondition.class)).size() > 0;
    if (!isAnotherPostconditionAvailable)
        return;
    // if another post-condition is available we need to add a default expression of TRUE
    // since post-conditions are usually connected with a logical AND
    final BooleanExpression postconditionBooleanExpression = addCallsToSuperMethodNodeAnnotationClosure(method.getDeclaringClass(), method, Postcondition.class, new BooleanExpression(ConstantExpression.TRUE), true);
    if (postconditionBooleanExpression.getExpression() == ConstantExpression.TRUE)
        return;
    final BlockStatement blockStatement = wrapAssertionBooleanExpression(type, method, postconditionBooleanExpression, "postcondition");
    addPostcondition(method, blockStatement);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Postcondition(org.apache.groovy.contracts.annotations.meta.Postcondition)

Example 37 with BooleanExpression

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

the class PostconditionGenerator method generatePostconditionAssertionStatement.

/**
 * Injects a postcondition assertion statement in the given <tt>method</tt>, based on the <tt>booleanExpression</tt>.
 *
 * @param method        the {@link org.codehaus.groovy.ast.MethodNode} for assertion injection
 * @param postcondition the {@link org.apache.groovy.contracts.domain.Postcondition} the assertion statement should be generated from
 */
public void generatePostconditionAssertionStatement(MethodNode method, org.apache.groovy.contracts.domain.Postcondition postcondition) {
    final BooleanExpression postconditionBooleanExpression = addCallsToSuperMethodNodeAnnotationClosure(method.getDeclaringClass(), method, Postcondition.class, postcondition.booleanExpression(), true);
    BlockStatement blockStatement;
    final BlockStatement originalBlockStatement = postcondition.originalBlockStatement();
    // if use execution tracker flag is found in the meta-data the annotation closure visitor discovered
    // method calls which might be subject to cycling boolean expressions -> no inline mode possible
    final boolean useExecutionTracker = originalBlockStatement == null || Boolean.TRUE.equals(originalBlockStatement.getNodeMetaData(AnnotationClosureVisitor.META_DATA_USE_EXECUTION_TRACKER));
    if (!useExecutionTracker && Boolean.TRUE.equals(method.getNodeMetaData(META_DATA_USE_INLINE_MODE))) {
        blockStatement = getInlineModeBlockStatement(originalBlockStatement);
    } else {
        blockStatement = wrapAssertionBooleanExpression(method.getDeclaringClass(), method, postconditionBooleanExpression, "postcondition");
    }
    addPostcondition(method, blockStatement);
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 38 with BooleanExpression

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

the class ExpressionUtils method getBooleanExpressionsFromAssertionStatements.

/**
 * Returns all {@link BooleanExpression} instances found in the given {@link BlockStatement}.
 */
public static List<BooleanExpression> getBooleanExpressionsFromAssertionStatements(BlockStatement blockStatement) {
    AssertStatementCollector collector = new AssertStatementCollector();
    collector.visitBlockStatement(blockStatement);
    List<AssertStatement> assertStatements = collector.assertStatements;
    if (assertStatements.isEmpty())
        return Collections.emptyList();
    List<BooleanExpression> booleanExpressions = new ArrayList<>();
    for (AssertStatement assertStatement : assertStatements) {
        booleanExpressions.add(assertStatement.getBooleanExpression());
    }
    return booleanExpressions;
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) ArrayList(java.util.ArrayList)

Example 39 with BooleanExpression

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

the class ExpressionUtils method getBooleanExpressions.

/**
 * Returns all {@link BooleanExpression} instances found in the given {@link BlockStatement}.
 */
private static List<BooleanExpression> getBooleanExpressions(BlockStatement closureBlockStatement) {
    final List<Statement> statementList = closureBlockStatement.getStatements();
    List<BooleanExpression> booleanExpressions = new ArrayList<>();
    for (Statement stmt : statementList) {
        BooleanExpression tmp = null;
        if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BooleanExpression) {
            tmp = (BooleanExpression) ((ExpressionStatement) stmt).getExpression();
            saveLabel(tmp, stmt);
        } else if (stmt instanceof ExpressionStatement) {
            Expression expression = ((ExpressionStatement) stmt).getExpression();
            tmp = boolX(expression);
            tmp.setSourcePosition(expression);
            saveLabel(tmp, stmt);
        }
        booleanExpressions.add(tmp);
    }
    return booleanExpressions;
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ArrayList(java.util.ArrayList)

Example 40 with BooleanExpression

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

the class AssertStatementCreationUtility method getAssertionStatements.

/**
 * 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 getAssertionStatements(final List<BooleanExpression> booleanExpressions) {
    List<Statement> assertStatements = new ArrayList<>();
    for (BooleanExpression booleanExpression : booleanExpressions) {
        assertStatements.add(getAssertionStatement(booleanExpression));
    }
    final BlockStatement blockStatement = block();
    blockStatement.getStatements().addAll(assertStatements);
    return blockStatement;
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

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