GroovyInterceptable (AOP) 和闭包

发布于 2024-10-09 17:40:55 字数 885 浏览 0 评论 0原文

我有一个 grails 应用程序,其中的 Service 类继承自 Groovy 的 GroovyInterceptable:

class customerSerrvice implements GroovyInterceptable {

private List<Customer> customers

def invokeMethod(String name, args) {
    log.debug "=======>INVOKING method [$name] with args:$args"
}

void foo() {
    customers.each { doSomething(it) }
}

void doSomething(Customer cust) { log.debug "doSomething invoked with $cust" }

}

上面是一个大大简化的表示,但它为您提供了想法。如果我从另一个类直接调用 foo() 或 doSomething() ,则 invokeMethod 会像预期的那样被调用。然而,当 foo() 调用 doSomething() 时,该调用不会在 invokeMethod 中被拦截。

更改

如果我从customers.each { doSomething(it) }


for(Customer cust:customers) { doSomething(cust) }

然后调用 invokeMethod 就可以了。

那么闭包和 GroovyInterceptable 有什么不兼容的地方吗?有什么方法可以让 invokeMethod 与闭包一起使用而不需要将它们全部更改掉吗?

谢谢

I've got a grails app with Service classes that inherit from Groovy's GroovyInterceptable:

class customerSerrvice implements GroovyInterceptable {

private List<Customer> customers

def invokeMethod(String name, args) {
    log.debug "=======>INVOKING method [$name] with args:$args"
}

void foo() {
    customers.each { doSomething(it) }
}

void doSomething(Customer cust) { log.debug "doSomething invoked with $cust" }

}

The above is a greatly simplified representation, but it gives you the idea. If I call foo() or doSomething() directly from another class, the invokeMethod gets called like it is supposed to. However, when foo() calls doSomething(), that call is not intercepted in invokeMethod.

If I change from

customers.each { doSomething(it) }

to
for(Customer cust: customers) { doSomething(cust) }

then the invokeMethod gets called just fine.

So is there something about closures and GroovyInterceptable that don't go together? Is there any way to get the invokeMethod to work with closures short of changing them all out?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唔猫 2024-10-16 17:40:55

确认为错误,旧链接:
http://jira.codehaus.org/browse/GROOVY-4610,新链接:
https://issues.apache.org/jira/browse/GROOVY-4610

Confirmed as a bug, old link:
http://jira.codehaus.org/browse/GROOVY-4610, new link:
https://issues.apache.org/jira/browse/GROOVY-4610

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文