Question

Comment puis-je obtenir un objet cible dans mon intercepteur?

   bindInterceptor(subclassesOf(A.class), any(), new MethodInterceptor() {
        @Override
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            A a = getTarget();    //how?
            return methodInvocation.proceed();
        }
    });

Mettre à jourEn fait, il existe une solution basée sur la réflexion, mais il espère qu'il y a d'autres 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);
}
Était-ce utile?

La solution

N'est-ce pas juste methodInvocation.getThis()?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top