Weld 在 JDK 8u60 环境下的一个问题 已翻译 100%

oschina 投递于 2015/09/16 11:49 (共 2 段, 翻译完成于 09-18)
阅读 3976
收藏 7
0
加载中

Recently released JDK 8u60 has revealed a problem in Weld where not all synthetic members were ignored correctly. This has been fixed in 2.2.16.Final, 2.3.0.CR2 and 3.0.0.Alpha14  (not released yet).     However, if not using a lambda referencing an  event/disposed parameter inside an observer/disposer method, your  application is most probably not affected. See also the related issue - WELD-2019.

Use case

If there is a lambda referencing an event/disposed  parameter inside an observer/disposer method, the compiler creates a  synthetic method with the event parameter as one of the method  parameters. Since 8u60 the parameter annotations are preserved even for  the synthetic method. And so Weld incorrectly recognizes these synthetic  methods as observer/disposer methods.

已有 1 人翻译此段
我来翻译

Symptoms

You may get WELD-000409: Observer method for container lifecycle event can only… if using such a lambda in an extension or even WELD-001408 Unsatisfied dependencies for type…  if using such a lambda in a regular observer and the lambda is using  more references (i.e. not only event/disposed parameter) - these are  also the method parameters of the synthetic method, and in CDI these  additional parameters are injection points.

Example

class Foo {
  void observe(@Observes @Juicy String payload) {
    Arrays.asList("foo").stream().filter((s) -> s.equals(payload));
  }
}
A synthetic method is created for the lambda and  the event parameter is passed as a method parameter. The annotations are  preserved. As a result Weld creates two observer methods having  the same event parameter: @Observes @Juicy String payload.

Workaround

Instead of the event parameter reference use an additional local variable with the same value assigned:

public void observe(@Observes @Juicy String payload) {
   String p = payload;
   Arrays.asList("foo").stream().filter((s) -> s.equals(p));
}
已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(10)

osc_2318810
osc_2318810
语法越来越像C#了
__zero__
__zero__
卧槽,百度翻译的吧
Hello_Maybe
Hello_Maybe
333
非职业码农
非职业码农
确实没看懂,谁能讲讲代码是什么意思
博雅智信科技
博雅智信科技
没怎么看懂
guhai2004
guhai2004
学无止境,我竟然看不懂13
Liuzh_533
Liuzh_533
谁能说说上面这种写法的作用和好处?
景愿
景愿
你们叼叼的,我做了这么多年java愣是没看懂“用例”那段,什么是虚方法?
Credo-Zhao
Credo-Zhao
同上,还好没升级上去哈.
lanmingle
lanmingle
知道了,不过编译环境是7~
返回顶部
顶部