How I can get Target object in my interceptor?
bindInterceptor(subclassesOf(A.class), any(), new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
A a = getTarget(); //how?
return methodInvocation.proceed();
}
});
UPD Actually, there is reflection based solution, but it hope that there other solutions..
private static Object getTarget(MethodInvocation methodInvocatio开发者_JAVA技巧n) 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);
}
Isn't it just methodInvocation.getThis()
?
精彩评论