You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by jo...@gmail.com,
jo...@gmail.com on 2018/05/31 21:57:24 UTC
question on AST transformations
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.
Re: question on AST transformations
Posted by Paul King <pa...@asert.com.au>.
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 josedeoliveiraguimaraes@gmail.com, <
josedeoliveiraguimaraes@gmail.com> 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é
>
Re: question on AST transformations
Posted by jo...@gmail.com,
jo...@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é
Re: question on AST transformations
Posted by Paul King <pa...@asert.com.au>.
Answers below
On Fri, Jun 1, 2018 at 7:57 AM, josedeoliveiraguimaraes@gmail.com <
josedeoliveiraguimaraes@gmail.com> 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.