直接上代码
final static String AOP_JAVA__TARGET_FIELD = "h";
final static String AOP_CGLIB_TARGET_FIELD = "CGLIB$CALLBACK_0";
###jdk代理
private static Object getJdkDynamicProxyTargetObject(Object proxy) throws Exception {
Field h = proxy.getClass().getSuperclass()
.getDeclaredField(AOP_JAVA__TARGET_FIELD);
h.setAccessible(true);
AopProxy aopProxy = (AopProxy) h.get(proxy);
Field advised = aopProxy.getClass().getDeclaredField("advised");
advised.setAccessible(true);
Object target = ((AdvisedSupport) advised.get(aopProxy))
.getTargetSource().getTarget();
return target;
}
###cglib代理
private static Object getCglibProxyTargetObject(Object proxy) throws Exception {
Field h = proxy.getClass().getDeclaredField(AOP_CGLIB_TARGET_FIELD);
h.setAccessible(true);
Object dynamicAdvisedInterceptor = h.get(proxy);
Field advised = dynamicAdvisedInterceptor.getClass()
.getDeclaredField("advised");
advised.setAccessible(true);
Object target = ((AdvisedSupport)advised.get(dynamicAdvisedInterceptor))
.getTargetSource().getTarget();
return target;
}