2

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 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);
}
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132

1 Answers1

3

Isn't it just methodInvocation.getThis()?

Pace
  • 41,875
  • 13
  • 113
  • 156
  • Yeap. Thanks a lot. There was some bug in my interceptors and `getThis()` used to return some wrong object. Now it works well. Thanks. – Stan Kurilin May 14 '11 at 13:19