Search in sources :

Example 36 with MethodNode

use of org.codehaus.groovy.ast.MethodNode 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 37 with MethodNode

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

the class TestMixinTransformation method autoAddTestAnnotation.

private void autoAddTestAnnotation(ClassNode classNode) {
    if (isSpockTest(classNode))
        return;
    Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
    for (String methodName : declaredMethodsMap.keySet()) {
        MethodNode methodNode = declaredMethodsMap.get(methodName);
        ClassNode testAnnotationClassNode = TEST_ANNOTATION.getClassNode();
        List<AnnotationNode> existingTestAnnotations = methodNode.getAnnotations(testAnnotationClassNode);
        if (isCandidateMethod(methodNode) && (methodNode.getName().startsWith("test") || existingTestAnnotations.size() > 0)) {
            if (existingTestAnnotations.size() == 0) {
                ClassNode returnType = methodNode.getReturnType();
                if (returnType.getName().equals(VOID_TYPE)) {
                    methodNode.addAnnotation(TEST_ANNOTATION);
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode)

Example 38 with MethodNode

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

the class GroovyPageBytecodeOptimizer method performInjection.

public void performInjection(SourceUnit source, GeneratorContext context, ClassNode classNode) {
    // search run method in GSP script and get codeblock
    MethodNode runMethod = classNode.getMethod(RUN_METHOD, new Parameter[0]);
    if (runMethod != null && runMethod.getCode() instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) runMethod.getCode();
        //scan all MethodExpressionCalls to optimize them
        GroovyPageOptimizerVisitor groovyPageVisitor = new GroovyPageOptimizerVisitor(classNode);
        groovyPageVisitor.visitBlockStatement(block);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 39 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

the class DefaultStrategy method createBuilderMethodForProp.

private MethodNode createBuilderMethodForProp(ClassNode builder, PropertyInfo pinfo, String prefix) {
    ClassNode fieldType = pinfo.getType();
    String fieldName = pinfo.getName();
    String setterName = getSetterName(prefix, fieldName);
    return new MethodNode(setterName, ACC_PUBLIC, newClass(builder), params(param(fieldType, fieldName)), NO_EXCEPTIONS, block(stmt(assignX(propX(varX("this"), constX(fieldName)), varX(fieldName, fieldType))), returnS(varX("this", builder))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode)

Example 40 with MethodNode

use of org.codehaus.groovy.ast.MethodNode in project groovy-core by groovy.

the class ExternalStrategy method createBuildMethod.

private static MethodNode createBuildMethod(BuilderASTTransformation transform, AnnotationNode anno, ClassNode sourceClass, List<PropertyInfo> fields) {
    String buildMethodName = transform.getMemberStringValue(anno, "buildMethodName", "build");
    final BlockStatement body = new BlockStatement();
    Expression sourceClassInstance = initializeInstance(sourceClass, fields, body);
    body.addStatement(returnS(sourceClassInstance));
    return new MethodNode(buildMethodName, ACC_PUBLIC, sourceClass, NO_PARAMS, NO_EXCEPTIONS, body);
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

MethodNode (org.codehaus.groovy.ast.MethodNode)294 ClassNode (org.codehaus.groovy.ast.ClassNode)193 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)94 Parameter (org.codehaus.groovy.ast.Parameter)79 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)65 FieldNode (org.codehaus.groovy.ast.FieldNode)57 LinkedList (java.util.LinkedList)50 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)50 Expression (org.codehaus.groovy.ast.expr.Expression)49 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)47 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)43 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)39 ArrayList (java.util.ArrayList)36 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)36 Statement (org.codehaus.groovy.ast.stmt.Statement)34 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)33 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)29 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)27 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)27