ObjectTools反射实例
Posted therunningfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ObjectTools反射实例相关的知识,希望对你有一定的参考价值。
ObjectTools反射实例
package com.shitou.deposit.chinapnr.utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * ObjectTools * @author zhouky * @since 2018年12月6日 */ public class ObjectTools { private static transient final Log log = LogFactory.getLog("ObjectTools"); /** * 根据属性名获取属性值 * @param fieldName * @param o * @return */ private Object getFieldValueByName(String fieldName, Object o) { try { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = "get" + firstLetter + fieldName.substring(1); Method method = o.getClass().getMethod(getter, new Class[] {}); Object value = method.invoke(o, new Object[] {}); return value; } catch (Exception e) { log.error(e.getMessage(), e); return null; } } /** * 获取属性名数组 * @param o * @return */ private String[] getFiledName(Object o) { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { System.out.println(fields[i].getType()); fieldNames[i] = fields[i].getName(); } return fieldNames; } /** * 获取属性名数组 * @param o * @return */ public List<Map<String, Object>> getFiledsInfo(Object o) { Field[] fields = o.getClass().getDeclaredFields(); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> infoMap = null; for (int i = 0; i < fields.length; i++) { if ("serialVersionUID".equals(fields[i].getName()) || "log".equals(fields[i].getName())) { continue; } infoMap = new HashMap<String, Object>(); infoMap.put("type", fields[i].getType().toString()); infoMap.put("name", fields[i].getName()); infoMap.put("value", getFieldValueByName(fields[i].getName(), o)); list.add(infoMap); } fields = o.getClass().getSuperclass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if ("serialVersionUID".equals(fields[i].getName()) || "log".equals(fields[i].getName()) || "url".equals(fields[i].getName()) || "cfcaMerCustId".equals(fields[i].getName())) { continue; } infoMap = new HashMap<String, Object>(); infoMap.put("type", fields[i].getType().toString()); infoMap.put("name", fields[i].getName()); infoMap.put("value", getFieldValueByName(fields[i].getName(), o)); list.add(infoMap); } fields = o.getClass().getSuperclass().getSuperclass() .getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if ("serialVersionUID".equals(fields[i].getName()) || "log".equals(fields[i].getName()) || "url".equals(fields[i].getName()) || "cfcaMerCustId".equals(fields[i].getName())) { continue; } infoMap = new HashMap<String, Object>(); infoMap.put("type", fields[i].getType().toString()); infoMap.put("name", fields[i].getName()); infoMap.put("value", getFieldValueByName(fields[i].getName(), o)); list.add(infoMap); } return list; } /** * 获取对象的所有属性值,返回一个对象数组 * @param o * @return */ public Object[] getFiledValues(Object o) { String[] fieldNames = this.getFiledName(o); Object[] value = new Object[fieldNames.length]; for (int i = 0; i < fieldNames.length; i++) { value[i] = this.getFieldValueByName(fieldNames[i], o); } return value; } }
以上是关于ObjectTools反射实例的主要内容,如果未能解决你的问题,请参考以下文章