/Users/lyon/j4p/src/javassist/reflect/Loader.java

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