Search in sources :

Example 61 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project groovy by apache.

the class AutoNewLineTransformer method createNewLine.

private Statement createNewLine(final ASTNode node) {
    MethodCallExpression mce = new MethodCallExpression(new VariableExpression("this"), "newLine", ArgumentListExpression.EMPTY_ARGUMENTS);
    mce.setImplicitThis(true);
    mce.setSourcePosition(node);
    ExpressionStatement stmt = new ExpressionStatement(mce);
    stmt.setSourcePosition(node);
    return stmt;
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 62 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project gcontracts by andresteingress.

the class DynamicSetterInjectionVisitor method visitClass.

@Override
public void visitClass(ClassNode classNode) {
    // if a class invariant is available visit all property nodes else skip this class
    final MethodNode invariantMethodNode = BaseGenerator.getInvariantMethodNode(classNode);
    if (invariantMethodNode == null || AnnotationUtils.hasAnnotationOfType(classNode, SPRING_STEREOTYPE_PACKAGE))
        return;
    invariantAssertionBlockStatement = new BlockStatement();
    invariantAssertionBlockStatement.addStatement(new ExpressionStatement(new MethodCallExpression(VariableExpression.THIS_EXPRESSION, invariantMethodNode.getName(), ArgumentListExpression.EMPTY_ARGUMENTS)));
    List<ConstructorNode> declaredConstructors = classNode.getDeclaredConstructors();
    if (declaredConstructors == null || declaredConstructors.isEmpty()) {
        // create default constructor with class invariant check
        ConstructorNode constructor = new ConstructorNode(Opcodes.ACC_PUBLIC, invariantAssertionBlockStatement);
        constructor.setSynthetic(true);
        classNode.addConstructor(constructor);
    }
    super.visitClass(classNode);
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 63 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project gcontracts by andresteingress.

the class PreconditionGenerator method addPrecondition.

private void addPrecondition(MethodNode method, BlockStatement blockStatement) {
    final BlockStatement modifiedMethodCode = new BlockStatement();
    modifiedMethodCode.addStatements(blockStatement.getStatements());
    if (method.getCode() instanceof BlockStatement) {
        BlockStatement methodBlock = (BlockStatement) method.getCode();
        for (Statement statement : methodBlock.getStatements()) {
            if (method instanceof ConstructorNode && statement instanceof ExpressionStatement && ((ExpressionStatement) statement).getExpression() instanceof ConstructorCallExpression) {
                modifiedMethodCode.getStatements().add(0, statement);
            } else {
                modifiedMethodCode.getStatements().add(statement);
            }
        }
    } else {
        modifiedMethodCode.addStatement(method.getCode());
    }
    method.setCode(modifiedMethodCode);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 64 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement 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 65 with ExpressionStatement

use of org.codehaus.groovy.ast.stmt.ExpressionStatement in project spock by spockframework.

the class SpecialMethodCall method isMatch.

public boolean isMatch(Statement stat) {
    ExpressionStatement exprStat = ObjectUtil.asInstance(stat, ExpressionStatement.class);
    if (exprStat == null)
        return false;
    Expression expr = exprStat.getExpression();
    return expr == binaryExpr || expr == methodCallExpr;
}
Also used : ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Aggregations

ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)122 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)78 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 Statement (org.codehaus.groovy.ast.stmt.Statement)58 Expression (org.codehaus.groovy.ast.expr.Expression)54 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)52 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)51 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)49 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)44 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)39 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)35 Parameter (org.codehaus.groovy.ast.Parameter)30 MethodNode (org.codehaus.groovy.ast.MethodNode)29 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)29 ClassNode (org.codehaus.groovy.ast.ClassNode)27 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)26 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)26 ArrayList (java.util.ArrayList)25 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)24