Java如何獲取對象屬性及對應(yīng)值
利用反射獲取對象的所有屬性及對應(yīng)的值
1、獲取屬性名數(shù)組
private static String[] getFiledName(Object o) { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { fieldNames[i] = fields[i].getName(); } return fieldNames; }
2、根據(jù)屬性名獲取屬性值
private static 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) { logger.error('獲取屬性值失敗!' + e, e); } return null; }
3、獲取屬性的數(shù)據(jù)類型
private static Object getFiledType(String fieldName, Object o) { Field[] fields = o.getClass().getDeclaredFields(); for (Field field : fields) { if (Objects.equals(fieldName, field.getName())) {return field.getType(); } } return null; }
4、完整代碼及其引入的包
package com.hao.search;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Objects;import org.apache.commons.collections.CollectionUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.google.common.base.Joiner;public class ObjectPaseUtils { private static Logger logger = LoggerFactory.getLogger(ObjectPaseUtils.class); /** * @desc 將對象轉(zhuǎn)換成指定String * @param <T> * @param t * @return */ public static <T> String objectToStr(T t) { List<String> list = new ArrayList<String>(); String[] fieldNames = getFiledName(t); for (int i = 0; i < fieldNames.length; i++) { String name = fieldNames[i]; Object value = getFieldValueByName(name, t); if (null != value) {if (getFiledType(name, t).equals(Date.class)) { value = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format((Date)value);}list.add(name + ' : ' + value); } } if (CollectionUtils.isNotEmpty(list)) { return Joiner.on('r').skipNulls().join(list); } return null; } /** * @desc 獲取屬性名數(shù)組 * @param o * @return */ private static String[] getFiledName(Object o) { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { fieldNames[i] = fields[i].getName(); } return fieldNames; } /** * @desc 根據(jù)屬性名獲取屬性值 * @param fieldName * @param o * @return */ private static 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) { logger.error('獲取屬性值失敗!' + e, e); } return null; } /** * @desc 獲取屬性的數(shù)據(jù)類型 * @param fieldName * @param o * @return */ private static Object getFiledType(String fieldName, Object o) { Field[] fields = o.getClass().getDeclaredFields(); for (Field field : fields) { if (Objects.equals(fieldName, field.getName())) {return field.getType(); } } return null; }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何對php程序中的常見漏洞進(jìn)行攻擊2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. jsp EL表達(dá)式詳解4. Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法5. JavaWeb Servlet中url-pattern的使用6. Ajax提交post請求案例分析7. Ajax請求超時(shí)與網(wǎng)絡(luò)異常處理圖文詳解8. ASP中格式化時(shí)間短日期補(bǔ)0變兩位長日期的方法9. JSP之表單提交get和post的區(qū)別詳解及實(shí)例10. ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)
