Search in sources :

Example 56 with BlockStatement

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;
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 57 with BlockStatement

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;
}
Also used : 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 58 with BlockStatement

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());
}
Also used : BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 59 with BlockStatement

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);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ValidationErrors(grails.validation.ValidationErrors) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ASTNode(org.codehaus.groovy.ast.ASTNode)

Example 60 with BlockStatement

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);
    }
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ASTNode(org.codehaus.groovy.ast.ASTNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)212 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)94 Statement (org.codehaus.groovy.ast.stmt.Statement)87 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)76 Expression (org.codehaus.groovy.ast.expr.Expression)75 MethodNode (org.codehaus.groovy.ast.MethodNode)64 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)62 Parameter (org.codehaus.groovy.ast.Parameter)58 ClassNode (org.codehaus.groovy.ast.ClassNode)54 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)52 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)51 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)45 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)45 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)44 FieldNode (org.codehaus.groovy.ast.FieldNode)38 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)37 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)33 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)33 ArrayList (java.util.ArrayList)32 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)28