【发布时间】:2016-07-01 16:53:29
【问题描述】:
我正在尝试弄清楚如何使用 Byte Buddy 设置实例字段的值。文档说:
在调用此类动态类的实例的方法之前,请务必记住为该字段赋值。否则,方法委托将导致 NullPointerException。
但我在文档或单元测试中没有看到有关如何执行此操作的任何内容。
我的动态类是:
new ByteBuddy().subclass(AbstractService.class)
.name(serviceName)
.method(ElementMatchers.named("start").and(
ElementMatchers.takesArguments(0)))
.intercept(
MethodDelegation.toInstanceField(service, "consumer")
.filter(ElementMatchers.isAnnotatedWith(Start.class)))
.method(ElementMatchers.named("stop").and(
ElementMatchers.takesArguments(0)))
.intercept(
MethodDelegation.to(instance).filter(
ElementMatchers.isAnnotatedWith(Stop.class)))
.make();
我看到另一个帖子的答案是拦截任何构造函数并使用@FieldProxy 和MethodDelegation,但我不知道该怎么做。我在.constructor(ElementMatchers.any()).intercept(...) 的某些变化方面所做的一切尝试都会导致:
java.lang.IllegalArgumentException: [] 中的任何一个都不允许...
【问题讨论】:
标签: java bytecode instrumentation byte-buddy