Analysis and extension of Java Dynamic Proxy Mechanism — turn

Introduction < P style =" margin top: 5px! important; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding: 6px 0px; border: 0px; outline: 0px; font-size: 1.166em ! important; vertical-align: baseline; font-family: Arial,sans-serif; color: #222222; line-height: 1.5em; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; - webkit-text-stroke-width: 0px; background-color: #ffffff; "> with the emergence of Java dynamic proxy mechanism, Java developers do not need to write proxy classes manually. They can dynamically obtain proxy classes by simply specifying a group of interfaces and delegate class objects. The proxy class will be responsible for dispatching all method calls to the delegate object for reflection and execution. In the process of dispatching and execution, developers can also adjust the delegate class objects and their functions as needed , this is a very flexible and flexible agent framework. By reading this article, readers will have a deeper understanding of Java dynamic proxy mechanism. Firstly, starting from the running mechanism and characteristics of Java Dynamic agent, this paper analyzes its code and deduces the internal implementation of dynamically generated classes.

Method 1: this method is used to obtain the calling processor static invocationhandler getinvocationhandler (object proxy) associated with the specified proxy object. / / method 2: this method is used to obtain the class object static class getproxyclass (classloader loader, class [] interfaces) associated with the dynamic proxy class of the specified class loader and a group of interfaces //Method 3: this method is used to judge whether the specified class object is a dynamic proxy class static Boolean isproxyclass (class CL) / / method 4: this method is used to generate a dynamic proxy class instance static object newproxyinstance (classloader, class [] interfaces, invocationhandler h) for the specified class loader, a group of interfaces and the calling processor

//Dynamically create a class object of proxy class through proxy for a group of interfaces including interface interface. Class clazz = proxy getProxyClass(classLoader,new Class[] { Interface.class,... }); // Get the constructor object constructor constructor = clazz from the generated class object by reflection getConstructor(new Class[] { InvocationHandler.class }); // Create a dynamic proxy class instance through the constructor object interface proxy = (Interface) constructor newInstance(new Object[] { handler });
//Create a dynamic proxy class instance directly through proxy. Interface proxy = (Interface) proxy newProxyInstance( classLoader,new Class[] { Interface.class },handler );
//Tag: used to mark that a dynamic proxy class is being created. Private static object pendinggenerationmarker = new object()// Synchronization table: records the dynamic proxy class types that have been created. It is mainly judged by the isproxyclass method. Private static map proxyclasses = collections synchronizedMap(new WeakHashMap()); // The associated calling processor refers to the protected invocationhandler h;
//Since the constructor is never called directly inside the proxy, protected means that only subclasses can call protected proxy (invocationhandler h) {this. H = h;}
[] interfaces, invocationhandler h) throws illegalargumentexception {/ / check that h is not empty, or throw an exception
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>