/*** Get target object *@paramProxy Object *@return * @throwsException*/ Public StaticObject Gettarget (Object proxy)throwsException {if(!aoputils.isaopproxy (proxy)) { returnProxy//not a proxy object} if(Aoputils.isjdkdynamicproxy (proxy)) {returngetjdkdynamicproxytargetobject (proxy);} Else{//Cglibreturngetcglibproxytargetobject (proxy);} } Private StaticObject Getcglibproxytargetobject (Object proxy)throwsException {Field h= Proxy.getclass (). Getdeclaredfield ("Cglib$callback_0"); 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 ();returnTarget;} Private StaticObject Getjdkdynamicproxytargetobject (Object proxy)throwsException {Field h= Proxy.getclass (). Getsuperclass (). Getdeclaredfield ("H"); 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 ();returnTarget;}
Gets a real instance of the spring proxy object that can reflect private methods for easy testing