Asm-3.3.1.jar Explanation (Turn)

Source: Internet
Author: User

Java bytecode manipulation framework. It can dynamically generate stub classes or other proxy classes directly in binary form, or dynamically modify classes at load time. ASM offers a toolkit similar to Bcel and SERP, but is designed to be smaller and faster, which makes it suitable for real-time code insertion.
. net/liyangbing315/article/details/5472862
You can use ASM to dynamically manipulate class

We knowJavais a static language, andPythonRubyis a dynamic language, once the Java program is written.It is difficult to change the behavior of a class at run time, while Python, Ruby can.
But based on the bytecode level we can do some hands and feet to make the Java program more flexible and magic,asm is such a widely used open source library.

ASM is a Java bytecode manipulation framework. It can be used to dynamically generatestubs classes or other proxy classes,
Directly in binary form, or to dynamically modify classes at load time, i.e., justbefore they is loaded into the Java
Virtual Machine.

ASM accomplishes the same functionality as BCEL and SERP , but ASM
Only more than 30 K, and then the two are 350k and 150k respectively. Apache is really getting too angry.

Let's look at a simple example of ASM Helloworld.java, which generates a example class and a Main method, and the main method prints "Hello world!" Statement:

Java code

  1. import Java.io.FileOutputStream;
  2. import Java.io.PrintStream;
  3. import Org.objectweb.asm.ClassWriter;
  4. import org.objectweb.asm.MethodVisitor;
  5. import org.objectweb.asm.Opcodes;
  6. import Org.objectweb.asm.Type;
  7. import Org.objectweb.asm.commons.GeneratorAdapter;
  8. import Org.objectweb.asm.commons.Method;
  9. public class Helloworld extends ClassLoader implements opcodes {
  10. public static void main (final String args[]) throws Exception {
  11. //Creates a classwriter for the Example public class,
  12. //which inherits from Object
  13. Classwriter CW = new classwriter (0);
  14. Cw.visit (V1_1, Acc_public, "Example", null, "Java/lang/object", null);
  15. Methodvisitor MW = Cw.visitmethod (Acc_public, "<init>", "() V", null,
  16. null);
  17. MW.VISITVARINSN (Aload, 0);
  18. MW.VISITMETHODINSN (invokespecial, "Java/lang/object", "<init>", "() V");
  19. MW.VISITINSN (RETURN);
  20. Mw.visitmaxs (1, 1);
  21. Mw.visitend ();
  22. MW = Cw.visitmethod (Acc_public + acc_static, "main",
  23. "([ljava/lang/string;) V", null, null);
  24. MW.VISITFIELDINSN (getstatic, "Java/lang/system", "out" ,
  25. "Ljava/io/printstream;");
  26. MW.VISITLDCINSN ("Hello world!");
  27. MW.VISITMETHODINSN (invokevirtual, "Java/io/printstream", "println" ,
  28. "(ljava/lang/string;) V");
  29. MW.VISITINSN (RETURN);
  30. Mw.visitmaxs (2, 2);
  31. Mw.visitend ();
  32. byte [] Code = Cw.tobytearray ();
  33. FileOutputStream fos = new fileoutputstream ("Example.class");
  34. Fos.write (code);
  35. Fos.close ();
  36. Helloworld loader = new Helloworld ();
  37. Class ExampleClass = Loader
  38. . DefineClass ("Example", Code, 0, code.length);
  39. Exampleclass.getmethods () [0].invoke (null, new object[] { null });
  40. // ------------------------------------------------------------------------   
  41. //Same example with a generatoradapter (more convenient but slower)
  42. // ------------------------------------------------------------------------   
  43. CW = New classwriter (CLASSWRITER.COMPUTE_MAXS);
  44. Cw.visit (V1_1, Acc_public, "Example", null, "Java/lang/object", null);
  45. Method m = Method.getmethod ("void <init> ()");
  46. Generatoradapter mg = new generatoradapter (Acc_public, M, null, null,
  47. CW);
  48. Mg.loadthis ();
  49. Mg.invokeconstructor (Type.GetType (Object). Class), M);
  50. Mg.returnvalue ();
  51. Mg.endmethod ();
  52. m = Method.getmethod ("void Main (string[])");
  53. MG = new generatoradapter (Acc_public + acc_static, m, null, null, CW);
  54. Mg.getstatic (Type.GetType (System. Class), "Out", Type
  55. . GetType (PrintStream. Class));
  56. Mg.push ("Hello world!");
  57. Mg.invokevirtual (Type.GetType (printstream. Class), Method
  58. . GetMethod ("void println (String)"));
  59. Mg.returnvalue ();
  60. Mg.endmethod ();
  61. Cw.visitend ();
  62. Code = Cw.tobytearray ();
  63. Loader = new Helloworld ();
  64. ExampleClass = Loader.defineclass ("Example", Code, 0, code.length);
  65. Exampleclass.getmethods () [0].invoke (null, new object[] { null });
  66. }
  67. }


We see examples above that use ASM methodvisitor and Generatoradapter two ways to dynamically generate example classes and invoke print statements.

Asm-3.3.1.jar Explanation (Turn)

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.