Groovy has a size property for Collection?

不羁的心 提交于 2020-01-03 18:52:07

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/26532434/groovy-has-a-size-property-for-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!