hiddenapi无法反射问题

一.摘要

    最近在q上遇到一个问题,有同事反应为什么我反射的方法在q上找不到对应的方法了,只获得了一些class.java的方法,这是q上在art中新加的hiddenapi相关的策略做了限制导致的,那么我们来看看是怎么限制的吧

二.代码分析

1.测试代码:


void test() {
    List list = new ArrayList();
    Class mclass = null;
    try {
        mclass = Class.forName("android.app.Utils");
        Log.d(TAG, mclass.getName());
        // android.app.SmtPCUtils
        Method[] methods = mclass.getMethods();
        for (Method s : methods) {
            Log.d(TAG, s.getName());
            list.add(s.getName());
        }
        Log.d(TAG, list.toString());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

2.系统源码:

2.1 getMethods

public Method[] getMethods() throws SecurityException {

    List<Method> methods = new ArrayList<Method>();

    // 获取public方法

    getPublicMethodsInternal(methods);

    /*

     * Remove duplicate methods defined by superclasses and

     * interfaces, preferring to keep methods declared by derived

     * types.

     */

    CollectionUtils.removeDuplicates(methods, Method.ORDER_BY_SIGNATURE);

    return methods.toArray(new Method[methods.size()]);

}



private void getPublicMethodsInternal(List<Method> result) {

    // 获取该类中宣告可以调用的方法

    Collections.addAll(result, getDeclaredMethodsUnchecked(true));

    if (!isInterface()) {

        // Search superclasses, for interfaces don't search java.lang.Object.

        for (Class<?> c = superClass; c != null; c = c.superClass) {

            // 获取该类的父类中可以调用的方法

            Collections.addAll(result, c.getDeclaredMethodsUnchecked(true));

        }

    }

    // Search iftable which has a flattened and uniqued list of interfaces.

    Object[] iftable = ifTable;

    if (iftable != null) {

        for (int i = 0; i < iftable.length; i += 2) {

            Class<?> ifc = (Class<?>) iftable[i];

            // 获取接口方法

            Collections.addAll(result, ifc.getDeclaredMethodsUnchecked(true));

        }

    }

}

这里我们会去获取android.app.SmtPCUtils类的所有方法:打个断点:

我们从断点中确实可以看得,我们没有获得android.app.SmtPCUtils类的方法,那我们直接看看getDeclaredMethodsUnchecked方法吧:

2.2 Class_getDeclaredMethodsUnchecked

http://aosp.opersys.com/xref/android-10.0.0_r41/xref/art/runtime/native/java_lang_Class.cc#575

static jobjectArray Class_getDeclaredMethodsUnchecked(JNIEnv* env, jobject javaThis,

                                                      jboolean publicOnly) {

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值