Reprinted from The Fall of the column http://blog.csdn.net/quhongwei_zhanqiu/article/details/41597219
Javassistproxyfactory: Using bytecode technology to create objects
public <T> T getproxy (invoker<t> invoker,class<?>[] interfaces) { return (T) Proxy.getproxy (interfaces). newinstance (New Invokerinvocationhandler (Invoker));}
Like the JDK generation proxy, the proxy class here is not the class that comes with the build proxy object in the JDK:
Com.alibaba.dubbo.common.bytecode.Proxy.
This proxy class is written by Dubbo itself, using the Javassist tool to generate proxy code with the interface to be proxied.
Get Invoker Object
Public<T> invoker<t> Getinvoker (T proxy, class<t>type, url url) { FinalWrapper Wrapper = Wrapper.getwrapper (Proxy.getclass (). GetName (). IndexOf (' $ ') < 0?proxy.getclass (): type); return NewAbstractproxyinvoker<t>(proxy, type, URL) {protectedObject Doinvoke (T proxy, String methodName, Class<?>[] parametertypes, object[] arguments)throwsThrowable {returnWrapper.invokemethod (proxy, MethodName, parametertypes, arguments); } }; }
Creates a wrapper object for an incoming proxy object based on its class information wrapper
Returns an instance of the Invoker object that can be invoker based on the name of the method contained in the incoming invocation object.
Method parameter to call the proxy object to return the result of the call
Com.alibaba.dubbo.common.bytecode.Proxy tool class for generating proxy objects
1. Iterate through all the entry interfaces to; Split the connection, with it as key to map for cache lookup If there is, the proxy object has been created to return
2. Using Atomiclong object self-increment gets a long array to be used as the suffix of the production class to prevent conflicts
3. The traversal interface gets all the defined methods, added to a set set<string> worked, used to weigh,
Gets the index of method y should be in the methods array subscript IX
Gets the parameter type of the method and the return type
Build method Body return ret= Handler.invoke (this, Methods[ix], args);
The method call here is actually delegated to the Invokerinvocationhandler instance object, to invoke the real instance method into the methods array .
4. Create a proxy instance object proxyinstance
The class name is Pkg + ". POxy" +id = package name + ". POxy" + self-increment value
Add static field method[] methods;
Add an Instance object Invokerinvocationhandler Hanler
The Add constructor parameter is Invokerinvocationhandler
Add parameterless constructor
Generate corresponding bytecode using the tool class Classgenerator
5. Create a proxy object whose newinstance (handler) method is used to create a proxy based on our interface
Proxy object name proxy + ID
Inherit from proxy, so to implement the Newinstance method
Add a default constructor
Implementation method newinstance code, new PCN (Hadler) Here PCN is the previously generated proxy object class name
Use the tool class Classgenerator to generate bytecode and instantiate objects to return
5.Dubbo Principle Analysis-agent's javassist bytecode technology generation agent (GO)