JavaWrapper.java

  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. import java.lang.reflect.Method;
  19. import java.lang.reflect.Modifier;

  20. import org.apache.commons.lang3.StringUtils;

  21. /**
  22.  * Java interpreter replacement, i.e., wrapper that uses its own ClassLoader to modify/generate classes as they're
  23.  * requested. You can take this as a template for your own applications.
  24.  * <p>
  25.  * Call this wrapper with:
  26.  * </p>
  27.  *
  28.  * <pre>
  29.  * java org.apache.bcel.util.JavaWrapper &lt;real.class.name&gt; [arguments]
  30.  * </pre>
  31.  * <p>
  32.  * To use your own class loader you can set the "bcel.classloader" system property.
  33.  * </p>
  34.  *
  35.  * <pre>
  36.  * java org.apache.bcel.util.JavaWrapper -Dbcel.classloader=foo.MyLoader &lt;real.class.name&gt; [arguments]
  37.  * </pre>
  38.  *
  39.  * @see ClassLoader
  40.  */
  41. public class JavaWrapper {

  42.     private static java.lang.ClassLoader getClassLoader() {
  43.         final String s = System.getProperty("bcel.classloader");
  44.         if (StringUtils.isEmpty(s)) {
  45.             throw new IllegalStateException("The property 'bcel.classloader' must be defined");
  46.         }
  47.         try {
  48.             return (java.lang.ClassLoader) Class.forName(s).getConstructor().newInstance();
  49.         } catch (final Exception e) {
  50.             throw new IllegalStateException(e.toString(), e);
  51.         }
  52.     }

  53.     /**
  54.      * Default main method used as wrapper, expects the fully qualified class name of the real class as the first argument.
  55.      */
  56.     public static void main(final String[] argv) throws Exception {
  57.         /*
  58.          * Expects class name as first argument, other arguments are by-passed.
  59.          */
  60.         if (argv.length == 0) {
  61.             System.out.println("Missing class name.");
  62.             return;
  63.         }
  64.         final String className = argv[0];
  65.         final String[] newArgv = new String[argv.length - 1];
  66.         System.arraycopy(argv, 1, newArgv, 0, newArgv.length);
  67.         new JavaWrapper().runMain(className, newArgv);
  68.     }

  69.     private final java.lang.ClassLoader loader;

  70.     public JavaWrapper() {
  71.         this(getClassLoader());
  72.     }

  73.     public JavaWrapper(final java.lang.ClassLoader loader) {
  74.         this.loader = loader;
  75.     }

  76.     /**
  77.      * Runs the main method of the given class with the arguments passed in argv
  78.      *
  79.      * @param className the fully qualified class name
  80.      * @param argv the arguments just as you would pass them directly
  81.      * @throws ClassNotFoundException if {@code className} can't be found.
  82.      */
  83.     public void runMain(final String className, final String[] argv) throws ClassNotFoundException {
  84.         final Class<?> cl = loader.loadClass(className);
  85.         Method method = null;
  86.         try {
  87.             method = cl.getMethod("main", argv.getClass());
  88.             /*
  89.              * Method main is sane ?
  90.              */
  91.             final int m = method.getModifiers();
  92.             final Class<?> r = method.getReturnType();
  93.             if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m) || r != Void.TYPE) {
  94.                 throw new NoSuchMethodException();
  95.             }
  96.         } catch (final NoSuchMethodException no) {
  97.             System.out.println("In class " + className + ": public static void main(String[] argv) is not defined");
  98.             return;
  99.         }
  100.         try {
  101.             method.invoke(null, (Object[]) argv);
  102.         } catch (final Exception ex) {
  103.             ex.printStackTrace();
  104.         }
  105.     }
  106. }