Groovy有Collection的大小属性? [英] Groovy has a size property for Collection?

查看:197
本文介绍了Groovy有Collection的大小属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一段代码,我正在检查一个ArrayList的大小,如:

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

[1,2,3].size

在Groovy控制台和Grails嵌入式Tomcat服务器上都可以正常工作。但是一旦我把这个代码部署到Websphere Application Server,我收到一个异常

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.

经过一段时间的调试,测试和大量的WTF,我意识到,方法调用。属性符号不应该工作,因为没有方法 getSize()为集合(它是普通的 size())和这个

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.

令人困惑的是 someCollection.size 为什么在Groovy控制台上工作,

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

Grails和Groovy控制台版本是2.3.6

Grails and Groovy Console version is 2.3.6

推荐答案

ArrayList jdk6 / jdk / file / cdb085a5127c / src / share / classes / java / util / ArrayList.javarel =nofollow> OpenJDK 1.6 拥有 private int size ,这是groovy访问。如果你的其他环境使用另一个JDK,这个var可能不存在,groovy会回到 [1,2,3] *。getSize()的解释, 。

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.

这篇关于Groovy有Collection的大小属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆