Search in sources :

Example 1 with Precondition

use of org.apache.groovy.contracts.annotations.meta.Precondition in project groovy by apache.

the class PreconditionGenerator method generateDefaultPreconditionStatement.

/**
 * Generates the default precondition statement for {@link org.codehaus.groovy.ast.MethodNode} instances with
 * the {@link org.apache.groovy.contracts.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.apache.groovy.contracts.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 = boolX(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.apache.groovy.contracts.annotations.meta.Precondition) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

Precondition (org.apache.groovy.contracts.annotations.meta.Precondition)1 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)1 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)1