You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by John Smiljanic <jo...@gmail.com> on 2016/04/20 22:08:03 UTC

Adding a Trait to a ClassNode during semantic analysis

Hi All,

Groovy 2.3.7.

I've been experimenting with trying to add a Trait (or Mixin) to a
ClassNode during semantic analysis (compiler phase).  I've tried using
ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I
sort of expected ClassNode to have an addTrait or something like.

Is setting a trait at this phase of compilation supported?

JR

Re: Adding a Trait to a ClassNode during semantic analysis

Posted by Mario Garcia <ma...@gmail.com>.
I'm afraid the latest CompilePhase where you can use that approach is
CONVERSION. That means that in order to achieve what you wanted, you should
be using a global transformation.

Then you can make the ClassNode to implement the given trait .

Hope this helps :)
Mario

2016-04-20 22:08 GMT+02:00 John Smiljanic <jo...@gmail.com>:

> Hi All,
>
> Groovy 2.3.7.
>
> I've been experimenting with trying to add a Trait (or Mixin) to a
> ClassNode during semantic analysis (compiler phase).  I've tried using
> ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I
> sort of expected ClassNode to have an addTrait or something like.
>
> Is setting a trait at this phase of compilation supported?
>
> JR
>

Re: Adding a Trait to a ClassNode during semantic analysis

Posted by Mario Garcia <ma...@gmail.com>.
Ok thanks!
Mario

2016-04-25 19:23 GMT+02:00 John Smiljanic <jo...@gmail.com>:

> Two separate compilations.  One for the traits and transforms (framework
> code).  The second for the user scripts that are transformed to dynamically
> implement the trait.
>
> On Apr 22, 2016, at 01:33, Mario Garcia <ma...@gmail.com> wrote:
>
> Nice :)
>
> Just to get this straight, you compiled the traits first and then you
> compiled the local transformation and the user-developer code altogether.
> It's that correct ?
>
>
>
> 2016-04-21 23:16 GMT+02:00 John Smiljanic <jo...@gmail.com>:
>
>> After some digging I found
>> org.codehaus.groovy.transform.trait.TraitComposer and from there I was able
>> to backtrack to how groovy transforms statically defined traits.  This is
>> performed in canonalization, so it appears that a trait that is added to a
>> class during semantic analysis may still have its fields/behavior applied
>> to that class.  So, I was able to get this to work as follows:
>>
>> 1.  Compile the trait.
>>
>> I was having some problems jointly compiling the trait while compiling
>> the class that implements the trait when using the embedded compiler.  If I
>> recall correctly the base issue was that if I declared the trait as follows:
>>
>>
>>             classNode.addInterface(new ClassNode(
>>                <trait name>
>>                , Opcodes.ACC_INTERFACE
>>                , ClassHelper.make(Object.class)));
>>
>> the TraitComposer did not recognize the interface ClassNode as a Trait
>> (no @Trait annotation).  I suppose that the ClassNode constructor above
>> does not trigger a proper compilation/load of my trait's groovy file (?),
>> but I frankly didn't dig much deeper.
>>
>> This is fine for my purposes.  In my case, the Trait developer (framework
>> developer)  is different than the class developer (citizen developer).
>>
>> 2.  Declare the trait during semantic analysis of the Class.
>>
>> classNode.addInterface(new ClassNode(<trait javaclass from 1>);
>>
>>
>>
>> On Wed, Apr 20, 2016 at 3:08 PM, John Smiljanic <john.smiljanic@gmail.com
>> > wrote:
>>
>>> Hi All,
>>>
>>> Groovy 2.3.7.
>>>
>>> I've been experimenting with trying to add a Trait (or Mixin) to a
>>> ClassNode during semantic analysis (compiler phase).  I've tried using
>>> ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I
>>> sort of expected ClassNode to have an addTrait or something like.
>>>
>>> Is setting a trait at this phase of compilation supported?
>>>
>>> JR
>>>
>>
>>
>

Re: Adding a Trait to a ClassNode during semantic analysis

Posted by John Smiljanic <jo...@gmail.com>.
Two separate compilations.  One for the traits and transforms (framework code).  The second for the user scripts that are transformed to dynamically
implement the trait.

> On Apr 22, 2016, at 01:33, Mario Garcia <ma...@gmail.com> wrote:
> 
> Nice :)
> 
> Just to get this straight, you compiled the traits first and then you compiled the local transformation and the user-developer code altogether. It's that correct ?
> 
> 
> 
> 2016-04-21 23:16 GMT+02:00 John Smiljanic <jo...@gmail.com>:
>> After some digging I found org.codehaus.groovy.transform.trait.TraitComposer and from there I was able to backtrack to how groovy transforms statically defined traits.  This is performed in canonalization, so it appears that a trait that is added to a class during semantic analysis may still have its fields/behavior applied to that class.  So, I was able to get this to work as follows:
>> 
>> 1.  Compile the trait.
>> 
>> I was having some problems jointly compiling the trait while compiling the class that implements the trait when using the embedded compiler.  If I recall correctly the base issue was that if I declared the trait as follows:
>> 
>> 
>>             classNode.addInterface(new ClassNode(
>>                <trait name>
>>                , Opcodes.ACC_INTERFACE
>>                , ClassHelper.make(Object.class)));
>> 
>> the TraitComposer did not recognize the interface ClassNode as a Trait (no @Trait annotation).  I suppose that the ClassNode constructor above does not trigger a proper compilation/load of my trait's groovy file (?), but I frankly didn't dig much deeper.
>> 
>> This is fine for my purposes.  In my case, the Trait developer (framework developer)  is different than the class developer (citizen developer).
>> 
>> 2.  Declare the trait during semantic analysis of the Class.
>> 
>> classNode.addInterface(new ClassNode(<trait javaclass from 1>);
>> 
>> 
>> 
>>> On Wed, Apr 20, 2016 at 3:08 PM, John Smiljanic <jo...@gmail.com> wrote:
>>> Hi All,
>>> 
>>> Groovy 2.3.7.
>>> 
>>> I've been experimenting with trying to add a Trait (or Mixin) to a ClassNode during semantic analysis (compiler phase).  I've tried using ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I sort of expected ClassNode to have an addTrait or something like. 
>>> 
>>> Is setting a trait at this phase of compilation supported?
>>> 
>>> JR
> 

Re: Adding a Trait to a ClassNode during semantic analysis

Posted by Mario Garcia <ma...@gmail.com>.
Nice :)

Just to get this straight, you compiled the traits first and then you
compiled the local transformation and the user-developer code altogether.
It's that correct ?



2016-04-21 23:16 GMT+02:00 John Smiljanic <jo...@gmail.com>:

> After some digging I found
> org.codehaus.groovy.transform.trait.TraitComposer and from there I was able
> to backtrack to how groovy transforms statically defined traits.  This is
> performed in canonalization, so it appears that a trait that is added to a
> class during semantic analysis may still have its fields/behavior applied
> to that class.  So, I was able to get this to work as follows:
>
> 1.  Compile the trait.
>
> I was having some problems jointly compiling the trait while compiling the
> class that implements the trait when using the embedded compiler.  If I
> recall correctly the base issue was that if I declared the trait as follows:
>
>
>             classNode.addInterface(new ClassNode(
>                <trait name>
>                , Opcodes.ACC_INTERFACE
>                , ClassHelper.make(Object.class)));
>
> the TraitComposer did not recognize the interface ClassNode as a Trait (no
> @Trait annotation).  I suppose that the ClassNode constructor above does
> not trigger a proper compilation/load of my trait's groovy file (?), but I
> frankly didn't dig much deeper.
>
> This is fine for my purposes.  In my case, the Trait developer (framework
> developer)  is different than the class developer (citizen developer).
>
> 2.  Declare the trait during semantic analysis of the Class.
>
> classNode.addInterface(new ClassNode(<trait javaclass from 1>);
>
>
>
> On Wed, Apr 20, 2016 at 3:08 PM, John Smiljanic <jo...@gmail.com>
> wrote:
>
>> Hi All,
>>
>> Groovy 2.3.7.
>>
>> I've been experimenting with trying to add a Trait (or Mixin) to a
>> ClassNode during semantic analysis (compiler phase).  I've tried using
>> ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I
>> sort of expected ClassNode to have an addTrait or something like.
>>
>> Is setting a trait at this phase of compilation supported?
>>
>> JR
>>
>
>

Re: Adding a Trait to a ClassNode during semantic analysis

Posted by John Smiljanic <jo...@gmail.com>.
After some digging I found
org.codehaus.groovy.transform.trait.TraitComposer and from there I was able
to backtrack to how groovy transforms statically defined traits.  This is
performed in canonalization, so it appears that a trait that is added to a
class during semantic analysis may still have its fields/behavior applied
to that class.  So, I was able to get this to work as follows:

1.  Compile the trait.

I was having some problems jointly compiling the trait while compiling the
class that implements the trait when using the embedded compiler.  If I
recall correctly the base issue was that if I declared the trait as follows:


            classNode.addInterface(new ClassNode(
               <trait name>
               , Opcodes.ACC_INTERFACE
               , ClassHelper.make(Object.class)));

the TraitComposer did not recognize the interface ClassNode as a Trait (no
@Trait annotation).  I suppose that the ClassNode constructor above does
not trigger a proper compilation/load of my trait's groovy file (?), but I
frankly didn't dig much deeper.

This is fine for my purposes.  In my case, the Trait developer (framework
developer)  is different than the class developer (citizen developer).

2.  Declare the trait during semantic analysis of the Class.

classNode.addInterface(new ClassNode(<trait javaclass from 1>);



On Wed, Apr 20, 2016 at 3:08 PM, John Smiljanic <jo...@gmail.com>
wrote:

> Hi All,
>
> Groovy 2.3.7.
>
> I've been experimenting with trying to add a Trait (or Mixin) to a
> ClassNode during semantic analysis (compiler phase).  I've tried using
> ClassNode::addInterface and ClassNode::addMixin passing the appropriate.  I
> sort of expected ClassNode to have an addTrait or something like.
>
> Is setting a trait at this phase of compilation supported?
>
> JR
>