defaultNullable in Validateable

126 views
Skip to first unread message

Aaron

unread,
Oct 21, 2015, 12:32:54 AM10/21/15
to Grails Dev Discuss
Working on porting from Grails 2.4 to 3.0.9 and came across something confusing. The @Validateable annotation used to take nullable=true to indicate that the default behavior should be to default unspecified properties to nullable instead of non-nullable.

In Grails 3, the Validateable trait has a defaultNullable() method which returns a boolean. It seems the best option would be to override the method and return true instead of false. However, for some reason the method is static.

Is there some reason this method is static? 

Jeff Scott Brown

unread,
Oct 21, 2015, 6:50:38 AM10/21/15
to grails-de...@googlegroups.com
I think it should be static unless there is a reason for it to be an instance method. I don’t think it would ever make sense to have the default nullable behavior be bound to an instance.



JSB

Jeff Scott Brown
Principal Software Engineer
Grails Development Team
Object Computing Inc.
http://www.ociweb.com/

Autism Strikes 1 in 166
Find The Cause ~ Find The Cure
http://www.autismspeaks.org/

Aaron

unread,
Oct 21, 2015, 9:49:10 AM10/21/15
to Grails Dev Discuss
I was thinking it needed to be non-static so I could override it in classes that implement the trait. However, playing around I see that implementing classes can apparently override static methods as well in the trait (I guess I was thinking about it too 'java').

If I had @Validateable(nullable = true) on a command object, would switching to implements Validateable and then adding static boolean defaultNullable() { return true } be the way to get the same behavior in Grails 3?

Thanks,
Aaron

Jeff Scott Brown

unread,
Oct 21, 2015, 10:30:36 AM10/21/15
to grails-de...@googlegroups.com

> On Oct 21, 2015, at 8:49 AM, Aaron <lon...@gmail.com> wrote:
>
> I was thinking it needed to be non-static so I could override it in classes that implement the trait. However, playing around I see that implementing classes can apparently override static methods as well in the trait (I guess I was thinking about it too 'java').
>
> If I had @Validateable(nullable = true) on a command object, would switching to implements Validateable and then adding static boolean defaultNullable() { return true } be the way to get the same behavior in Grails 3?


It should, yes.

There is some test code at https://github.com/grails/grails-core/blob/v3.0.9/grails-test-suite-uber/src/test/groovy/grails/validation/ValidateableTraitSpec.groovy which includes testing a class which overrides defaultNullable(). There are also some docs at http://grails.github.io/grails-doc/3.0.9/guide/theWebLayer.html#commandObjects which shows an example. The Validateable docs are a little fragmented right now. The notes at http://grails.github.io/grails-doc/3.0.9/guide/validation.html#validationNonDomainAndCommandObjectClasses should be aligned with the command object notes I linked above. Pull requests to https://github.com/grails/grails-doc are welcome. :)

Aaron

unread,
Oct 21, 2015, 1:49:47 PM10/21/15
to Grails Dev Discuss
Thanks, I'll play around with it.

IMO, it still might be preferable to have the trait method not be static. The problem with overriding a static method in a trait is that the IDE has no idea what's going on with it. IntelliJ thinks at best the method is unused. If you try to add @Override, it just flat out hates it. I guess if someone wanted to have a per-instance definition of that, it wouldn't preclude it either (although I agree I can't see how that would be useful).

-Aaron

Jeff Scott Brown

unread,
Oct 21, 2015, 2:45:04 PM10/21/15
to grails-de...@googlegroups.com

> On Oct 21, 2015, at 12:49 PM, Aaron <lon...@gmail.com> wrote:
> The problem with overriding a static method in a trait is that the IDE has no idea what's going on with it.

It doesn’t surprise me that you can’t mark a static trait method with @Override. A method marked with @Override must be overriding a method inherited from a parent class or from an implemented interface. Instance methods in a Trait are represented in the corresponding interface. Static methods aren’t.
Reply all
Reply to author
Forward
0 new messages