Does Groovy have a size property for a Collection?
I wrote a piece of code where I check the size of the ArrayList:
[1,2,3].size
Everything works well in the Groovy console and with Grails' built-in Tomcat server. But as soon as I deployed this code to the Websphere Application Server, I get an exception stating
Exception evaluating property 'size' for java.util.ArrayList, Reason: groovy.lang.MissingPropertyException: No such property: size for class: java.lang.Integer.
After some time of debugging, testing and lots of WTFs, I realized that there were no parentheses in the method call. Property notation shouldn't work as there is no method getSize()
for Collection (it's simple size()
) and it all makes sense.
What puzzles me is why does it someCollection.size
work in Groovy Console and Grails?
Grails and Groovy console version 2.3.6
source to share
ArrayList
in (at least) Sun JDK 1.7u67 and OpenJDK 1.6 contains private int size
, available for groovy. If your other environment uses a different JDK, this var may not exist and groovy will fall back to interpret [1,2,3]*.getSize()
which then fails.
source to share