title: Cannot get Groovy AST example to work
body: <p>I am trying to learn how to use (and extend) groovy, and I am following the example from <a href="http://groovy.codehaus.org/Local+AST+Transformations" rel="nofollow noreferrer">this page</a>. Basically, it shows how to define an annotation for groovy code that lets you hook in to the compiler process. The example revolves around writing and annotation that will cause lines to be printed before and after method calls. </p>
  
  <p>My code goes as follows; first the necessary imports:</p>
  
  <pre><code>package foo
  
  import org.codehaus.groovy.transform.*
  import java.lang.annotation.*
  import org.codehaus.groovy.ast.*
  import org.codehaus.groovy.control.*
  import org.codehaus.groovy.ast.stmt.*
  import org.codehaus.groovy.ast.expr.*
  </code></pre>
  
  <p>Then, we define the annotation that is to be used:</p>
  
  <pre><code>@Retention(RetentionPolicy.SOURCE)
  @Target([ElementType.METHOD])
  @GroovyASTTransformationClass(["foo.LoggingASTTransformation"])
  public @interface WithLogging {
  }
  </code></pre>
  
  <p>Then the transformation itself:</p>
  
  <pre><code>@GroovyASTTransformation(phase=CompilePhase.SEMANTIC_ANALYSIS)
  public class LoggingASTTransformation implements ASTTransformation {
  
    public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
        println("visiting astnodes")
        List methods = sourceUnit.getAST()?.getMethods()
        // find all methods annotated with @WithLogging
        methods.findAll { MethodNode method -&gt;
            method.getAnnotations(new ClassNode(WithLogging))
        }.each { MethodNode method -&gt;
            Statement startMessage = createPrintlnAst("Starting $method.name")
            Statement endMessage = createPrintlnAst("Ending $method.name")
  
            List existingStatements = method.getCode().getStatements()
            existingStatements.add(0, startMessage)
            existingStatements.add(endMessage)
        }
    }
  
    private Statement createPrintlnAst(String message) {
        return new ExpressionStatement(
            new MethodCallExpression(
                new VariableExpression("this"),
                new ConstantExpression("println"),
                new ArgumentListExpression(
                    new ConstantExpression(message)
                )
            )
        )
    }
  }
  </code></pre>
  
  <p>Finally, my code that is supposed to use this transformation:</p>
  
  <pre><code>public class Foo {
     @WithLogging
     def f() { println "hello from f" }
  }
  
  f = new Foo()
  f.f()
  </code></pre>
  
  <p>Now, this is supposed to print <em>Starting f\n hello from f \n Ending f</em>, but all it prints is <em>hello from f</em> (and therein lies my problem). As you can see from the code, I have also put a <em>visiting astnodes</em> message in the transformation itself, to hopefully see if it ever gets there, but alas it doesn't (or so it seems).</p>
  
  <p><code>groovy -version</code> prints <code>Groovy Version: 1.6.0 JVM: 1.6.0_11</code></p>
  
  <p>Could anyone try this code and see if it works on their system, or give me some pointers as to what could be wrong? </p>