Search in sources :

Example 1 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project grails-core by grails.

the class TagLibraryTransformer method addGetTagLibNamespaceMethod.

private void addGetTagLibNamespaceMethod(final ClassNode classNode, final String namespace) {
    final ConstantExpression namespaceConstantExpression = new ConstantExpression(namespace);
    Statement returnNamespaceStatement = new ReturnStatement(namespaceConstantExpression);
    final MethodNode m = new MethodNode(GET_TAG_LIB_NAMESPACE_METHOD_NAME, Modifier.PROTECTED, new ClassNode(String.class), Parameter.EMPTY_ARRAY, null, returnNamespaceStatement);
    classNode.addMethod(m);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 2 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project grails-core by grails.

the class TestMixinTransformation method getOrCreateMethodBody.

protected static BlockStatement getOrCreateMethodBody(ClassNode classNode, MethodNode methodNode, String name) {
    BlockStatement methodBody;
    if (!methodNode.getDeclaringClass().equals(classNode)) {
        methodBody = new BlockStatement();
        methodNode = new MethodNode(name, Modifier.PUBLIC, methodNode.getReturnType(), GrailsArtefactClassInjector.ZERO_PARAMETERS, null, methodBody);
        classNode.addMethod(methodNode);
    } else {
        final Statement setupMethodBody = methodNode.getCode();
        if (!(setupMethodBody instanceof BlockStatement)) {
            methodBody = new BlockStatement();
            if (setupMethodBody != null) {
                if (!(setupMethodBody instanceof ReturnStatement)) {
                    methodBody.addStatement(setupMethodBody);
                }
            }
            methodNode.setCode(methodBody);
        } else {
            methodBody = (BlockStatement) setupMethodBody;
        }
    }
    return methodBody;
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 3 with ReturnStatement

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

the class Java5 method setMethodDefaultValue.

private static void setMethodDefaultValue(MethodNode mn, Method m) {
    Object defaultValue = m.getDefaultValue();
    ConstantExpression cExp = ConstantExpression.NULL;
    if (defaultValue != null)
        cExp = new ConstantExpression(defaultValue);
    mn.setCode(new ReturnStatement(cExp));
    mn.setAnnotationDefault(true);
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 4 with ReturnStatement

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

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

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