[英]Pass argument to bytebuddy interceptor
I have an interceptor using Byte Buddy and I want to pass an argument to the interceptor. 我有一个使用Byte Buddy的拦截器,并且想将参数传递给拦截器。 How can I do this?
我怎样才能做到这一点?
ExpressionHandler expressionHandler = ... // a handler
Method method = ... // the method that will be intercepted
ByteBuddy bb = new ByteBuddy();
bb.subclass(theClazz)
.method(ElementMatchers.is(method))
.intercept(MethodDelegation.to(MethodInterceptor.class));
.make()
.load(theClazz.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
The intercepting method in MethodInterceptor
is: MethodInterceptor
的拦截方法为:
@RuntimeType
public static Attribute intercept(@Origin Method method, @AllArguments Object[] args) throws Exception {
String name = method.getName();
Class<? extends Attribute> type = (Class<? extends Attribute>) method.getReturnType();
ExpressionHandler expressionHandler= // ???
expressionHandler.attachStuff(name, type);
return expressionHandler;
}
How can I pass the expressionHandler
from the builder to the interceptor method? 如何将
expressionHandler
从构建器传递到拦截器方法?
Simply use instance delegation instead of class-level delegation: 只需使用实例委托而不是类级委托即可:
MethodDelegation.to(new MethodInterceptor(expressionHandler))
with 与
public class MethodInterceptor {
private final ExpressionHandler expressionHandler;
public MethodInterceptor(ExpressionHandler expressionHandler) {
this.expressionHandler = expressionHandler;
}
@RuntimeType
public Attribute intercept(@Origin Method method, @AllArguments Object[] args) throws Exception {
String name = method.getName();
Class<? extends Attribute> type = (Class<? extends Attribute>) method.getReturnType();
this.expressionHandler.attachStuff(name, type);
return expressionHandler;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.