Official homepage: http://www.csg.is.titech.ac.jp /~ CHBA/Javassist/
Javassist,CglibSome code enhancement tools, such as Java bytecode enhancement at runtime. Although the speed is a little slower, it brings about concise code. Today, we use Javassist for code enhancement.
Code:
Testbean. Java
- Package example sist. sample;
- Public abstract class testbean {
- Public string field;
- Public abstract string getm ();
- Public abstract void SETF (string F );
- Public String getf (){
- Return this. field;
- }
- }
Testbytecode. Java
- Package example sist. sample;
- Import Javassist. classpool;
- Import Javassist. ctclass;
- Import ipvsist. ctconstructor;
- Import ipvsist. ctmethod;
- Public class testbytecode {
- Public static void main (string [] ARGs) throws exception {
- Classpool pool = classpool. getdefault ();
- Ctclass Pt = pool. makeclass ("ASDF", pool. Get ("Javassist. sample. testbean "));
- Ctmethod Method1 = new ctmethod (pool. Get ("Java. Lang. String"), "getm", null, pt );
- Method1.setbody ("{return/" /";}");
- PT. addmethod (Method1 );
- Ctconstructor cc = new ctconstructor (null, pt );
- Cc. setbody ("This. Field =/" why? /";");
- PT. addconstructor (CC );
- Ctmethod method2 = new ctmethod (ctclass. voidtype, "SETF ",
- New ctclass [] {pool. Get ("Java. Lang. String")}, pt );
- Method2.setbody ("{This. Field = $1 ;}");
- PT. addmethod (method2 );
- Class <?> C = pt. toclass ();
- Testbean bean = (testbean) C. newinstance ();
- System. Out. println (bean. getm ());
- System. Out. println (bean. getf ());
- Bean. SETF ("SETF ");
- System. Out. println (bean. getf ());
- }
- }
Output:
Hi!
Why?
SETF
We can see that dynamic construction is implemented and abstract functions are implemented dynamically. In code automation projects, bytecode enhancement can greatly improve the quality of code and reduce the number of code.
Let's look at another example:
A. Java
- Package example sist. Demo;
- Public Class {
- Public void method (){
- For (INT I = 0; I <1000000; I ++ ){
- }
- System. Out. println ("Method1 ");
- }
- }
Export sisttest. Java
- Package example sist. Demo;
- Import Javassist. classpool;
- Import Javassist. ctclass;
- Import ipvsist. ctmethod;
- Import ipvsist. ctnewmethod;
- Public class implements sisttest {
- Public static void main (string [] ARGs) throws exception {
- // Used to obtain the bytecode class. It must be in the current classpath and use the full name
- Ctclass = classpool. getdefault (). Get ("Javassist. Demo. ");
- // Method name to be modified
- String mname = "method ";
- Ctmethod mold = ctclass. getdeclaredmethod (mname );
- // Modify the original method name
- String nname = mname + "$ impl ";
- Mold. setname (nname );
- // Create a new method and copy the original method
- Ctmethod mnew = ctnewmethod. Copy (mold, mname, ctclass, null );
- // Main injection code
- Stringbuffer body = new stringbuffer ();
- Body. append ("{/nlong start = system. currenttimemillis ();/N ");
- // Call the original code, similar to method (); ($) indicates all parameters
- Body. append (nname + "($);/N ");
- Body. append ("system. Out. println (/" call to method "+ mname
- + "Took/" +/N (system. currenttimemillis ()-Start) +"
- + "/" Ms./");/N ");
- Body. append ("}");
- // Replace the New Method
- Mnew. setbody (body. tostring ());
- // Add a new method
- Ctclass. addmethod (mnew );
- // The class has been changed. You cannot use a A = new A (); because the same classloader cannot load the same class twice.
- A A = (a) ctclass. toclass (). newinstance ();
- A. Method ();
- }
- }
MATERIALS:
Http://tsaijun.spaces.live.com/blog/cns! C9f557e0fd3838c1! 121. Entry
Http://www.blogjava.net/zyl/archive/2007/02/10/99171.html
Http://www.ibm.com/developerworks/cn/java/j-dyn0916/