In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to deeply analyze the dynamic proxy principle in the Java reflection mechanism. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
For example code of dynamic proxy class principle, see "Analysis of Java reflection Mechanism: a brief talk about dynamic proxy")
A) understand the dynamic agent example process above
B) Source code analysis of proxy interface implementation class
Let's take a look at the source code of the proxy implementation class ($Proxy0) and the flow of the entire dynamic proxy.
The code generated by $Proxy0 is as follows:
Import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.lang.reflect.UndeclaredThrowableException; public final class $Proxy0 extends Proxy implements Manager {private static Method M1; private static Method m0; private static Method M3; private static Method m2 Static {try {M1 = Class.forName ("java.lang.Object"). GetMethod ("equals", new Class [] {Class.forName ("java.lang.Object")}); M0 = Class.forName ("java.lang.Object"). GetMethod ("hashCode", new Class [0]); m3 = Class.forName ("com.ml.test.Manager"). GetMethod ("test", new Class [0]) M2 = Class.forName ("java.lang.Object"). GetMethod ("toString", new Class [0]);} catch (NoSuchMethodException nosuchmethodexception) {throw new NoSuchMethodError (nosuchmethodexception.getMessage ());} catch (ClassNotFoundException classnotfoundexception) {throw new NoClassDefFoundError (classnotfoundexception.getMessage ());}} public $Proxy0 (InvocationHandler invocationhandler) {super (invocationhandler) } @ Override public final boolean equals (Object obj) {try {return ((Boolean) super.h.invoke (this, M1, new Object [] {obj})) .booleanValue ();} catch (Throwable throwable) {throw new UndeclaredThrowableException (throwable);}} @ Override public final int hashCode () {try {return ((Integer) super.h.invoke (this, M0, null)). IntValue () } catch (Throwable throwable) {throw new UndeclaredThrowableException (throwable);}} public final void test () {try {super.h.invoke (this, m3, null); return;} catch (Error e) {} catch (Throwable throwable) {throw new UndeclaredThrowableException (throwable) } @ Override public final String toString () {try {return (String) super.h.invoke (this, m2, null);} catch (Throwable throwable) {throw new UndeclaredThrowableException (throwable);}
What comes to mind is that the proxy interface implementation class implements the interface of the business class (that is, the UserManager interface in the example) and inherits the base class Proxy class
Then there is the constructor, which passes BusinessHandler in the constructor, and then $Proxy0 calls the constructor of the parent class Proxy to assign a value to h (see the constructor of Proxy here)
Then we see that this class overrides the Equals, hashCode, and toString methods of the Proxy class, and implements the methods of the business class interface (that is, the test method of UserManager). The specific rewriting and implementation are all using the super.h.invoke (that is, Proxy.h.invoke) method.
After a brief analysis of the proxy interface implementation class, let's take a look at how the dynamic proxy is implemented as a whole:
First, the client initializes the BusinessHandler class, calling the newProxyInstance (new UserManagerImpl ()) method of the class to initialize the above proxy interface implementation class
Next, the proxy interface implementation class passes the BusinessHandler through the constructor (that is, the this in the code) and assigns a value to h through the constructor of Proxy
Then the client can instantiate the proxy interface implementation class $Proxy0, which we cast to the business implementation interface (UserManager) type. (why cast? it's very interesting here. If it's not cast, an error will be reported. It's easy to explain here, because the current environment simply doesn't know that the proxy interface implementation class $Proxy0 inherits both Proxy and business implementation interface UserManager. But it is possible to cast to UserManager because there is UserManager in the current environment. This is the power of reflection. You can call any class dynamically at run time and use the details of that class. )
Later, when we call the test method, we actually call the test method in $Proxy0, which is implemented through the invoke method of Proxy.h (that is, the BusinessHandler.invoke method is called)
After that, the invoke method of Method is called (in this case the arguments are this, and args).
This calls the corresponding method of UserManagerImpl, which is then returned to the client.
This completes the entire invocation relationship.
Reflection, programmer's happiness
After an in-depth analysis of the dynamic proxy in the previous article, it is still very interesting to think about it, and the most fundamental mechanism is the reflection mechanism, which dynamically instantiates any class at runtime and invokes its details. Now looking back at the example of dynamic agent, we find that the most important thing here is in Proxy.newProxyInstance (..) When the method is executed, the memory bytecode of $Proxy0 is generated, when we have the memory bytecode, our reflection will be very powerful, so that we will have a series of invocation relationships.
Through the analysis of the reflection mechanism and the analysis of dynamic proxy examples, we find that programming is so interesting that we cannot extricate ourselves from it.
The above is how to deeply analyze the dynamic agent principle in the Java reflection mechanism. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.