FileDocCategorySizeDatePackage
JavaWrapper.javaAPI DocJava SE 6 API5828Tue Jun 10 00:22:24 BST 2008com.sun.org.apache.bcel.internal.util

JavaWrapper

public class JavaWrapper extends Object
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.
Call this wrapper with
java com.sun.org.apache.bcel.internal.util.JavaWrapper <real.class.name> [arguments]

To use your own class loader you can set the "bcel.classloader" system property which defaults to "com.sun.org.apache.bcel.internal.util.ClassLoader", e.g., with

java com.sun.org.apache.bcel.internal.util.JavaWrapper -Dbcel.classloader=foo.MyLoader <real.class.name> [arguments]

version
$Id: JavaWrapper.java,v 1.1.2.1 2005/07/31 23:47:01 jeffsuttor Exp $
author
M. Dahm
see
ClassLoader

Fields Summary
private ClassLoader
loader
Constructors Summary
public JavaWrapper(ClassLoader loader)

    this.loader = loader;
  
public JavaWrapper()

    this(getClassLoader());
  
Methods Summary
public static void_main(java.lang.String[] argv)
Default _main method used as wrapper, expects the fully qualified class name of the real class as the first argument.

    /* 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);
  
private static java.lang.ClassLoadergetClassLoader()

    String s = System.getProperty("bcel.classloader");

    if((s == null) || "".equals(s))
      s = "com.sun.org.apache.bcel.internal.util.ClassLoader";

    try {
      return (java.lang.ClassLoader)Class.forName(s).newInstance();
    } catch(Exception e) {
      throw new RuntimeException(e.toString());
    }
  
public voidrunMain(java.lang.String class_name, java.lang.String[] argv)
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

    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();
    }