我有一个对象service
,它有多个方法,其中一个方法是foo(arg1, arg2)
。
想要创建一个新的包装类:
- 有一个方法
_foo
以及一个附加参数 - 将
_foo
执行委托(delegate)给拦截器,返回值被忽略 - 最后,委托(delegate)调用
service
上的目标foo
。
不知何故,我没有这样做:
final List<Class<?>> parameters =
Arrays.stream(fooMethod.getParameters())
.map(Parameter::getType)
.collect(Collectors.toList());
parameters.add(AdditionalParameter.class);
final DynamicType.Unloaded unloadedType = new ByteBuddy()
.subclass(Object.class)
.implement(interfaceType)
.name(service.getClass().getSimpleName() + "Dynamic")
.defineMethod(
"_" + methodName,
resolveReturnType(fooMethod),
Modifier.PUBLIC)
.withParameters(parameters)
.intercept(to(fooInterceptor).andThen(
MethodCall.invoke(fooMethod).on(service)
))
.make();
fooInterceptor
是一个 InvocatiomHandler
实例:
public class FooInterceptor implements InvocationHandler {
public Object invoke(
@This final Object proxy,
@Origin final Method method.
@AllArguments final Object[] args) {
...
}
}
异常表明我的 fooService
“不接受 0 个参数”。
我可以从拦截器调用 service.foo()
- 但不使用反射吗?我无法这样做(但还没有玩过那个部分)。
帮忙吗?🙏
编辑:我无法控制 service
中的方法,因此我不能简单地使用 to(service)
进行拦截调用;可能存在 ByteBuddy 无法找到匹配方法的情况。
EDIT2:如果我可以“告诉”ByteBuddy 要绑定(bind)的目标方法的名称,那就太棒了。然后我可以使用带有给定提示的 to(service)
。
最佳答案
您可以向 MethodDelegation
提供匹配器来缩小要考虑的方法的范围:
MethodDelegation.withDefaultConfiguration().filter(...).to(...)
对于您的MethodCall
,您需要指定要包含的参数,foo
有两个参数。由于您的原始参数似乎是等效的,因此您可以设置:
MethodCall.invoke(fooMethod).on(service).withAllArguments();
关于java - 使用 ByteBuddy 定义方法、调用和拦截器并委托(delegate)给目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544019/