package.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
  2. <html>
  3. <head>
  4. <title>The gnu.bytecode package</title>
  5. </head>
  6. <body><p>
  7. Contains classes to generate, read,
  8. write, and print Java bytecode in the form of <code>.class</code> files.
  9. <p>
  10. It is used by
  11. <a href="../kawa/">Kawa</a>
  12. to compile Scheme into bytecodes; it should be useful
  13. for other languages that need to be compiled into Java bytecodes.
  14. (An interesting exercise would be an interactive Java expression evaluator.)
  15. The classes here are relatively low-level. If you want to use them
  16. to generate bytecode from a high-level language, it would be easier to
  17. use the <a href="../../gnu/expr/package-summary.html">gnu.expr</a> package, which works
  18. at the expression level, and handles all the code-generation for you.
  19. <p>
  20. The most important class is <code>ClassType</code>.
  21. This contains information
  22. about a single class. Note that the difference between <code>ClassType</code>
  23. and <code>java.lang.Class</code> is that the latter can only represent existing
  24. classes that have been loaded into the Java VM; in contrast,
  25. <code>ClassType</code> can be used to build new classes
  26. incrementally and on the fly. A <code>ClassType</code> can also
  27. <q>wrap</q> a <code>java.lang.Class</code>, or data read in
  28. from a <code>.class</code> file.
  29. Use <code>ClassType.make</code> to refer to
  30. existing classes and <code>new ClassType</code> to refer to classes you're
  31. generating.
  32. <p>
  33. A <code>ClassType</code> has a list of <code>Field</code> objects;
  34. new ones can be added using
  35. the various <code>addField</code> methods. A <code>ClassType</code>
  36. manages a <code>ConstantPool</code>.
  37. A <code>ClassType</code> also has a list of <code>Method</code> objects;
  38. new ones can be created by the various <code>addMethod</code> objects.
  39. <p>
  40. Calling <code>Method.startCode</code> gives you a <code>CodeAttr</code> object
  41. you can use to emit bytecodes for that method.
  42. <p>
  43. Once you have finished generating a <code>ClassType</code>, you
  44. can write it to a <code>.class</code> file with
  45. the <code>writeToFile</code> method. You can also make a
  46. byte array suitable for <code>ClassLoader.defineClass</code> using the
  47. <code>writeToArray</code>
  48. method. This is useful if you want to compile and immediately load a class,
  49. without going via disk.
  50. <p>
  51. You can print out the contents of a <code>ClassType</code> in
  52. human-readable
  53. form using the class <code>ClassTypeWriter</code>. This prints a fair bit of
  54. information of the generated class, including
  55. dis-assembling the code of the methods.
  56. <p>
  57. You can also build a <code>ClassType</code> by reading it from an
  58. existing <code>.class</code>
  59. file by using a <code>ClassFileInput</code> class. This reads the constant
  60. pool, the fields, methods, superclass, and interfaces.
  61. The <code>gnu.bytecode.dump</code> class has a <code>main</code> method
  62. that prints out the information in a named class file, which you can use as
  63. a replacement for <code>javap(1)</code>.
  64. <h2>A Simple Example</h2>
  65. <p>Here's a complete example showing the basics. If this was really all you
  66. wanted to do, the code could be shorter, but you get to see more of what's
  67. available this way.
  68. <blockquote><pre>
  69. import gnu.bytecode.*;
  70. public class MetaHelloWorld {
  71. public static void main(String[] args) throws Exception {
  72. <font color="green"> // &quot;public class HelloWorld extends java.lang.Object&quot;.</font>
  73. ClassType c = new ClassType(&quot;HelloWorld&quot;);
  74. c.setSuper(&quot;java.lang.Object&quot;);
  75. c.setModifiers(Access.PUBLIC);
  76. <font color="green"> // &quot;public static int add(int, int)&quot;.</font>
  77. Method m = c.addMethod(&quot;add&quot;, &quot;(II)I&quot;, Access.PUBLIC | Access.STATIC);
  78. CodeAttr code = m.startCode();
  79. code.pushScope();
  80. code.emitLoad(code.getArg(0));
  81. code.emitLoad(code.getArg(1));
  82. code.emitAdd(Type.intType);
  83. Variable resultVar = code.addLocal(Type.intType, &quot;result&quot;);
  84. code.emitDup();
  85. code.emitStore(resultVar);
  86. code.emitReturn();
  87. code.popScope();
  88. <font color="green"> // Get a byte[] representing the class file.
  89. // We could write this to disk if we wanted.</font>
  90. byte[] classFile = c.writeToArray();
  91. <font color="green"> // Disassemble this class.
  92. // The output is similar to javap(1).</font>
  93. ClassTypeWriter.print(c, System.out, 0);
  94. <font color="green"> // Load the generated class into this JVM.
  95. // gnu.bytecode provides ArrayClassLoader, or you can use your own.</font>
  96. ArrayClassLoader cl = new ArrayClassLoader();
  97. cl.addClass(&quot;HelloWorld&quot;, classFile);
  98. <font color="green"> // Actual invocation is just the usual reflection code.</font>
  99. Class&lt;?&gt; helloWorldClass = cl.loadClass(&quot;HelloWorld&quot;, true);
  100. Class[] argTypes = new Class[] { int.class, int.class };
  101. int result = (Integer) helloWorldClass.getMethod(&quot;add&quot;, argTypes).invoke(null, 1, 2);
  102. System.err.println(result);
  103. }
  104. }
  105. </pre></blockquote>
  106. <h2>License</h2>
  107. See the file <a href="../../COPYING">COPYING</a>.
  108. <h2>Author</h2>
  109. <a href="http://www.bothner.com/~per">Per Bothner</a>
  110. <tt>&lt;<a href="mailto:per@bothner.com">per@bothner.com</a>&gt;</tt>
  111. <h2>How to get it</h2>
  112. The <code>gnu.bytecode</code> package is currently distributed as part of
  113. <a href="../kawa/">Kawa</a>, though it can be used independently
  114. of the rest of Kawa.
  115. <h2>Bugs and patches</h2>
  116. Send them to <a href="mailto:per@bothner.com">per@bothner.com</a>,
  117. or to the <a href="mailto:kawa@sourceware.cygnus.com">Kawa mailing list</a>.
  118. </body>
  119. </html>