gpt4 book ai didi

java - 在 MethodInterceptor 中获取目标

转载 作者:行者123 更新时间:2023-11-30 09:49:01 25 4
gpt4 key购买 nike

如何在我的拦截器中获取 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);
}

最佳答案

不就是methodInvocation.getThis()吗?

关于java - 在 MethodInterceptor 中获取目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6001305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com