use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovity by disney.
the class StatsASTTransformation method createImplicitReturn.
private Statement createImplicitReturn(Statement st) {
if (st instanceof ExpressionStatement) {
return new ReturnStatement(((ExpressionStatement) st).getExpression());
}
if (st instanceof BlockStatement) {
List<Statement> ls = ((BlockStatement) st).getStatements();
int pos = ls.size() - 1;
if (pos >= 0) {
Statement li;
if (((li = ls.get(pos)) instanceof ExpressionStatement)) {
// convert to return, groovy will miss this since we are wrapping in try/catch
ls.set(pos, new ReturnStatement(((ExpressionStatement) li).getExpression()));
}
}
}
return st;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project gcontracts by andresteingress.
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<BooleanExpression>();
for (Statement stmt : statementList) {
BooleanExpression tmp = null;
if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BooleanExpression) {
tmp = (BooleanExpression) ((ExpressionStatement) stmt).getExpression();
tmp.setNodeMetaData("statementLabel", stmt.getStatementLabel());
} else if (stmt instanceof ExpressionStatement) {
Expression expression = ((ExpressionStatement) stmt).getExpression();
tmp = new BooleanExpression(expression);
tmp.setSourcePosition(expression);
tmp.setNodeMetaData("statementLabel", stmt.getStatementLabel());
}
booleanExpressions.add(tmp);
}
return booleanExpressions;
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project gcontracts by andresteingress.
the class ClassInvariantGenerator method generateInvariantAssertionStatement.
/**
* Reads the {@link org.gcontracts.annotations.Invariant} boolean expression and generates a synthetic
* method holding this class invariant. This is used for heir calls to find out about inherited class
* invariants.
*
* @param type the current {@link org.codehaus.groovy.ast.ClassNode}
* @param classInvariant the {@link org.gcontracts.domain.ClassInvariant} the assertion statement should be generated from
*/
public void generateInvariantAssertionStatement(final ClassNode type, final org.gcontracts.domain.ClassInvariant classInvariant) {
BooleanExpression classInvariantExpression = addCallsToSuperAnnotationClosure(type, ClassInvariant.class, classInvariant.booleanExpression());
final BlockStatement blockStatement = new BlockStatement();
// add a local protected method with the invariant closure - this is needed for invariant checks in inheritance lines
MethodNode methodNode = type.addMethod(getInvariantMethodName(type), Opcodes.ACC_PROTECTED | Opcodes.ACC_SYNTHETIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, blockStatement);
methodNode.setSynthetic(true);
blockStatement.addStatements(wrapAssertionBooleanExpression(type, methodNode, classInvariantExpression, "invariant").getStatements());
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project grails-core by grails.
the class ASTValidationErrorsHelper method addInitErrorsMethod.
protected void addInitErrorsMethod(final ClassNode paramTypeClassNode) {
final ASTNode initErrorsMethod = paramTypeClassNode.getMethod(INIT_ERRORS_METHOD_NAME, GrailsArtefactClassInjector.ZERO_PARAMETERS);
if (initErrorsMethod == null) {
final BlockStatement initErrorsMethodCode = new BlockStatement();
final BinaryExpression errorsIsNullExpression = new BinaryExpression(ERRORS_EXPRESSION, Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), NULL_EXPRESSION);
Expression beanPropertyBindingResultConstructorArgs = new ArgumentListExpression(new VariableExpression("this"), new ConstantExpression(paramTypeClassNode.getName()));
final Statement newEvaluatorExpression = new ExpressionStatement(new BinaryExpression(ERRORS_EXPRESSION, EQUALS_SYMBOL, new ConstructorCallExpression(new ClassNode(ValidationErrors.class), beanPropertyBindingResultConstructorArgs)));
final Statement initErrorsIfNullStatement = new IfStatement(new BooleanExpression(errorsIsNullExpression), newEvaluatorExpression, new ExpressionStatement(new EmptyExpression()));
initErrorsMethodCode.addStatement(initErrorsIfNullStatement);
MethodNode methodNode = new MethodNode(INIT_ERRORS_METHOD_NAME, Modifier.PRIVATE, ClassHelper.VOID_TYPE, GrailsArtefactClassInjector.ZERO_PARAMETERS, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, initErrorsMethodCode);
paramTypeClassNode.addMethod(methodNode);
AnnotatedNodeUtils.markAsGenerated(paramTypeClassNode, methodNode);
}
}
use of org.codehaus.groovy.ast.stmt.BlockStatement in project grails-core by grails.
the class ASTValidationErrorsHelper method addGetErrorsMethod.
protected void addGetErrorsMethod(final ClassNode paramTypeClassNode) {
final ASTNode getErrorsMethod = paramTypeClassNode.getMethod(GET_ERRORS_METHOD_NAME, GrailsArtefactClassInjector.ZERO_PARAMETERS);
if (getErrorsMethod == null) {
final BlockStatement getErrorsMethodCode = new BlockStatement();
final Expression initErrorsMethodCallExpression = new MethodCallExpression(new VariableExpression("this"), INIT_ERRORS_METHOD_NAME, EMPTY_TUPLE);
getErrorsMethodCode.addStatement(new ExpressionStatement(initErrorsMethodCallExpression));
final Statement returnStatement = new ReturnStatement(ERRORS_EXPRESSION);
getErrorsMethodCode.addStatement(returnStatement);
MethodNode methodNode = new MethodNode(GET_ERRORS_METHOD_NAME, Modifier.PUBLIC, ERRORS_CLASS_NODE, GrailsArtefactClassInjector.ZERO_PARAMETERS, GrailsArtefactClassInjector.EMPTY_CLASS_ARRAY, getErrorsMethodCode);
paramTypeClassNode.addMethod(methodNode);
AnnotatedNodeUtils.markAsGenerated(paramTypeClassNode, methodNode);
}
}
Aggregations