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