1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 <real.class.name> [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 <real.class.name> [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
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
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 }