« Return to Thread: Groovy AST not taking?

Re: Groovy AST not taking?

by Robert Fischer :: Rate this Message:

Reply to Author | View in Thread

And with regard to build -- is there a particular stunt I need to pull to ensure a local transform
is used?  I thought it was sufficient to just have it in the class path.

~~ Robert.

Peter Niederwieser wrote:

> Hi Robert,
>
> your transform works fine for me, so I guess there is a problem with the way
> you build/run your code.
>
> To investigate your transform, I used the following class:
>
> class WithLogTest {
>     static void main(args) {
>         def clazz = new GroovyClassLoader().parseClass("""  
> class WLT {
>     static run() {
>         println HasLog.log
>     }
> }
>
> @foo.WithLog
> class HasLog {}
>         """)
>
>         clazz.run()
>     }
> }
>
> Now set a break point in the transform and run the class.
>
> Your transform can be simplified to:
>
> @GroovyASTTransformation(phase=CompilePhase.SEMANTIC_ANALYSIS)
> public class WithLogImpl implements ASTTransformation {
>   public void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
>       def classNode = nodes[1] // nodes[0] is the annotation that triggered
> the transform
>       classNode.addField("log",
>           ClassNode.ACC_PUBLIC | ClassNode.ACC_STATIC | ClassNode.ACC_FINAL,
> new ClassNode(Logger),
>           new StaticMethodCallExpression(new ClassNode(Logger), "getLogger",
> new ClassExpression(classNode))
>       )
>   }
> }
>
> One downside of using a local transform for this task is that you won't be
> able to refer to 'log' via its class name from within the class because the
> Groovy compiler will complain about a missing property 'log' before your
> transform kicks in. To solve this problem you'll probably need to use a
> global transform in phase CONVERSION. Such a transform will look pretty much
> like your version of the local transform, only that it will take a bit more
> work to identify @WithLog annotations because types haven't been resolved
> yet.
>
> Hope this helps.
>
> Cheers,
> Peter
>
> PS: Please provide output and/or stacktrace when asking such a question.
>
>
> Robert Fischer wrote:
>> I'm working on a "WithLog" AST transform that will basically do this to
>> the attached class:
>>
>> private static final Logger log =
>> Logger.getLogger(WhateverTheClassNameIs.class)
>>
>> Not a big deal.  Once I got my head wrapped around what is going on, I
>> created this annotation and impl:
>>
>> https://svn.codehaus.org/grails-plugins/grails-sublog/trunk/src/groovy/WithLog.groovy
>> https://svn.codehaus.org/grails-plugins/grails-sublog/trunk/src/groovy/WithLogImpl.groovy
>>
>> Unfortunately, this test doesn't work:
>>
>> https://svn.codehaus.org/grails-plugins/grails-sublog/trunk/test/unit/ASTTransformTests.groovy
>> https://svn.codehaus.org/grails-plugins/grails-sublog/trunk/src/groovy/testing/Foo.groovy
>>
>> Any idea why?  I'm totally at a loss for how to debug Groovy AST
>> transforms when they don't seem to
>> be being called at all, but are compiled in fine.
>>
>> ~~ Robert Fischer, Smokejumper IT Consulting.
>> Enfranchised Mind Blog http://EnfranchisedMind.com/blog
>>
>> Check out my book, "Grails Persistence with GORM and GSQL"!
>> http://www.smokejumperit.com/redirect.html
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>>
>

--
~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Check out my book, "Grails Persistence with GORM and GSQL"!
http://www.smokejumperit.com/redirect.html

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: Groovy AST not taking?