|
I'd like to try using the ASTTransformation on my project, but I'm not clear how to add transformations.
I see ClassNode has an addTransform method: http://groovy.codehaus.org/api/org/codehaus/groovy/ast/ClassNode.html#addTransform(java.lang.Class,%20org.codehaus.groovy.ast.ASTNode) My approach so far is to subclass GroovyClassLoader and then add a PhaseOperation, which in turn gives me a ClassNode. I think this will work, but it doesn't seem right. Is there an easy way for users to add an ASTTransformation? Is there some way to just use an annotation like this and have the tranformation magically invoked? @GroovyASTTransformation(phase= CompilePhase.CANONICALIZATION) I'd prefer to continue just using groovyc and having somethng like this work without mucking with compiling the files myself. Thanks, -- Hamlet D'Arcy [hidden email] |
|
This is question was asked earlier, but I'm still looking for an answer:
http://www.nabble.com/Adding-dynamic-property-to-all-domain-classes-td22234650.html#a22250749
|
Guillaume's article on InfoQ provides detailed information: http://www.infoq.com/articles/groovy-1-6 Cheers, Peter |
|
Sorry to be dense on this subject...
I understand Global Transformations. That works for me just fine. I can't get local transformations to work. Here is what I'm doing. Step 1: Define my annotation @Retention(RetentionPolicy.SOURCE) @Target([ElementType.METHOD]) @GroovyASTTransformationClass(["gep.transform.local.LoggingASTTransformation"]) public @interface WithLogging { } Step 2: Define my transformation (just trying to get something to happen here!) : @GroovyASTTransformation(phase=CompilePhase.CONVERSION) public class LoggingASTTransformation implements ASTTransformation { public void visit(ASTNode[] nodes, SourceUnit source) { System.err.println("LoggingASTTransformation was invoked") throw new RuntimeException("LoggingASTTransformation was invoked") } } Step 3: Try to compile a usage (which isn't working for me) @gep.transform.local.WithLogging def greet() { println "Hello World" } greet() I've tried 2 different things. I just call it call from groovyc like so: groovyc src\gep\transform\local\WithLogging.groovy src\gep\transform\local\LoggingASTTransformation.groovy src\gep\transform\local\LoggingExample.groovy This ignores my transformation. No error is caught and no error message appears. I also tried stuffing the Annotation and the Transformation into a jar file called "WithLogging.jar" and invoking groovyc like this: groovyc -cp WithLogging.jar src\gep\transform\local\LoggingExample.groovy Again, my transformation is ignored. Am I doing something obviously wrong for local transformations? Thanks, -- Hamlet D'Arcy [hidden email] On Sun, Mar 1, 2009 at 10:52 AM, Peter Niederwieser <[hidden email]> wrote:
|
|
Local transforms can't be run in phase CONVERSION but only from phase SEMANTIC_ANALYSIS onwards. I suggest you file a JIRA to issue an error if this is tried.
Cheers, Peter
|
|
Created: http://jira.codehaus.org/browse/GROOVY-3388
I assume there is a reason not to allow this. On Sun, Mar 1, 2009 at 3:50 PM, Peter Niederwieser <[hidden email]> wrote:
-- Hamlet D'Arcy [hidden email] |
Because types are only resolved in phase SEMANTIC_ANALYSIS, there isn't currently a way for the compiler to access, say, annotation type WithLogging and its GroovyASTTransformationClass meta-annotation in phase CONVERSION. Cheers, Peter |
|
In reply to this post by Hamlet D'Arcy
A global annotation can have a phase CONVERSION (for example @Grab does). So there is a real-world use of that phase. As stated by peter, local annotations are not resolved until SEMANTIC_ANALYSIS.
On Sun, Mar 1, 2009 at 7:15 PM, Hamlet D'Arcy <[hidden email]> wrote: Created: http://jira.codehaus.org/browse/GROOVY-3388 -- ------------------------------------------------------ I'm Danno Ferrin, and I approved this message. |
| Powered by Nabble | Edit this page |
