通過java main函數調用 其它main函數執行

本文講通過java main函數調用 其它main函數執行 (當然也可以調用其它函數) ;android 部分源碼

被調用的 java main函數

package main;

public class Main {

public static void main(String[] args) {

System.out.println(args[0]);

}

}

 

調用Java main函數的類

package main;

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

public class TestMain {
    public static void main(String[] args) {
        TestMain testMain = new TestMain();

        Runnable runnable = findStaticMain("main.Main", new String[] { "testtest" }, testMain.getClass().getClassLoader());
        runnable.run();
    }

    /**
     * Invokes a static "main(argv[]) method on class "className". Converts various
     * failing exceptions into RuntimeExceptions, with the assumption that they will
     * then cause the VM instance to exit.
     *
     * @param className
     *            Fully-qualified class name
     * @param argv
     *            Argument vector for main()
     * @param classLoader
     *            the classLoader to load {@className} with
     */
    protected static Runnable findStaticMain(String className, String[] argv, ClassLoader classLoader) {
        Class<?> cl;

        try {
            cl = Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException ex) {
            throw new RuntimeException("Missing class when invoking static main " + className, ex);
        }

        Method m;
        try {
            m = cl.getMethod("main", new Class[] { String[].class });
        } catch (NoSuchMethodException ex) {
            throw new RuntimeException("Missing static main on " + className, ex);
        } catch (SecurityException ex) {
            throw new RuntimeException("Problem getting static main on " + className, ex);
        }

        int modifiers = m.getModifiers();
        if (!(Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers))) {
            throw new RuntimeException("Main method is not public and static on " + className);
        }

        /*
         * This throw gets caught in ZygoteInit.main(), which responds by invoking the
         * exception's run() method. This arrangement clears up all the stack frames
         * that were required in setting up the process.
         */
        return new MethodAndArgsCaller(m, argv);
    }
}
 

利用反射調用main函數

package main;

 

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

 

/**

 * Helper class which holds a method and arguments and can call them. This is

 * used as part of a trampoline to get rid of the initial process setup stack

 * frames.

 */

public class MethodAndArgsCaller implements Runnable {

/** method to call */

private final Method mMethod;

 

/** argument array */

private final String[] mArgs;

 

public MethodAndArgsCaller(Method method, String[] args) {

mMethod = method;

mArgs = args;

}

 

public void run() {

try {

mMethod.invoke(null, new Object[] { mArgs });

} catch (IllegalAccessException ex) {

throw new RuntimeException(ex);

} catch (InvocationTargetException ex) {

Throwable cause = ex.getCause();

if (cause instanceof RuntimeException) {

throw (RuntimeException) cause;

} else if (cause instanceof Error) {

throw (Error) cause;

}

throw new RuntimeException(ex);

}

}

}

發佈了49 篇原創文章 · 獲贊 17 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章