Question

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);
}
Was it helpful?

Solution

Isn't it just methodInvocation.getThis()?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top