Java反射在Android中的应用,以及注意事项

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53402586
本文出自【DylanAndroid的博客】


Java反射在Android中的应用,以及注意事项

【玩转SQLite系列】(七)打造轻量级ORM工具类SQLiteDbUtil操作数据库
这篇文章当中,我封装了一个轻量级的数据库ORM工具类,其中我们发现,不管是建表、查询数据、插入数据、都只需要一个简单的javabean对象,这正是这个
轻量级的工具类的强大之处,那么强大的背后到底是如何通过数据库中的字段和javabean对象之间去互转呢?这就牵扯到一个java的基础只是:java的反射。

1.Java反射工具类

package cn.bluemobi.dylan.sqlitelibrary;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.R.attr.value;

/**
 * Created by yuandl on 2016-11-21.
 */a
public class JavaReflectUtil {
    /**
     * 获取类的简名,不包含包名
     *
     * @param c 类
     * @return 类名
     */
    public static String getClassName(Class c) {
        if (c == null) {
            return null;
        }
        return c.getSimpleName();
    }

    /**
     * 获取类的属性名
     *
     * @param c 类
     * @return 所有属性的数组
     */
    public static String[] getAttributeNames(Class c) {
        if (c == null) {
            return null;
        }
        Field[] declaredFields = new Field[0];
        try {
            declaredFields = c.getDeclaredFields();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        List<String> names = new ArrayList<>();
        for (int i = 0; i < declaredFields.length; i++) {
            /**忽略编译产生的属性**/
            if (declaredFields[i].isSynthetic()) {
                continue;
            }
            /**忽略serialVersionUID**/
            if (declaredFields[i].getName().equals("serialVersionUID")) {
                continue;
            }
            names.add(declaredFields[i].getName());
        }
        return names.toArray(new String[names.size()]);
    }

    /**
     * 获取类的属性类型
     *
     * @param c 类
     * @return 所有属性类型的数组
     */
    public static Class[] getAttributeType(Class c) {
        if (c == null) {
            return null;
        }
        Field[] declaredFields = new Field[0];
        try {
            declaredFields = c.getDeclaredFields();
        } catch (SecurityException e) {
            e.printStackTrace();
        }

        List<Object> types = new ArrayList<>();
        for (int i = 0; i < declaredFields.length; i++) {
            if (declaredFields[i].isSynthetic()) {
                continue;
            }
            if (declaredFields[i].getName().equals("serialVersionUID")) {
                continue;
            }
            types.add(declaredFields[i].getType());
        }
        return types.toArray(new Class[types.size()]);
    }

    /**
     * 获取类的属性名获取属性值
     *
     * @param o         类对象
     * @param attribute 属性名称
     * @return 所对应的属性值
     */
    public static Object getValueByAttribute(Object o, String attribute) {
        if (o == null) {
            return null;
        }
        if (attribute == null || attribute.isEmpty()) {
            return null;
        }
        try {
            String getMethodName = "get" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1);
            Method method = o.getClass().getMethod(getMethodName, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (NoSuchMethodException e) {
            String getMethodName = "is" + attribute.substring(0, 1).toUpperCase() + attribute.substring(1);
            try {
                Method method = o.getClass().getMethod(getMethodName, new Class[]{});
                Object value = method.invoke(o, new Object[]{});
                return value;
            } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
            } catch (InvocationTargetException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取类的属性名获取属性值
     *
     * @param o 类对象
     * @return 所对应的属性值
     */
    public static List<Map<String, Object>> getAllFiledInfo(Object o) {
        if (o == null) {
            return null;
        }
        String[] attributes = getAttributeNames(o.getClass());
        Object[] attributeTypes = getAttributeType(o.getClass());
        List<Map<String, Object>> allFiledInfos = new ArrayList<>();
        for (int i = 0; i < attributes.length; i++) {
            Map<String, Object> allFiledInfo = new HashMap<>();
            allFiledInfo.put("name", attributes[i]);
            allFiledInfo.put("type", attributeTypes[i]);
            allFiledInfo.put("value", getValueByAttribute(o, attributes[i]));
            allFiledInfos.add(allFiledInfo);
        }
        return allFiledInfos;
    }

}

2.在Android中需要注意的事项

我们在遍历循环一个类中的属性还是属性名的时候一定要注意:Android studio2.2之后的Instant Run功能的使用会导致JavaBean对象在编译之后
多产生两个属性。所以,我们在获取的时候一定要记得忽略。

  for (int i = 0; i < declaredFields.length; i++) {
            /**忽略编译产生的属性**/
            if (declaredFields[i].isSynthetic()) {
                continue;
            }
            /**忽略serialVersionUID**/
            if (declaredFields[i].getName().equals("serialVersionUID")) {
                continue;
            }
            names.add(declaredFields[i].getName());
        }
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值