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);
}
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);
}
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;
}
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;
}
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;
}
Aggregations