Gradle choosing wrong setter when executing closure

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Gradle choosing wrong setter when executing closure

Xavier Ducrohet
I have something like that looks like this:

foo {
   myProp = 9
}

The class being configured (represented by foo) has 2 different SetMyProp. One receives an instance of a class 'Bar' and the other one an int.

Gradle fails with

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '9' with class 'java.lang.Integer' to class 'Bar'
at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.setProperty(BeanDynamicObject.java:192)
at org.gradle.api.internal.BeanDynamicObject.setProperty(BeanDynamicObject.java:117)
at org.gradle.api.internal.CompositeDynamicObject.setProperty(CompositeDynamicObject.java:101)
at com.android.build.gradle.internal.dsl.GroupableProductFlavorDsl_Decorated.setProperty(Unknown Source)


When I saw this, I added a new setMyProp that receives an Integer, but this failed the same way. Editing build.gradle with 

foo {
   myProp 9
}

Made it call my custom myProp() method, of which there are 3 version (receiving an int, an Integer, and finally an instance of 'Bar'), and it chose the right one.

Note that there is a single getter than returns an instance of Bar so I'm wondering if this forces setProperty to use the matching getter. Still this is not very convenient.

Anyway to work around that? thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Gradle choosing wrong setter when executing closure

Luke Daley-2
Groovy doesn’t support overloaded setters and properties: https://jira.codehaus.org/browse/GROOVY-2500

We could support it though as we have our own dynamic layer. This would be a divergence from Groovy behaviour and only work for Gradle managed objects though.

On 5 June 2014 at 11:42:26 am, Xavier Ducrohet ([hidden email]) wrote:

I have something like that looks like this:

foo {
   myProp = 9
}

The class being configured (represented by foo) has 2 different SetMyProp. One receives an instance of a class 'Bar' and the other one an int.

Gradle fails with

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '9' with class 'java.lang.Integer' to class 'Bar'
at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.setProperty(BeanDynamicObject.java:192)
at org.gradle.api.internal.BeanDynamicObject.setProperty(BeanDynamicObject.java:117)
at org.gradle.api.internal.CompositeDynamicObject.setProperty(CompositeDynamicObject.java:101)
at com.android.build.gradle.internal.dsl.GroupableProductFlavorDsl_Decorated.setProperty(Unknown Source)


When I saw this, I added a new setMyProp that receives an Integer, but this failed the same way. Editing build.gradle with 

foo {
   myProp 9
}

Made it call my custom myProp() method, of which there are 3 version (receiving an int, an Integer, and finally an instance of 'Bar'), and it chose the right one.

Note that there is a single getter than returns an instance of Bar so I'm wondering if this forces setProperty to use the matching getter. Still this is not very convenient.

Anyway to work around that? thanks.
— 

Luke Daley
Gradleware
Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA: http://www.gradlesummit.com
Reply | Threaded
Open this post in threaded view
|

Re: Gradle choosing wrong setter when executing closure

Xavier Ducrohet
Well, this is unfortunate. I guess we should recommend people not use the equal sign then.


On Wed, Jun 4, 2014 at 6:46 PM, Luke Daley <[hidden email]> wrote:
Groovy doesn’t support overloaded setters and properties: https://jira.codehaus.org/browse/GROOVY-2500

We could support it though as we have our own dynamic layer. This would be a divergence from Groovy behaviour and only work for Gradle managed objects though.

On 5 June 2014 at 11:42:26 am, Xavier Ducrohet ([hidden email]) wrote:

I have something like that looks like this:

foo {
   myProp = 9
}

The class being configured (represented by foo) has 2 different SetMyProp. One receives an instance of a class 'Bar' and the other one an int.

Gradle fails with

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '9' with class 'java.lang.Integer' to class 'Bar'
at org.gradle.api.internal.BeanDynamicObject$MetaClassAdapter.setProperty(BeanDynamicObject.java:192)
at org.gradle.api.internal.BeanDynamicObject.setProperty(BeanDynamicObject.java:117)
at org.gradle.api.internal.CompositeDynamicObject.setProperty(CompositeDynamicObject.java:101)
at com.android.build.gradle.internal.dsl.GroupableProductFlavorDsl_Decorated.setProperty(Unknown Source)


When I saw this, I added a new setMyProp that receives an Integer, but this failed the same way. Editing build.gradle with 

foo {
   myProp 9
}

Made it call my custom myProp() method, of which there are 3 version (receiving an int, an Integer, and finally an instance of 'Bar'), and it chose the right one.

Note that there is a single getter than returns an instance of Bar so I'm wondering if this forces setProperty to use the matching getter. Still this is not very convenient.

Anyway to work around that? thanks.
— 

Luke Daley
Gradleware
Join us for Gradle Summit 2014, June 12th and 13th in Santa Clara, CA: http://www.gradlesummit.com