question on AST transformations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

question on AST transformations

josedeoliveiraguimaraes@gmail.com

   I am writing an article on metaprogramming and have several questions on Groovy. I would thank any help with them.

   1. Is there any example in which @GroovyASTTransformationClass takes an array with more
   than one element? Something like this:
   
       @Retention(RetentionPolicy.SOURCE)
       @Target([ElementType.TYPE])
       @GroovyASTTransformationClass( [FirstTransformation, SecondTransformation] )
       @interface MyAnnot { }
   
   I could not find one. If this is possible, I suppose that a single annotation
   can apply transformations in several compiler phases.

   2. Suppose I want to use the annotation WithLogging of
        http://groovy-lang.org/metaprogramming.html#developing-ast-xforms
   All I have is the Groovy compiler, a .exe file. What do I do? The general question is: how the Groovy compiler finds a **local** AST transformation class? For global transformation the page above says

   Compiled classes that implement global transformations are in a JAR added to the classpath of the compiler and contain service locator file META-INF...

   But for local transformations, there is no such observation.
Reply | Threaded
Open this post in threaded view
|

Re: question on AST transformations

paulk_asert
Answers below

On Fri, Jun 1, 2018 at 7:57 AM, [hidden email] <[hidden email]> wrote:

   I am writing an article on metaprogramming and have several questions on Groovy. I would thank any help with them.

   1. Is there any example in which @GroovyASTTransformationClass takes an array with more
   than one element? Something like this:

       @Retention(RetentionPolicy.SOURCE)
       @Target([ElementType.TYPE])
       @GroovyASTTransformationClass( [FirstTransformation, SecondTransformation] )
       @interface MyAnnot { }

   I could not find one. If this is possible, I suppose that a single annotation
   can apply transformations in several compiler phase

Yes, this is possible. Examples in the Groovy codebase:
src/test/org/codehaus/groovy/transform/LocalASTTransformTest.groovy
src/test/groovy/bugs/G3839A2.java

 

   2. Suppose I want to use the annotation WithLogging of
        http://groovy-lang.org/metaprogramming.html#developing-ast-xforms
   All I have is the Groovy compiler, a .exe file. What do I do? The general question is: how the Groovy compiler finds a **local** AST transformation class? For global transformation the page above says

   Compiled classes that implement global transformations are in a JAR added to the classpath of the compiler and contain service locator file META-INF...

   But for local transformations, there is no such observation.

You need the annotation definition and transformation class in your classpath. The annotation annotating your code is what triggers the compiler to invoke the transformation.

Cheers, Paul.

Reply | Threaded
Open this post in threaded view
|

Re: question on AST transformations

josedeoliveiraguimaraes@gmail.com
Thanks a lot, Paul. That will be very helpful in the second article I am preparing on the Metaobject Protocol of language Cyan. The first article is available in http://cyan-lang.org/articles/. By the way, it cites your Groovy book.

One more question: suppose an AST transformation class inserts a method and a field in a class 'HelpMe'. That is made in the SEMANTIC_ANALYSIS phase (maybe another phase, I think it does not matter). Another class 'Other' accesses the inserted field and call the inserted method of 'HelpMe'. Annotation @CompileStatic is attached to 'Other'. Assume that the semantic analysis of 'Other' is made before that of 'HelpMe' (I am assuming there is no pre-defined compilation order between the source files, then it could be 'HelpMe' before 'Other' but the problem that follows should not happen in this case). That will cause a compile error because the field and method will not be found in 'HelpMe'. Is that correct?

Cheers, José
Reply | Threaded
Open this post in threaded view
|

Re: question on AST transformations

paulk_asert
I'm on holidays and can't really check right now but I wouldn't necessarily assume a compilation error.

On Fri., 1 Jun. 2018, 7:25 pm [hidden email], <[hidden email]> wrote:
Thanks a lot, Paul. That will be very helpful in the second article I am preparing on the Metaobject Protocol of language Cyan. The first article is available in http://cyan-lang.org/articles/. By the way, it cites your Groovy book.

One more question: suppose an AST transformation class inserts a method and a field in a class 'HelpMe'. That is made in the SEMANTIC_ANALYSIS phase (maybe another phase, I think it does not matter). Another class 'Other' accesses the inserted field and call the inserted method of 'HelpMe'. Annotation @CompileStatic is attached to 'Other'. Assume that the semantic analysis of 'Other' is made before that of 'HelpMe' (I am assuming there is no pre-defined compilation order between the source files, then it could be 'HelpMe' before 'Other' but the problem that follows should not happen in this case). That will cause a compile error because the field and method will not be found in 'HelpMe'. Is that correct?

Cheers, José