多实例的Groovy方法参考

多实例的Groovy方法参考,groovy,easybind,Groovy,Easybind,我正在从Java迁移到Groovy,在方法引用方面遇到了问题 在Java中,我可以这样做: Function<Bean, String> f = Bean::method; String s = f.apply(new Bean()); 但是我得到了一个例外,在f.apply行: groovy.lang.MissingMethodException: No signature of method: Bean.method() is applicable for argument t

我正在从Java迁移到Groovy,在方法引用方面遇到了问题

在Java中,我可以这样做:

Function<Bean, String> f = Bean::method;
String s = f.apply(new Bean());
但是我得到了一个例外,在
f.apply
行:

groovy.lang.MissingMethodException: No signature of method: Bean.method() is applicable for argument types: (Bean) values: [Bean@17483c58]
我知道我可以执行以下操作来获取实例方法的方法引用,但我想为任何实例获取泛型方法

MethodClosure f = bean.&method
String s = f()
我想用这个来使用EasyBind库。它允许您选择带有函数引用的JavaFX属性。您可能有一个类和属性的层次结构,要选择它们,您可以执行以下操作:

property.bind(EasyBind.select(root).select(Root::branch).selectObject(Branch::leaf));
因此,当树中的任何值发生更改时,
property
get将使用正确的值进行更新

我可以用
{Bean->Bean.method}
替换
Bean.&method
,效果很好。在Java中,
Bean::method
实际上是
Bean->Bean的别名类型

MethodClosure f = { it.method }
String s = f()

恐怕这是不可能的。只对静态方法有效。@Opal不鼓励我使用Groovy来做我当时正在做的事情:/Maybe给我们一个更广泛的上下文?我用这个用法更新了我的问题。我想你应该把你的解决方案作为答案发布。看起来不错。您也可以使用
{it.method}
MethodClosure f = { it.method }
String s = f()