    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.bcel.util;
    019
    020import java.lang.reflect.Method;
    021import java.lang.reflect.Modifier;
    022
    023/**
    024 * Java interpreter replacement, i.e., wrapper that uses its own ClassLoader
    025 * to modify/generate classes as they're requested. You can take this as a template
    026 * for your own applications.
    027 * <p>
    028 * Call this wrapper with:
    029 * </p>
    030 * <pre>java org.apache.bcel.util.JavaWrapper &lt;real.class.name&gt; [arguments]</pre>
    031 * <p>
    032 * To use your own class loader you can set the "bcel.classloader" system property.
    033 * </p>
    034 * <pre>java org.apache.bcel.util.JavaWrapper -Dbcel.classloader=foo.MyLoader &lt;real.class.name&gt; [arguments]</pre>
    035 *
    036 * @see ClassLoader
    037 */
    038public class JavaWrapper {
    039
    040    private final java.lang.ClassLoader loader;
    041
    042
    043    private static java.lang.ClassLoader getClassLoader() {
    044        final String s = System.getProperty("bcel.classloader");
    045        if ((s == null) || "".equals(s)) {
    046            throw new IllegalStateException("The property 'bcel.classloader' must be defined");
    047        }
    048        try {
    049            return (java.lang.ClassLoader) Class.forName(s).newInstance();
    050        } catch (final Exception e) {
    051            throw new IllegalStateException(e.toString(), e);
    052        }
    053    }
    054
    055
    056    public JavaWrapper(final java.lang.ClassLoader loader) {
    057        this.loader = loader;
    058    }
    059
    060
    061    public JavaWrapper() {
    062        this(getClassLoader());
    063    }
    064
    065
    066    /** Runs the main method of the given class with the arguments passed in argv
    067     *
    068     * @param class_name the fully qualified class name
    069     * @param argv the arguments just as you would pass them directly
    070     */
    071    public void runMain( final String class_name, final String[] argv ) throws ClassNotFoundException {
    072        final Class<?> cl = loader.loadClass(class_name);
    073        Method method = null;
    074        try {
    075            method = cl.getMethod("main", argv.getClass());
    076            /* Method main is sane ?
    077             */
    078            final int m = method.getModifiers();
    079            final Class<?> r = method.getReturnType();
    080            if (!(Modifier.isPublic(m) && Modifier.isStatic(m)) || Modifier.isAbstract(m)
    081                    || (r != Void.TYPE)) {
    082                throw new NoSuchMethodException();
    083            }
    084        } catch (final NoSuchMethodException no) {
    085            System.out.println("In class " + class_name
    086                    + ": public static void main(String[] argv) is not defined");
    087            return;
    088        }
    089        try {
    090            method.invoke(null, argv);
    091        } catch (final Exception ex) {
    092            ex.printStackTrace();
    093        }
    094    }
    095
    096
    097    /** Default main method used as wrapper, expects the fully qualified class name
    098     * of the real class as the first argument.
    099     */
    100    public static void main( final String[] argv ) throws Exception {
    101        /* Expects class name as first argument, other arguments are by-passed.
    102         */
    103        if (argv.length == 0) {
    104            System.out.println("Missing class name.");
    105            return;
    106        }
    107        final String class_name = argv[0];
    108        final String[] new_argv = new String[argv.length - 1];
    109        System.arraycopy(argv, 1, new_argv, 0, new_argv.length);
    110        final JavaWrapper wrapper = new JavaWrapper();
    111        wrapper.runMain(class_name, new_argv);
    112    }
    113}