In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to view dynamic proxy class content in jvm by java". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When looking at the dubbo source code, I found that the dubbo call process designed a lot of dynamic proxy class technology, but these dynamic proxy classes are running in memory, do not generate class files, do not understand the contents of these dynamic proxy classes, do not know what his calling process is?
The following example is my debugging of the mybatis framework. We all know that the mybatis framework needs to write mapper.xml and mapper.java interfaces to operate the database. The persistence layer implementation class can be directly injected into the mapper.java class and call methods to operate the database. How does mybatis wrap the mapper.java interface class for calling?
Main tools: jvm's HSDB tool
Operating system: windows system
Jdk version: jdk1.8
1. Breakpoint
In dubug mode, the upper and lower breakpoints of the proxy object. The main purpose of this step is to obtain the class name of the proxy class. You can print it out directly. The breakpoint operation is to prevent the proxy class from changing.
System.out.println ("* * &" + userMapper.getClass () .getName ())
2. View the process ID
The jps command looks at the process ID value of the current program and notes that if the program is restarted, the process id will change.
What if the process id is determined? Very simply, when the project is not started, jps will take a look. After the project is started, jps will see that the id of that process is the id value we need.
My current program startup process is 38916.
3. Start the HSDB tool in jvm
Hsdb is a tool that comes with jvm, so the premise is to install jdk and configure the environment.
Java-classpath "% JAVA_HOME%/lib/sa-jdi.jar" sun.jvm.hotspot.HSDB4, configuration HSDB
Click [File]-- > [Attach to hotspot process]-- > enter process id-- > [ok]
5. Get the class name of the proxy object
The class name of the proxy class corresponding to my breakpoint userMapper is $Proxy18@4954. It's obvious in the picture.
6. View class in JVM according to the proxy class name
Click [Tools]-- > [Class Browser]-- > enter the proxy class name
Here the userMapper object shows that the proxy class name is $Proxy18, and one is found below.
7. Generate class
Click the found class, and then click [Create .class File], which will generate a class file locally.
8. Class decompilation
The default generated class file is on disk C, and my directory is in C:\ Users\ Lu\ com\ sun\ proxy\ $Proxy18.class. Take a closer look at the generation path, which generates the corresponding directory according to the package name.
The jd-gui.exe decompiler tool did not compile successfully.
I am directly copy the class file to the idea tool, under the target/class directory of a project, and let the idea tool decompile. I see the selectByNumb method, which means that this class is the proxy class I'm looking for for userMapper.
Through the result of this decompilation, we can understand:
1. Why does Proxy.newProxyInstance pass in the InvocationHandler class when it generates the proxy class, and how it is called to the invoke () method. Because the proxy object's implementation of the method calls the handler.invoke method directly.
2. Why the mapper.java interface class written by mybatis can be directly injected and called, because for each mapper.java interface class, mybatis generates a proxy object that implements this interface.
This is the end of the content of "how to view the dynamic proxy class class content in jvm by java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.