/**
* 根据变量名字,来获取实体对应字段的值
* @param element
* @param o
* @return
*/
public static Map<String, Object> getMapByEntityElement(String[] element, Object o){
try {
Class aClass = o.getClass();
Field[] declaredFields = aClass.getDeclaredFields();
Map<String, Object> map = new HashMap<String, Object>(12);
for (int i = 1; i < declaredFields.length; i++) {
Field field = declaredFields[i];
String fieldName = field.getName();
String getMethod = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length());
Object invoke = aClass.getMethod(getMethod).invoke(o);
for (String s : element) {
if (s.equals(fieldName)) {
map.put(fieldName, invoke);
}
}
}
return map;
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException n) {
Class<? extends ReflectiveOperationException> aClass = n.getClass();
if("NoSuchMethod".equals(aClass)){
System.out.println("没有对应方法!");
}
if("IllegalAccess".equals(aClass)){
System.out.println("权限不足!");
}
if("InvocationTarget".equals(aClass)){
System.out.println("错误!");
}
n.printStackTrace();
return null;
}
}
09-27
7851
7851

被折叠的 条评论
为什么被折叠?



