View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.util;
18  
19  import java.lang.reflect.Method;
20  import java.lang.reflect.Modifier;
21  
22  /***
23   * Java interpreter replacement, i.e., wrapper that uses its own ClassLoader
24   * to modify/generate classes as they're requested. You can take this as a template
25   * for your own applications.<br>
26   * Call this wrapper with
27   * <pre>java org.apache.bcel.util.JavaWrapper &lt;real.class.name&gt; [arguments]</pre>
28   * <p>
29   * To use your own class loader you can set the "bcel.classloader" system property
30   * which defaults to "org.apache.bcel.util.ClassLoader", e.g., with
31   * <pre>java org.apache.bcel.util.JavaWrapper -Dbcel.classloader=foo.MyLoader &lt;real.class.name&gt; [arguments]</pre>
32   * </p>
33   *
34   * @version $Id: JavaWrapper.java 386056 2006-03-15 11:31:56Z tcurdt $
35   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
36   * @see ClassLoader
37   */
38  public class JavaWrapper {
39  
40      private java.lang.ClassLoader loader;
41  
42  
43      private static java.lang.ClassLoader getClassLoader() {
44          String s = System.getProperty("bcel.classloader");
45          if ((s == null) || "".equals(s)) {
46              s = "org.apache.bcel.util.ClassLoader";
47          }
48          try {
49              return (java.lang.ClassLoader) Class.forName(s).newInstance();
50          } catch (Exception e) {
51              throw new RuntimeException(e.toString());
52          }
53      }
54  
55  
56      public JavaWrapper(java.lang.ClassLoader loader) {
57          this.loader = loader;
58      }
59  
60  
61      public JavaWrapper() {
62          this(getClassLoader());
63      }
64  
65  
66      /*** Runs the main method of the given class with the arguments passed in argv
67       *
68       * @param class_name the fully qualified class name
69       * @param argv the arguments just as you would pass them directly
70       */
71      public void runMain( String class_name, String[] argv ) throws ClassNotFoundException {
72          Class cl = loader.loadClass(class_name);
73          Method method = null;
74          try {
75              method = cl.getMethod("main", new Class[] {
76                  argv.getClass()
77              });
78              /* Method main is sane ?
79               */
80              int m = method.getModifiers();
81              Class r = method.getReturnType();
82              if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m)
83                      || (r != Void.TYPE)) {
84                  throw new NoSuchMethodException();
85              }
86          } catch (NoSuchMethodException no) {
87              System.out.println("In class " + class_name
88                      + ": public static void main(String[] argv) is not defined");
89              return;
90          }
91          try {
92              method.invoke(null, new Object[] {
93                  argv
94              });
95          } catch (Exception ex) {
96              ex.printStackTrace();
97          }
98      }
99  
100 
101     /*** Default main method used as wrapper, expects the fully qualified class name
102      * of the real class as the first argument.
103      */
104     public static void main( String[] argv ) throws Exception {
105         /* Expects class name as first argument, other arguments are by-passed.
106          */
107         if (argv.length == 0) {
108             System.out.println("Missing class name.");
109             return;
110         }
111         String class_name = argv[0];
112         String[] new_argv = new String[argv.length - 1];
113         System.arraycopy(argv, 1, new_argv, 0, new_argv.length);
114         JavaWrapper wrapper = new JavaWrapper();
115         wrapper.runMain(class_name, new_argv);
116     }
117 }