如何在拦截器中获取目标对象?

   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()吗?

关于java - 在MethodInterceptor中获取目标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6001305/

10-09 21:22