1 Private StaticMap<short, map<byte, method>> receive_method_info =NewHashmap<>();2 3 Public Static<T>T registerreceiveproxy (Object obj) {4Class<?> target =Obj.getclass ();5 if(Target.isinterface ()) {6 Throw NewRuntimeException ("Class is Interface:" +target);7 }8Qmodel Modelanno = Reflectutil.getanno (target, Qmodel.class);9String proxyclassname = target.getcanonicalname () + "$ $receive $$";TenClasspool Classpool =Javassisthepler.classpool; OneCtclass Ctclass =Classpool.makeclass (proxyclassname); A - Try { - //set interface, inherit target theCtclass[] Interfaces =NewCtclass[1]; -Interfaces[0] = Classpool.get (irpcreceive.class. GetName ()); - ctclass.setinterfaces (interfaces); - Ctclass.setsuperclass (Javassisthepler.getctclass (target)); + { - //Add this field + FinalString Ctxname =target.getname (); ACtfield Ctfield =NewCtfield (Classpool.get (Ctxname), "_this", ctclass); atCtfield.setmodifiers (Modifier.private |modifier.final); - //add JSON ignore anno -Ctfield.getfieldinfo2 (). AddAttribute (Javassisthepler.addanno (Jsonignore.class, Ctclass)); - Ctclass.addfield (Ctfield); - } - inMap<byte, method> methods =NewHashmap<>(); - Receive_method_info.put (Modelanno.value (), methods); to + //Build Agent Method -Reflectutil.foreachmethods (Target, (method) { theQcommond Commond = method.getannotation (Qcommond.class); * if(Commond = =NULL) { $ return;Panax Notoginseng } - Methods.put (Commond.value (), method); theString Resulttype = ""; + if(void.class!=Method.getreturntype ()) { AResulttype = "return ($r)"; the } + FinalString BODY = "{" + Resulttype + "_this." + method.getname () + "($$);}"; - Javassisthepler.addmethod (Ctclass, method, body); $ }); $ - //generate Receive Method - { the FinalString BODY = "{return ($r)" + qrpcfactory.class. GetName () + ". Proxyreceive (_this,$2, (short) + modelanno.value () +", (byte) $);} "; - Javassisthepler.addmethod (Ctclass, Receive_method, body);Wuyi } the - //Add Construction method New Xxproxy (XX) WuCtconstructor Ctconstructor =NewCtconstructor (Javassisthepler.toctclassarray (target), ctclass); -Ctconstructor.setbody ("{this._this = $;}"); About ctconstructor.setmodifiers (modifier.public); $ Ctclass.addconstructor (ctconstructor); -Class<?> Newclass =Ctclass.toclass (); -Constructor<t> Constructor = (constructor<t>) Newclass.getconstructor (target); -Constructor.setaccessible (true); A Ctclass.detach (); +Object ret =constructor.newinstance (obj); the Receive.put (Modelanno.value (), (irpcreceive) ret); - return(T) ret; $}Catch(Exception e) { the Throw NewRuntimeException (e); the } the } the - //because the parameter type accessed by the Javassist $$ expression is Object-not get the target type, it can only be handled with Invoke in Public StaticObject Proxyreceive (Object target, object[] args, ShortModelbyteCommondindex) { theMap<byte, method> methods =Receive_method_info.get (model); the Try { About returnMethods.get (Commondindex). Invoke (target, args); the}Catch(Exception e) { the Throw NewQrpcexception (Qcode.enhance_error_rpc_nofind_model, "proxyreceive", e); the } + } -}
@Test Public voidtestreceive () {Testobject proxy= Qrpcfactory.registerreceiveproxy (NewTestobjectimpl ()); PROXY.A (1, "B"); Proxy.setage (30); Qresult<Integer> ret =Proxy.getage (); System.out.println (Ret.getresult ()); Object[] args=NewObject[1]; args[0] =18; ((irpcreceive) proxy). Receive ( (byte) 2, args); RET=Proxy.getage (); System.out.println (Ret.getresult ()); args[0] =NewTestObject1 (); ((irpcreceive) proxy). Receive ( (byte) 4, args); } @Test Public voidTestobjectargs () {Qrpcfactory.registerreceiveproxy (NewTestobjectimpl ()); Irpcreceive obj= Qrpcfactory.loadreceiveproxy (( Short) 1); intA=30; Integer b= 30; DoubleC=1d; List<Integer> d =NewArraylist<>(); Integer[] e=NewInteger[0]; Object[] args=NewObject[5]; args[0] =A; args[1] =b; args[2] =C; args[3] =D; args[4] =e; Obj.receive ((byte) 5, args); }
An error occurred at compile time because the type of the parameter accessed by the Javassist $$ expression is Object-not get the target type at the time of actual development
Type ' Java/lang/object ' (current frame, stack[1]) was not assignable to integer
So you can only use Invoke to handle
[Weave message Frame] [Java Core Technology] Dynamic agent application 8-irpcreceive implementation