Accessing pseudo properties with Groovy 4.x throws a MissingPropertyException.

huangapple go评论44阅读模式
英文:

Accessing pseudo properties with Groovy 4.x throws a MissingPropertyException

问题

当我尝试访问以下类的activated伪属性时,在Groovy 4.x上出现了MissingPropertyException

@Test
void beanPropertiesTest() {
   assert new User().activated // Fails
}

class User {

    Boolean isActivated() {
        return true
    }
}

但在Groovy 3.x上,代码正常工作。是否需要设置AST注解或编译器标志?

我使用的是Java 17。

英文:

When I try to access the activated pseudo property on the following class, I get a MissingPropertyException on Groovy 4.x:

@Test
void beanPropertiesTest() {
   assert new User().activated // Fails
}

class User {

    Boolean isActivated() {
        return true
    }
}

The code works fine on Groovy 3.x though. Is there an AST annotation or compiler flag that I need to set?

I'm on Java 17.

答案1

得分: 1

在经过长时间搜索后,我相信我已经找到了解决方案,那就是将返回类型从 Boolean 更改为 boolean4.0.0 变更日志 引用了 此 JIRA 问题,其中包含 Eric Miles 的 这个评论

> 返回任何其他类型的“is”方法都不是属性访问器。

进一步阅读揭示了他所说的“任何其他类型”指的是原始的 boolean 以外的任何其他类型,如 Paul King 在 这里 进一步解释的那样。

GitHub 上的以下拉取请求巩固了更改,推荐使用 isName() 而不是 getName() 用于 boolean 访问器:

GROOVY-9382,GROOVY-10133:更喜欢为 boolean 使用 isName()

现在,Boolean 访问器将不得不使用 getName(),以便与 JavaBean 规范(参见:Introspection 8.3.2)正确对齐。

英文:

After hours of searching, I believe I have found that the solution is to change the return type from Boolean to boolean. The 4.0.0 change log referenced this JIRA issue with this comment from Eric Miles:

> An “is” method that returns any other type is not a property accessor.

Further reading revealed that by "any other type" he meant any other type than the primitive boolean, as further explained by Paul King here.

The following pull request on GitHub cemented the change to prefer isName() over getName() for boolean accessors:

GROOVY-9382, GROOVY-10133: prefer isName() over getName() for boolean

Boolean accessors will now have to use getName() to properly align it with the JavaBean specification (See: Introspection 8.3.2).

huangapple
  • 本文由 发表于 2023年5月18日 05:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定