您的位置:首页 > 移动开发 > Android开发

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

2016-11-30 09:04 696 查看
转载请注明出处: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());
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息