Overloading setProperties on a domain class from a plugin

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

Overloading setProperties on a domain class from a plugin

Robert Fletcher-2
The Gson plugin attempts to overload domain class setProperties with an implementation that takes a com.google.gson.JsonObject. Unfortunately I've discovered that while this works in unit tests in an integration test or the running app it completely overrides the setProperties provided by ControllersDomainBindingApi resulting in GroovyCastException if you try to use `obj.properties = x` where `x` is anything other than a JsonObject.

I'm not sure why this should be the case given that I'm strongly typing the parameter to my implementation of setProperties, it seems like the behavior I'm seeing is violating how method dispatch should work in Groovy.


Is anyone able to point me in the right direction to get this working? Do I need to apply my setProperties impl using an AST transform as well? If so why? I haven't done so yet because I'm also overloading domain class constructors which AFAIK can't be done with an AST transform. Is this a concern that Groovy's method dispatch behavior is seemingly being violated here?
Reply | Threaded
Open this post in threaded view
|

Re: Overloading setProperties on a domain class from a plugin

Robert Fletcher-2
After some more investigation I found that the method is only being dispatched incorrectly if I use the 

    obj.properties = x

syntax. If I use

    obj.setProperties(x)

instead then everything works as expected.


On Thu, Mar 28, 2013 at 9:56 AM, Robert Fletcher <[hidden email]> wrote:
The Gson plugin attempts to overload domain class setProperties with an implementation that takes a com.google.gson.JsonObject. Unfortunately I've discovered that while this works in unit tests in an integration test or the running app it completely overrides the setProperties provided by ControllersDomainBindingApi resulting in GroovyCastException if you try to use `obj.properties = x` where `x` is anything other than a JsonObject.

I'm not sure why this should be the case given that I'm strongly typing the parameter to my implementation of setProperties, it seems like the behavior I'm seeing is violating how method dispatch should work in Groovy.


Is anyone able to point me in the right direction to get this working? Do I need to apply my setProperties impl using an AST transform as well? If so why? I haven't done so yet because I'm also overloading domain class constructors which AFAIK can't be done with an AST transform. Is this a concern that Groovy's method dispatch behavior is seemingly being violated here?

Reply | Threaded
Open this post in threaded view
|

Re: Overloading setProperties on a domain class from a plugin

basejump (Josh)
I fought a little with something similar a few months ago.
I don't totally recall how we solved it but there were a few things that I remember.

- there were inconstancies with adding a setter method on the domainClass.metaClass vs domainClass.clazz.metaClass. I ended up using domainClass.clazz.metaClass
- It seemed to get confused with where to go when the parameter is typed. Try replacing the setProperties that is added via the MetaClassEnhancer from ControllersDomainBindingApi instead of adding another that expects a typed jsonObject. then delgate to the original if your param is not a jsonobject.

I think it might be getting confused because setProperties is added during AST as well. I"m not totally sure why its done with AST as well as via metaclass via the ControllersDomainBindingApi.


On Mar 28, 2013, at 9:35 AM, Robert Fletcher wrote:

After some more investigation I found that the method is only being dispatched incorrectly if I use the 

    obj.properties = x

syntax. If I use

    obj.setProperties(x)

instead then everything works as expected.


On Thu, Mar 28, 2013 at 9:56 AM, Robert Fletcher <[hidden email]> wrote:
The Gson plugin attempts to overload domain class setProperties with an implementation that takes a com.google.gson.JsonObject. Unfortunately I've discovered that while this works in unit tests in an integration test or the running app it completely overrides the setProperties provided by ControllersDomainBindingApi resulting in GroovyCastException if you try to use `obj.properties = x` where `x` is anything other than a JsonObject.

I'm not sure why this should be the case given that I'm strongly typing the parameter to my implementation of setProperties, it seems like the behavior I'm seeing is violating how method dispatch should work in Groovy.


Is anyone able to point me in the right direction to get this working? Do I need to apply my setProperties impl using an AST transform as well? If so why? I haven't done so yet because I'm also overloading domain class constructors which AFAIK can't be done with an AST transform. Is this a concern that Groovy's method dispatch behavior is seemingly being violated here?


Reply | Threaded
Open this post in threaded view
|

Re: Overloading setProperties on a domain class from a plugin

Robert Fletcher-2
On Thu, Mar 28, 2013 at 4:52 PM, Josh (basejump) <[hidden email]> wrote:
- It seemed to get confused with where to go when the parameter is typed. Try replacing the setProperties that is added via the MetaClassEnhancer from ControllersDomainBindingApi instead of adding another that expects a typed jsonObject. then delgate to the original if your param is not a jsonobject.


I've done exactly that & got it working. It's what I was trying to avoid but in this instance seems to be the only practical solution. Thanks for taking the time to respond.