Android 源码中ZygoteInit 调用ActivityThread main 函数 实例

20 篇文章 0 订阅
3 篇文章 0 订阅

本文讲通过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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值