Android 反射工具

今天给大家介绍一个工具类:反射工具类。
个人觉得这个工具类还是非常方便的,下面有示例。

代码:

public class ReflectUtils {

    public static Method findMethod(Object instance, String name, Class<?>... parameterTypes) throws NoSuchMethodException {
        for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
            try {
                Method method = clazz.getDeclaredMethod(name, parameterTypes);
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                return method;
            } catch (NoSuchMethodException e) {
                // ignore and search next
            }
        }
        throw new NoSuchMethodException("Method " + name + " with parameters " + Arrays.asList(parameterTypes) + " not found in " + instance.getClass());
    }

    public static Field findField(Object instance, String name) throws NoSuchFieldException {
        for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
            try {
                Field field = clazz.getDeclaredField(name);
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                return field;
            } catch (NoSuchFieldException ignore) {
            }
        }
        throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
    }

    public static Field findField(String className, String name) throws NoSuchFieldException, ClassNotFoundException {
        for (Class<?> clazz = Class.forName(className); clazz != null; clazz = clazz.getSuperclass()) {
            try {
                Field field = clazz.getDeclaredField(name);
                if (!field.isAccessible()) {
                    field.setAccessible(true);
                }
                return field;
            } catch (NoSuchFieldException ignore) {
            }
        }
        throw new NoSuchFieldException("Field " + name + " not found in " + className);
    }
}

示例代码:

    private static Field hField;
    private static Field bField;
    private static Field aField;
    private static Field fField;
    private String getPackageName(AdView adView) {
        try {
            if (hField == null) {
                hField = ReflectUtils.findField(adView, "h");
            }
            Object h = hField.get(adView);
            if (bField == null) {
                bField = ReflectUtils.findField("com.f.a", "b");
            }
            HashSet hashSet = (HashSet) bField.get(h);
            Iterator<String> iterator = hashSet.iterator();
            if(iterator.hasNext()) {
                return iterator.next();
            }
            return null;
        } catch (Exception ignore) {
        }
        return null;
    }

用最简单的白话总结为:
AdView类中有属性h, H这个类中有属性b, b属性为HashSet ,获取其中的数值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值