2

インターセプターでTargetオブジェクトを取得するにはどうすればよいですか?

   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);
}
4

1 に答える 1

3

ただじゃないmethodInvocation.getThis()ですか?

于 2011-05-14T13:04:27.350 に答える