Quantcast

how to add ASTTransformation objects to my project

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to add ASTTransformation objects to my project

Hamlet D'Arcy
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]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Hamlet D'Arcy
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




HamletDRC wrote
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
hamletdrc@gmail.com


-----
--
Hamlet D'Arcy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Peter Niederwieser
HamletDRC wrote
I'd like to try using the ASTTransformation on my project, but I'm not clear
how to add transformations.
Guillaume's article on InfoQ provides detailed information: http://www.infoq.com/articles/groovy-1-6

Cheers,
Peter
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Hamlet D'Arcy
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:


HamletDRC wrote:
>
> I'd like to try using the ASTTransformation on my project, but I'm not
> clear
> how to add transformations.
>

Guillaume's article on InfoQ provides detailed information:
http://www.infoq.com/articles/groovy-1-6

Cheers,
Peter

--
View this message in context: http://www.nabble.com/how-to-add-ASTTransformation-objects-to-my-project-tp22262310p22274974.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

   http://xircles.codehaus.org/manage_email




Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Peter Niederwieser
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

HamletDRC wrote
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
hamletdrc@gmail.com


On Sun, Mar 1, 2009 at 10:52 AM, Peter Niederwieser <pniederw@gmail.com>wrote:

>
>
> HamletDRC wrote:
> >
> > I'd like to try using the ASTTransformation on my project, but I'm not
> > clear
> > how to add transformations.
> >
>
> Guillaume's article on InfoQ provides detailed information:
> http://www.infoq.com/articles/groovy-1-6
>
> Cheers,
> Peter
>
> --
> View this message in context:
> http://www.nabble.com/how-to-add-ASTTransformation-objects-to-my-project-tp22262310p22274974.html
> Sent from the groovy - user mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>


-----
--
Hamlet D'Arcy
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Hamlet D'Arcy
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:

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


HamletDRC wrote:
>
> 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:
>
>>
>>
>> HamletDRC wrote:
>> >
>> > I'd like to try using the ASTTransformation on my project, but I'm not
>> > clear
>> > how to add transformations.
>> >
>>
>> Guillaume's article on InfoQ provides detailed information:
>> http://www.infoq.com/articles/groovy-1-6
>>
>> Cheers,
>> Peter
>>
>> --
>> View this message in context:
>> http://www.nabble.com/how-to-add-ASTTransformation-objects-to-my-project-tp22262310p22274974.html
>> Sent from the groovy - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
> -----
> --
> Hamlet D'Arcy
>
>

--
View this message in context: http://www.nabble.com/how-to-add-ASTTransformation-objects-to-my-project-tp22262310p22278488.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

   http://xircles.codehaus.org/manage_email





--
Hamlet D'Arcy
[hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Peter Niederwieser
HamletDRC wrote
I assume there is a reason not to allow this.
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to add ASTTransformation objects to my project

Danno Ferrin
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 assume there is a reason not to allow this.



On Sun, Mar 1, 2009 at 3:50 PM, 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


HamletDRC wrote:
>
> 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:
>
>>
>>
>> HamletDRC wrote:
>> >
>> > I'd like to try using the ASTTransformation on my project, but I'm not
>> > clear
>> > how to add transformations.
>> >
>>
>> Guillaume's article on InfoQ provides detailed information:
>> http://www.infoq.com/articles/groovy-1-6
>>
>> Cheers,
>> Peter
>>
>> --
>> View this message in context:
>> http://www.nabble.com/how-to-add-ASTTransformation-objects-to-my-project-tp22262310p22274974.html
>> Sent from the groovy - user mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
> -----
> --
> Hamlet D'Arcy
>
>

--
View this message in context: http://www.nabble.com/how-to-add-ASTTransformation-objects-to-my-project-tp22262310p22278488.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

   http://xircles.codehaus.org/manage_email





--
Hamlet D'Arcy
[hidden email]



--
------------------------------------------------------
I'm Danno Ferrin, and I approved this message.
Loading...