Hello everyone, I'm Glacier~~
Bytecode programming is not commonly used in actual business development (CRUD), but with the continuous development and heavy use of network programming, RPC, dynamic bytecode enhancement techniques and automated testing, and zero-intrusive APM monitoring, more and more technology requires the use of bytecode programming.
Well, we will use Javassist to dynamically generate a HelloWorld case today. The relevant program case code can be obtained from the public account: Glacier Technology , or directly from Github and Gitee.
Github:https://github.com/binghe001/bytecode
Gitee:https://gitee.com/binghe001/bytecode
development environment
- JDK 1.8
- IDEA 2018.03
- Maven 3.6.0
Maven dependencies
Add the following environment dependencies to the project's pom.xml file.
<properties>
<javassist.version>3.20.0-GA</javassist.version>
</properties>
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>
</dependencies>
Case effect
The overall case effect is actually very simple. When learning the Java language, we will print the first Hello World program on the command line. Today, when we learn Javassist bytecode programming, we also implement a HelloWorld program.
The effect of the case is to generate the following program code.
package io.binghe.bytecode.javassist.test;
public class HelloWorld {
public static void main(String[] var0) {
System.out.println("Javassist Hello World by 冰河(公众号:冰河技术)");
}
public HelloWorld() {
}
}
Look at this effect, does it look like the Java code we wrote in IDEA? Let's use Javassist to achieve it.
Case realization
This case is actually quite simple, and the source code is given directly here.
/**
* @author binghe (公众号:冰河技术)
* @version 1.0.0
* @description 测试使用Javassist生成第一个类HelloWorld
*/
public class GenerateHelloWorldClass {
/**
* 创建HelloWorld的类,并返回HelloWorld的Class实例
*/
public static Class createHelloWorld()throws Exception{
//使用默认的ClassPool
ClassPool pool = ClassPool.getDefault();
//创建一个空类
CtClass ctClass = pool.makeClass("io.binghe.bytecode.javassist.test.HelloWorld");
//添加一个main方法
CtMethod ctMethod = new CtMethod(CtClass.voidType, "main", new CtClass[]{
pool.get(String[].class.getName())}, ctClass);
//将main方法声明为public static类型
ctMethod.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
//设置方法体
ctMethod.setBody("{" +
"System.out.println(\"Javassist Hello World by 冰河(公众号:冰河技术)\");" +
"}");
ctClass.addMethod(ctMethod);
//将生成的类的class文件输出的磁盘
ctClass.writeFile();
//返回HelloWorld的Class实例
return ctClass.toClass();
}
public static void main(String[] args) throws Exception {
Class clazz = createHelloWorld();
Object obj = clazz.newInstance();
Method mainMethod = clazz.getMethod("main", new Class[]{
String[].class});
mainMethod.invoke(obj, new String[1]);
}
}
Next, let's see how Javassist generates complete bytecode based on the above code.
(1) Create a ClassPool in the createHelloWorld() method, which is essentially a CtClass object container.
(2) Call the makeClass() method of ClassPool, pass in the complete package name + class name to generate an empty class information. The full package name + class name passed in here is io.binghe.bytecode.javassist.test.HelloWorld
.
(3) Add a method to the class, and set the method's return type, method name, parameter name (input and output parameters), access modifiers, and method body. The full method body set here is as follows:
public static void main(String[] var0) {
System.out.println("Javassist Hello World by 冰河(公众号:冰河技术)");
}
(4) Although we did not show the creation of a parameterless constructor in the above code, at compile time, Javassist will automatically create a parameterless constructor of the HelloWorld class.
(5) The class information in the memory is output to the disk through the writeFile() method of CtClass, so that we can clearly see the HelloWorld class generated by Javassist through IDEA.
(6) Finally, the toClass() method of CtClass is called in the createHelloWorld() method to return the Class object.
(7) Call the createHelloWorld() method in the main() method to obtain the Class object.
(8) Instantiate the object through reflection, and call the main() method of the generated HelloWorld class through reflection.
Effect demonstration
Running the main() method of the GenerateHelloWorldClass class will io/binghe/bytecode/javassist/test
generate the HelloWorld.class file in the directory under the top-level project directory, as shown below.
When viewing the output information of IDEA, it is found that the following content will be output.
Javassist Hello World by 冰河(公众号:冰河技术)
Process finished with exit code 0
Summary
We used Javassist to implement the function of creating a HelloWorld class. Bytecode programming sounds difficult, but under the powerful API of Javassist, it is quite simple to implement.
In the next period of time, Glacier will continue to output articles about bytecode programming, let us master bytecode programming together.
Okay, let's stop here today, I'm Glacier, see you next time~~
write at the end
If you want to enter a big factory, want to get a promotion and a salary increase, or are confused about your current job, you can privately message me to communicate, I hope some of my experiences can help you~~
Recommended reading:
- " Practice brings true knowledge: Deciphering the architecture of the strongest seckill system in the entire network, not all seckills are seckills! ! 》
- " From zero to hundreds of millions of users, how do I optimize MySQL database step by step? (recommended collection)》
- " I used multi-threading to further optimize the massive data proofreading system under the billion-level traffic e-commerce business, and the performance has been improved by 200%! ! (The whole process is dry, it is recommended to collect)》
- " I used multi-threading to optimize the massive data proofreading system under the billion-level traffic e-commerce business, and the performance was directly improved by 200%! ! (The whole process is dry, it is recommended to collect)》
- " I summed up the best learning route for concurrent programming with 10 pictures! ! (recommended collection)》
- " A lock that is faster than read-write locks in high concurrency scenarios, I am completely convinced after reading it! ! (recommended collection)》
- " Summary of the most complete performance optimization of the entire network! ! (Glacier Hematemesis Arrangement, Recommended Collection)》
- " After three days of playing MyBatis, feel free to ask! ! (Glacier Hematemesis Arrangement, Recommended Collection)》
- " Advice to those who have just started working: If you want to enter a big factory, you must master these concurrent programming knowledge! Complete Learning Route! ! (recommended collection)》
- " Advice to those juniors and juniors who have just started working: If you want to enter a big factory, you must master these core skills! Complete Learning Route! ! (recommended collection)》
- " Advice to those who have just started working: The sooner you know the basics of these computers and operating systems, the better! The 10,000-character long text is too top! ! (recommended collection)》
- " I developed a national-level game suitable for all ages in three days, supports playing music, and now open the complete source code and comments (recommended collection)! ! 》
- " I am the most hard-core high-concurrency programming author on the entire network, and the most noteworthy blogger on CSDN, do you agree? (recommended collection)》
- " Five years after graduation, from a monthly salary of 3,000 to an annual salary of one million, what core skills have I mastered? (recommended collection)》
- " I hacked into the Wifi of the girl next door and found out. . . (The whole process of actual combat dry goods, it is recommended to collect) "
- " Don't try "Panda Burning Incense" lightly, no, I regret it! 》
- " Tomb-sweeping Day secretly trained "Panda Burning Incense", and my computer "dedicated" to panda! 》
- " 730,000 words bursting with new features of Java8, I don't believe you can read it! (recommended collection)》
- " What's it like to unplug your server during peak business hours? 》
- " Summary of the most complete Linux commands on the entire network! ! (The most complete in history, recommended collection) "
- " Wrote a tool in Python, perfectly cracked MySQL! ! (recommended collection)》
- Why is the SimpleDateFormat class not thread-safe? (with six solutions, recommended collection) "
- " These three new indexes in MySQL 8 have directly let MySQL take off, you don't even know it! ! (recommended collection)》
- " After finishing the Spring source code, I open sourced this distributed cache framework! ! (recommended collection)》
- "The product of the 100 million-level traffic high concurrent spike system is "oversold", just because there are these two huge pits in the JDK synchronization container used! ! (The record of stepping on the pit, it is recommended to collect) "
- " Advise those who have just started working: If you want to learn concurrent programming well, you must pay attention to the pits of these concurrent containers! ! (recommended collection)》
- "The company's reporting tool is too difficult to use. I picked up an Excel tool in three days. The operation lady said it was very easy to use, and it is now open source! ! (recommended collection)》
- " Advice to those who have just started working: If you want to enter a big factory, these core skills of concurrent programming are what you must master! ! (recommended collection)》
- " Ali Interviewer: How does the high-concurrency and high-traffic spike system correctly solve the problem of oversold inventory? (recommended collection)》
- " Redis five data types and usage scenarios summary! ! (including complete actual combat cases, it is recommended to collect)》
Okay, let's stop here today, friends, like, favorite, comment, and start walking with one click, I'm Glacier, see you in the next issue~~