Search in sources :

Example 1 with Precondition

use of org.gcontracts.annotations.meta.Precondition 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)

Aggregations

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