English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
These days, after seeing the proxy part of Hibernate, the first reaction was that the underlying technology used reflection to generate proxy classes for user entities. Later, it occurred to me that reflection has no ability to generate new classes, and accordingly, I found Javassist (download address).
Most of the tutorials found online are explanations of the Javassist API, but in the end, there is often no loading process. When the author imitated these tutorials to load classes, the loaded results were all the original classes, and there was no content of bytecode modification.
After a series of explorations, the author found that most of the tutorials online use the parameterless overload of writeFile to save bytecode in the final step. Upon examining its function structure, it was discovered that there is also a String type overload. Since in Eclipse, the root location for storing bytecode is not '.\\' but '.\\bin', the other overload of writeFile is likely to be the parameter specifying the root location for bytecode. After making some changes, it turned out to be true.
The following will demonstrate the sharing of the code:
This is the structure of the author's project:
Editable.java: package com.thrblock.javassist; public class Editable {}} public void showInfo(){ System.out.println("InfoDefault!"); } }
Main.java: package com.thrblock.javassist; import java.io.IOException; import javassist.CannotCompileException; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.CtNewMethod; import javassist.NotFoundException; public class Main { public static void main(String[] args) { ClassPool pool = ClassPool.getDefault(); try{}} pool.insertClassPath(".\\bin"); //Set the root path. (The root path set here is obviously not used by writeFile) CtClass cc = pool.makeClass("com.thrblock.javassist.EditableChanged"); //To simulate the Hibernate proxy pattern, we create a new class cc.setSuperclass(pool.get("com.thrblock.javassist.Editable")); //Set its superclass CtMethod cm = CtNewMethod.make("public void showInfo(){super.showInfo();System.out.println("CustomInsertHAHA!");}", cc); //Append a method, note that it overrides the method in the superclass. cc.addMethod(cm); cc.writeFile(".\\bin"); //This is quite important; the result with no parameters is not saved to the root path of the eclipse bytecode. } catch (NotFoundException | CannotCompileException | IOException e) { e.printStackTrace(); } try{}} Class<?> cl = Class.forName("com.thrblock.javassist.EditableChanged"); //Load our new class Editable ed = (Editable) cl.newInstance(); //Since it inherits from the Editable class, it is the same as the load in Hibernate. ed.showInfo(); //Call method. } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } } }
Print result:
InfoDefault!
CustomInsertHAHA!
Other precautions:
Since we have generated a class, if the class name is the same as the original class name, it will overwrite the class file. However, if the class has been loaded into the JVM before the modification, the modified part will not take effect, and the JVM must be restarted.
Summary
This is the full content of the code analysis of the correct usage method of Javassist under Eclipse, hoping it will be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave a message if there are any shortcomings. Thank you for your support to this site!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)