You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Loader.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.tools.reflect;
  17. import javassist.CannotCompileException;
  18. import javassist.NotFoundException;
  19. import javassist.ClassPool;
  20. /**
  21. * A class loader for reflection.
  22. *
  23. * <p>To run a program, say <code>MyApp</code>,
  24. * including a reflective class,
  25. * you must write a start-up program as follows:
  26. *
  27. * <pre>
  28. * public class Main {
  29. * public static void main(String[] args) throws Throwable {
  30. * javassist.tools.reflect.Loader cl
  31. * = (javassist.tools.reflect.Loader)Main.class.getClassLoader();
  32. * cl.makeReflective("Person", "MyMetaobject",
  33. * "javassist.tools.reflect.ClassMetaobject");
  34. * cl.run("MyApp", args);
  35. * }
  36. * }
  37. * </pre>
  38. *
  39. * <p>Then run this program as follows:
  40. *
  41. * <pre>% java javassist.tools.reflect.Loader Main arg1, ...</pre>
  42. *
  43. * <p>This command runs <code>Main.main()</code> with <code>arg1</code>, ...
  44. * and <code>Main.main()</code> runs <code>MyApp.main()</code> with
  45. * <code>arg1</code>, ...
  46. * The <code>Person</code> class is modified
  47. * to be a reflective class. Method calls on a <code>Person</code>
  48. * object are intercepted by an instance of <code>MyMetaobject</code>.
  49. *
  50. * <p>Also, you can run <code>MyApp</code> in a slightly different way:
  51. *
  52. * <pre>
  53. * public class Main2 {
  54. * public static void main(String[] args) throws Throwable {
  55. * javassist.tools.reflect.Loader cl = new javassist.tools.reflect.Loader();
  56. * cl.makeReflective("Person", "MyMetaobject",
  57. * "javassist.tools.reflect.ClassMetaobject");
  58. * cl.run("MyApp", args);
  59. * }
  60. * }
  61. * </pre>
  62. *
  63. * <p>This program is run as follows:
  64. *
  65. * <pre>% java Main2 arg1, ...</pre>
  66. *
  67. * <p>The difference from the former one is that the class <code>Main</code>
  68. * is loaded by <code>javassist.tools.reflect.Loader</code> whereas the class
  69. * <code>Main2</code> is not. Thus, <code>Main</code> belongs
  70. * to the same name space (security domain) as <code>MyApp</code>
  71. * whereas <code>Main2</code> does not; <code>Main2</code> belongs
  72. * to the same name space as <code>javassist.tools.reflect.Loader</code>.
  73. * For more details,
  74. * see the notes in the manual page of <code>javassist.Loader</code>.
  75. *
  76. * <p>The class <code>Main2</code> is equivalent to this class:
  77. *
  78. * <pre>
  79. * public class Main3 {
  80. * public static void main(String[] args) throws Throwable {
  81. * Reflection reflection = new Reflection();
  82. * javassist.Loader cl
  83. * = new javassist.Loader(ClassPool.getDefault(reflection));
  84. * reflection.makeReflective("Person", "MyMetaobject",
  85. * "javassist.tools.reflect.ClassMetaobject");
  86. * cl.run("MyApp", args);
  87. * }
  88. * }
  89. * </pre>
  90. *
  91. * <p><b>Note:</b>
  92. *
  93. * <p><code>javassist.tools.reflect.Loader</code> does not make a class reflective
  94. * if that class is in a <code>java.*</code> or
  95. * <code>javax.*</code> pacakge because of the specifications
  96. * on the class loading algorithm of Java. The JVM does not allow to
  97. * load such a system class with a user class loader.
  98. *
  99. * <p>To avoid this limitation, those classes should be statically
  100. * modified with <code>javassist.tools.reflect.Compiler</code> and the original
  101. * class files should be replaced.
  102. *
  103. * @see javassist.tools.reflect.Reflection
  104. * @see javassist.tools.reflect.Compiler
  105. * @see javassist.Loader
  106. */
  107. public class Loader extends javassist.Loader {
  108. protected Reflection reflection;
  109. /**
  110. * Loads a class with an instance of <code>Loader</code>
  111. * and calls <code>main()</code> in that class.
  112. *
  113. * @param args command line parameters.
  114. * <ul>
  115. * <code>args[0]</code> is the class name to be loaded.
  116. * <br><code>args[1..n]</code> are parameters passed
  117. * to the target <code>main()</code>.
  118. * </ul>
  119. */
  120. public static void main(String[] args) throws Throwable {
  121. Loader cl = new Loader();
  122. cl.run(args);
  123. }
  124. /**
  125. * Constructs a new class loader.
  126. */
  127. public Loader() throws CannotCompileException, NotFoundException {
  128. super();
  129. delegateLoadingOf("javassist.tools.reflect.Loader");
  130. reflection = new Reflection();
  131. ClassPool pool = ClassPool.getDefault();
  132. addTranslator(pool, reflection);
  133. }
  134. /**
  135. * Produces a reflective class.
  136. * If the super class is also made reflective, it must be done
  137. * before the sub class.
  138. *
  139. * @param clazz the reflective class.
  140. * @param metaobject the class of metaobjects.
  141. * It must be a subclass of
  142. * <code>Metaobject</code>.
  143. * @param metaclass the class of the class metaobject.
  144. * It must be a subclass of
  145. * <code>ClassMetaobject</code>.
  146. * @return <code>false</code> if the class is already reflective.
  147. *
  148. * @see javassist.tools.reflect.Metaobject
  149. * @see javassist.tools.reflect.ClassMetaobject
  150. */
  151. public boolean makeReflective(String clazz,
  152. String metaobject, String metaclass)
  153. throws CannotCompileException, NotFoundException
  154. {
  155. return reflection.makeReflective(clazz, metaobject, metaclass);
  156. }
  157. }