KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > util > JavaWrapper


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 JavaDoc;
20 import java.lang.reflect.Modifier JavaDoc;
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 JavaDoc loader;
41
42
43     private static java.lang.ClassLoader JavaDoc getClassLoader() {
44         String JavaDoc 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 JavaDoc) Class.forName(s).newInstance();
50         } catch (Exception JavaDoc e) {
51             throw new RuntimeException JavaDoc(e.toString());
52         }
53     }
54
55
56     public JavaWrapper(java.lang.ClassLoader JavaDoc 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 JavaDoc class_name, String JavaDoc[] argv ) throws ClassNotFoundException JavaDoc {
72         Class JavaDoc cl = loader.loadClass(class_name);
73         Method JavaDoc method = null;
74         try {
75             method = cl.getMethod("main", new Class JavaDoc[] {
76                 argv.getClass()
77             });
78             /* Method main is sane ?
79              */

80             int m = method.getModifiers();
81             Class JavaDoc r = method.getReturnType();
82             if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m)
83                     || (r != Void.TYPE)) {
84                 throw new NoSuchMethodException JavaDoc();
85             }
86         } catch (NoSuchMethodException JavaDoc 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 JavaDoc[] {
93                 argv
94             });
95         } catch (Exception JavaDoc 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 JavaDoc[] argv ) throws Exception JavaDoc {
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 JavaDoc class_name = argv[0];
112         String JavaDoc[] new_argv = new String JavaDoc[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 }
118
Popular Tags