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

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