Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy has a size property for Collection?

I have written a piece of code where I'm checking the size of an ArrayList like:

[1,2,3].size

All works well on Groovy Console and with Grails embedded Tomcat server. But once I deployed this code to Websphere Application Server, I receivec 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 a while of debugging, testing and plenty of WTFs, I realized that there were parenthesis missing from the method call. The property notation should not work as there's no method getSize() for Collection (it's plain size()) and this all makes sense.

What's puzzling me, is why does someCollection.size work on Groovy Console and Grails?

Grails and Groovy Console version is 2.3.6

like image 239
kaskelotti Avatar asked Sep 29 '22 13:09

kaskelotti


1 Answers

ArrayList in (at least) the Sun JDK 1.7u67 and in OpenJDK 1.6 holds a private int size, which is accessible to groovy. If your other environment uses another JDK, this var might not exist and groovy would fallback to the interpretation of [1,2,3]*.getSize(), which then fails.

like image 68
cfrick Avatar answered Oct 03 '22 01:10

cfrick