在 groovy 中,创建方法闭包非常容易,例如:
groovy:000> p = 1.&plus
===> org.codehaus.groovy.runtime.MethodClosure@a7981d5
groovy:000> p(3)
===> 4
但是,由于某种原因,它在尝试使用以下实例时失败Class
:
groovy:000> List.isInstance([])
===> true
groovy:000> t = List.&isInstance
===> org.codehaus.groovy.runtime.MethodClosure@31ca1a68
groovy:000> t([])
ERROR groovy.lang.MissingMethodException:
No signature of method: java.util.List.isInstance() is applicable for argument types: (java.util.ArrayList) values: [[]]
at groovysh_evaluate.run (groovysh_evaluate:2)
...
groovy:000> t = List.class.&isInstance
===> org.codehaus.groovy.runtime.MethodClosure@7b34c5ff
groovy:000> t([])
ERROR groovy.lang.MissingMethodException:
No signature of method: java.util.List.isInstance() is applicable for argument types: (java.util.ArrayList) values: [[]]
at groovysh_evaluate.run (groovysh_evaluate:2)
...
解决这个问题很容易,但我想了解为什么会发生这种情况。MOP 中有什么东西可以阻止它工作等吗?