Search in sources :

Example 26 with ReturnStatement

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

the class MethodTest method testMethods.

public void testMethods() throws Exception {
    ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE);
    classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null));
    Statement statementA = new ReturnStatement(new ConstantExpression("calledA"));
    Statement statementB = new ReturnStatement(new ConstantExpression("calledB"));
    Statement emptyStatement = new BlockStatement();
    classNode.addMethod(new MethodNode("a", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementA));
    classNode.addMethod(new MethodNode("b", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementB));
    classNode.addMethod(new MethodNode("noReturnMethodA", ACC_PUBLIC, null, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
    classNode.addMethod(new MethodNode("noReturnMethodB", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
    classNode.addMethod(new MethodNode("c", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, emptyStatement));
    Class fooClass = loadClass(classNode);
    assertTrue("Loaded a new class", fooClass != null);
    Object bean = fooClass.getDeclaredConstructor().newInstance();
    assertTrue("Created instance of class: " + bean, bean != null);
    assertCallMethod(bean, "a", "calledA");
    assertCallMethod(bean, "b", "calledB");
    assertCallMethod(bean, "noReturnMethodA", null);
    assertCallMethod(bean, "noReturnMethodB", null);
    assertCallMethod(bean, "c", null);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 27 with ReturnStatement

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

the class PostconditionGenerator method addPostcondition.

private void addPostcondition(MethodNode method, BlockStatement postconditionBlockStatement) {
    final BlockStatement block = (BlockStatement) method.getCode();
    // if return type is not void, than a "result" variable is provided in the postcondition expression
    final List<Statement> statements = block.getStatements();
    if (statements.size() > 0) {
        Expression contractsEnabled = localVarX(BaseVisitor.GCONTRACTS_ENABLED_VAR, ClassHelper.boolean_TYPE);
        if (!isPrimitiveVoid(method.getReturnType())) {
            List<ReturnStatement> returnStatements = AssertStatementCreationUtility.getReturnStatements(method);
            for (ReturnStatement returnStatement : returnStatements) {
                BlockStatement localPostconditionBlockStatement = block(new VariableScope(), postconditionBlockStatement.getStatements());
                Expression result = localVarX("result", method.getReturnType());
                localPostconditionBlockStatement.getStatements().add(0, declS(result, returnStatement.getExpression()));
                AssertStatementCreationUtility.injectResultVariableReturnStatementAndAssertionCallStatement(block, method.getReturnType().redirect(), returnStatement, localPostconditionBlockStatement);
            }
            setOldVariablesIfEnabled(block, contractsEnabled);
        } else if (method instanceof ConstructorNode) {
            block.addStatements(postconditionBlockStatement.getStatements());
        } else {
            setOldVariablesIfEnabled(block, contractsEnabled);
            block.addStatements(postconditionBlockStatement.getStatements());
        }
    }
}
Also used : BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 28 with ReturnStatement

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

the class ClassInvariantGenerator method addInvariantAssertionStatement.

/**
 * Adds the current class-invariant to the given <tt>method</tt>.
 *
 * @param type   the {@link org.codehaus.groovy.ast.ClassNode} which declared the given {@link org.codehaus.groovy.ast.MethodNode}
 * @param method the current {@link org.codehaus.groovy.ast.MethodNode}
 */
public void addInvariantAssertionStatement(final ClassNode type, final MethodNode method) {
    final String invariantMethodName = getInvariantMethodName(type);
    final MethodNode invariantMethod = type.getDeclaredMethod(invariantMethodName, Parameter.EMPTY_ARRAY);
    if (invariantMethod == null)
        return;
    Statement invariantMethodCall = stmt(callThisX(invariantMethod.getName()));
    final Statement statement = method.getCode();
    if (statement instanceof BlockStatement && !isPrimitiveVoid(method.getReturnType()) && !(method instanceof ConstructorNode)) {
        final BlockStatement blockStatement = (BlockStatement) statement;
        final List<ReturnStatement> returnStatements = AssertStatementCreationUtility.getReturnStatements(method);
        for (ReturnStatement returnStatement : returnStatements) {
            AssertStatementCreationUtility.addAssertionCallStatementToReturnStatement(blockStatement, returnStatement, invariantMethodCall);
        }
        if (returnStatements.isEmpty())
            blockStatement.addStatement(invariantMethodCall);
    } else if (statement instanceof BlockStatement) {
        final BlockStatement blockStatement = (BlockStatement) statement;
        blockStatement.addStatement(invariantMethodCall);
    } else {
        final BlockStatement assertionBlock = new BlockStatement();
        assertionBlock.addStatement(statement);
        assertionBlock.addStatement(invariantMethodCall);
        method.setCode(assertionBlock);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 29 with ReturnStatement

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

the class TailRecursiveASTTransformation method replaceRecursiveReturnsInsideClosures.

@SuppressWarnings("Instanceof")
private void replaceRecursiveReturnsInsideClosures(final MethodNode method, final Map<Integer, Map<String, Object>> positionMapping) {
    Closure<Boolean> whenRecursiveReturn = new Closure<Boolean>(this, this) {

        public Boolean doCall(Statement statement, boolean inClosure) {
            if (!inClosure)
                return false;
            if (!(statement instanceof ReturnStatement)) {
                return false;
            }
            Expression inner = ((ReturnStatement) statement).getExpression();
            if (!(inner instanceof MethodCallExpression) && !(inner instanceof StaticMethodCallExpression)) {
                return false;
            }
            return isRecursiveIn(inner, method);
        }
    };
    Closure<Statement> replaceWithThrowLoopException = new Closure<Statement>(this, this) {

        public Statement doCall(ReturnStatement statement) {
            return new ReturnStatementToIterationConverter(AstHelper.recurByThrowStatement()).convert(statement, positionMapping);
        }
    };
    StatementReplacer replacer = new StatementReplacer(whenRecursiveReturn, replaceWithThrowLoopException);
    replacer.replaceIn(method.getCode());
}
Also used : Closure(groovy.lang.Closure) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression)

Example 30 with ReturnStatement

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

the class TailRecursiveASTTransformation method replaceRecursiveReturnsOutsideClosures.

@SuppressWarnings("Instanceof")
private void replaceRecursiveReturnsOutsideClosures(final MethodNode method, final Map<Integer, Map<String, Object>> positionMapping) {
    Closure<Boolean> whenRecursiveReturn = new Closure<Boolean>(this, this) {

        public Boolean doCall(Statement statement, boolean inClosure) {
            if (inClosure)
                return false;
            if (!(statement instanceof ReturnStatement)) {
                return false;
            }
            Expression inner = ((ReturnStatement) statement).getExpression();
            if (!(inner instanceof MethodCallExpression) && !(inner instanceof StaticMethodCallExpression)) {
                return false;
            }
            return isRecursiveIn(inner, method);
        }
    };
    Closure<Statement> replaceWithContinueBlock = new Closure<Statement>(this, this) {

        public Statement doCall(ReturnStatement statement) {
            return new ReturnStatementToIterationConverter().convert(statement, positionMapping);
        }
    };
    StatementReplacer replacer = new StatementReplacer(whenRecursiveReturn, replaceWithContinueBlock);
    replacer.replaceIn(method.getCode());
}
Also used : Closure(groovy.lang.Closure) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression)

Aggregations

ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)74 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)44 Statement (org.codehaus.groovy.ast.stmt.Statement)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)37 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)32 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 MethodNode (org.codehaus.groovy.ast.MethodNode)28 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)26 Expression (org.codehaus.groovy.ast.expr.Expression)25 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)24 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)23 ClassNode (org.codehaus.groovy.ast.ClassNode)22 Parameter (org.codehaus.groovy.ast.Parameter)22 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)21 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)18 ArrayList (java.util.ArrayList)17 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)17 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)15