310a436c
timfruit
完成menu单测
|
1
2
|
package cn.iocoder.dashboard.util;
|
d2e773e8
timfruit
根据艿艿提示修改
|
3
|
import cn.hutool.core.bean.BeanUtil;
|
310a436c
timfruit
完成menu单测
|
4
5
6
7
8
9
10
11
12
13
14
|
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AopProxy;
import org.springframework.aop.support.AopUtils;
import java.lang.reflect.Field;
/**
* http://www.bubuko.com/infodetail-3471885.html
*/
public class AopTargetUtils {
|
310a436c
timfruit
完成menu单测
|
15
16
17
18
19
20
21
22
|
/**
* 获取 目标对象
*
* @param proxy 代理对象
* @return
* @throws Exception
*/
public static Object getTarget(Object proxy) throws Exception {
|
310a436c
timfruit
完成menu单测
|
23
24
25
|
if (!AopUtils.isAopProxy(proxy)) {
return proxy; //不是代理对象
}
|
310a436c
timfruit
完成menu单测
|
26
27
28
29
30
|
if (AopUtils.isJdkDynamicProxy(proxy)) {
return getJdkDynamicProxyTargetObject(proxy);
} else { //cglib
return getCglibProxyTargetObject(proxy);
}
|
310a436c
timfruit
完成menu单测
|
31
32
|
}
|
310a436c
timfruit
完成menu单测
|
33
|
private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
|
d2e773e8
timfruit
根据艿艿提示修改
|
34
35
36
|
Object dynamicAdvisedInterceptor = BeanUtil.getFieldValue(proxy, "CGLIB$CALLBACK_0");
AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(dynamicAdvisedInterceptor, "advised");
Object target = advisedSupport.getTargetSource().getTarget();
|
310a436c
timfruit
完成menu单测
|
37
38
39
40
|
return target;
}
private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
|
d2e773e8
timfruit
根据艿艿提示修改
|
41
42
43
|
AopProxy aopProxy = (AopProxy) BeanUtil.getFieldValue(proxy, "h");
AdvisedSupport advisedSupport = (AdvisedSupport) BeanUtil.getFieldValue(aopProxy, "advised");
Object target = advisedSupport.getTargetSource().getTarget();
|
310a436c
timfruit
完成menu单测
|
44
45
46
47
|
return target;
}
}
|