Java 在MethodInterceptor中获取目标
如何在拦截器中获取目标对象Java 在MethodInterceptor中获取目标,java,guice,aop,aopalliance,Java,Guice,Aop,Aopalliance,如何在拦截器中获取目标对象 bindInterceptor(subclassesOf(A.class), any(), new MethodInterceptor() { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { A a = getTarget(); //how? return
bindInterceptor(subclassesOf(A.class), any(), new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
A a = getTarget(); //how?
return methodInvocation.proceed();
}
});
UPD
实际上,有基于反射的解决方案,但它希望有其他的解决方案
private static Object getTarget(MethodInvocation methodInvocation) throws NoSuchFieldException, IllegalAccessException {
return getFieldValue(methodInvocation, "proxy");
}
private static Object getFieldValue(Object obj, String field) throws NoSuchFieldException, IllegalAccessException {
Field f = obj.getClass().getDeclaredField(field);
f.setAccessible(true);
return f.get(obj);
}
它不只是
methodInvocation.getThis()
?是的。谢谢。我的拦截器中有一些bug,使用getThis()
返回一些错误的对象。现在它工作得很好。谢谢