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;
}
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);
}
}
}
}
}
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);
}
}
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))));
}
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);
}
Aggregations