Dynamic Injection of code using bytecode Processing Framework Javassist

Source: Internet
Author: User
Tags setf

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

  1. Package example sist. sample;
  2. Public abstract class testbean {
  3. Public string field;
  4. Public abstract string getm ();
  5. Public abstract void SETF (string F );
  6. Public String getf (){
  7. Return this. field;
  8. }
  9. }

Testbytecode. Java

  1. Package example sist. sample;
  2. Import Javassist. classpool;
  3. Import Javassist. ctclass;
  4. Import ipvsist. ctconstructor;
  5. Import ipvsist. ctmethod;
  6. Public class testbytecode {
  7. Public static void main (string [] ARGs) throws exception {
  8. Classpool pool = classpool. getdefault ();
  9. Ctclass Pt = pool. makeclass ("ASDF", pool. Get ("Javassist. sample. testbean "));
  10. Ctmethod Method1 = new ctmethod (pool. Get ("Java. Lang. String"), "getm", null, pt );
  11. Method1.setbody ("{return/" /";}");
  12. PT. addmethod (Method1 );
  13. Ctconstructor cc = new ctconstructor (null, pt );
  14. Cc. setbody ("This. Field =/" why? /";");
  15. PT. addconstructor (CC );
  16. Ctmethod method2 = new ctmethod (ctclass. voidtype, "SETF ",
  17. New ctclass [] {pool. Get ("Java. Lang. String")}, pt );
  18. Method2.setbody ("{This. Field = $1 ;}");
  19. PT. addmethod (method2 );
  20. Class <?> C = pt. toclass ();
  21. Testbean bean = (testbean) C. newinstance ();
  22. System. Out. println (bean. getm ());
  23. System. Out. println (bean. getf ());
  24. Bean. SETF ("SETF ");
  25. System. Out. println (bean. getf ());
  26. }
  27. }

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

  1. Package example sist. Demo;
  2. Public Class {
  3. Public void method (){
  4. For (INT I = 0; I <1000000; I ++ ){
  5. }
  6. System. Out. println ("Method1 ");
  7. }
  8. }

Export sisttest. Java

  1. Package example sist. Demo;
  2. Import Javassist. classpool;
  3. Import Javassist. ctclass;
  4. Import ipvsist. ctmethod;
  5. Import ipvsist. ctnewmethod;
  6. Public class implements sisttest {
  7. Public static void main (string [] ARGs) throws exception {
  8. // Used to obtain the bytecode class. It must be in the current classpath and use the full name
  9. Ctclass = classpool. getdefault (). Get ("Javassist. Demo. ");
  10. // Method name to be modified
  11. String mname = "method ";
  12. Ctmethod mold = ctclass. getdeclaredmethod (mname );
  13. // Modify the original method name
  14. String nname = mname + "$ impl ";
  15. Mold. setname (nname );
  16. // Create a new method and copy the original method
  17. Ctmethod mnew = ctnewmethod. Copy (mold, mname, ctclass, null );
  18. // Main injection code
  19. Stringbuffer body = new stringbuffer ();
  20. Body. append ("{/nlong start = system. currenttimemillis ();/N ");
  21. // Call the original code, similar to method (); ($) indicates all parameters
  22. Body. append (nname + "($);/N ");
  23. Body. append ("system. Out. println (/" call to method "+ mname
  24. + "Took/" +/N (system. currenttimemillis ()-Start) +"
  25. + "/" Ms./");/N ");
  26. Body. append ("}");
  27. // Replace the New Method
  28. Mnew. setbody (body. tostring ());
  29. // Add a new method
  30. Ctclass. addmethod (mnew );
  31. // The class has been changed. You cannot use a A = new A (); because the same classloader cannot load the same class twice.
  32. A A = (a) ctclass. toclass (). newinstance ();
  33. A. Method ();
  34. }
  35. }

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.