Java interpreter replacement : ClassLoader « Reflection « Java






Java interpreter replacement

   
/*
 * Copyright  2000-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 *
 */

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * Java interpreter replacement, i.e., wrapper that uses its own ClassLoader to
 * modify/generate classes as they're requested. You can take this as a template
 * for your own applications.<br>
 * Call this wrapper with
 * 
 * 
 * @version $Id: JavaWrapper.java 386056 2006-03-15 11:31:56Z tcurdt $
 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
 * @see ClassLoader
 */
public class JavaWrapper {

  private java.lang.ClassLoader loader;

  private static java.lang.ClassLoader getClassLoader() {
    String s = System.getProperty("bcel.classloader");
    if ((s == null) || "".equals(s)) {
      s = "org.apache.bcel.util.ClassLoader";
    }
    try {
      return (java.lang.ClassLoader) Class.forName(s).newInstance();
    } catch (Exception e) {
      throw new RuntimeException(e.toString());
    }
  }

  public JavaWrapper(java.lang.ClassLoader loader) {
    this.loader = loader;
  }

  public JavaWrapper() {
    this(getClassLoader());
  }

  /**
   * Runs the main method of the given class with the arguments passed in argv
   * 
   * @param class_name
   *          the fully qualified class name
   * @param argv
   *          the arguments just as you would pass them directly
   */
  public void runMain(String class_name, String[] argv) throws ClassNotFoundException {
    Class cl = loader.loadClass(class_name);
    Method method = null;
    try {
      method = cl.getMethod("main", new Class[] { argv.getClass() });
      /*
       * Method main is sane ?
       */
      int m = method.getModifiers();
      Class r = method.getReturnType();
      if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m)
          || (r != Void.TYPE)) {
        throw new NoSuchMethodException();
      }
    } catch (NoSuchMethodException no) {
      System.out.println("In class " + class_name
          + ": public static void main(String[] argv) is not defined");
      return;
    }
    try {
      method.invoke(null, new Object[] { argv });
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * Default main method used as wrapper, expects the fully qualified class name
   * of the real class as the first argument.
   */
  public static void main(String[] argv) throws Exception {
    /*
     * Expects class name as first argument, other arguments are by-passed.
     */
    if (argv.length == 0) {
      System.out.println("Missing class name.");
      return;
    }
    String class_name = argv[0];
    String[] new_argv = new String[argv.length - 1];
    System.arraycopy(argv, 1, new_argv, 0, new_argv.length);
    JavaWrapper wrapper = new JavaWrapper();
    wrapper.runMain(class_name, new_argv);
  }
}

   
    
    
  








Related examples in the same category

1.Determining from Where a Class Was Loaded
2.Loading a Class That Is Not on the Classpath
3.Dynamically Reloading a Modified Class
4.Get the path from where a class is loaded
5.Using the forName() method
6.Unqualified names
7.Return the context classloader.
8.Jar Class Loader
9.Zip Class Loader
10.Load class from Thread's ContextClassLoader, classloader of derived class or the system ClassLoader
11.Various Class Loader utilities
12.Load Class
13.Load classes
14.Class Finder
15.Class For Name
16.Context ClassLoader
17.Analyze ClassLoader hierarchy for any given object or class loader
18.A class loader which loads classes using a searchlist of other classloaders
19.Equivalently to invoking Thread.currentThread().getContextClassLoader().loadClass(className); but it supports primitive types and array classes of object types or primitive types.
20.A class to simplify access to resources through the classloader
21.Get a compatible constructor for the given value type
22.Force the given class to be loaded fully.
23.does Class Exist
24.Utility to essentially do Class forName and allow configurable Classloaders.
25.Attempts to list all the classes in the specified package as determined by the context class loader...
26.Resolve class and load class