Java interpreter replacement : ClassLoader « Reflection « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Reflection » ClassLoader 




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:[email protected]">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.ClassLoaderClass.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[] argvthrows 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[] argvthrows 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.