开发者

Get Target in MethodInterceptor

开发者 https://www.devze.com 2023-03-06 08:53 出处:网络
How I can get Target object in my interceptor? bindInterceptor(subclassesOf(A.class), any(), new MethodInterceptor() {

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 methodInvocatio开发者_JAVA技巧n) 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);
}


Isn't it just methodInvocation.getThis()?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号