這篇文章將為大家詳細講解有關(guān)Java反射通過Getter方法獲取對象VO的屬性值過程的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)主營秭歸網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),秭歸h5微信小程序開發(fā)搭建,秭歸網(wǎng)站營銷推廣歡迎秭歸等地區(qū)企業(yè)咨詢
比如,給你一個List,要你遍歷這個List的對象的屬性,而這個List里的對象并不固定。比如,這次User,下次可能是Company。
e.g. 這次我需要做一個Excel導(dǎo)出的工具類,導(dǎo)出的批量數(shù)據(jù)是以List類型傳入的,List里的對象自然每次都不同,這取決于需要導(dǎo)出什么信息。
為了使用方便,將對象的屬性名與屬性值存于Map當中,使用時就可以直接遍歷Map了。
此次的思路是通過反射和Getter方法取得值,然后記錄在一個Map當中。
Kick start...
將對象的屬性名與屬性值存于Map當中,以key,value的形式存在,而value并不希望以單一類型(如String)存在(因為涉及多種類型),所以用一個FieldEntity的自定義類(此類包含屬性名,屬性值,屬性值類型 等屬性)
FieldEntity
package com.nicchagil.util.fields;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class FieldsCollector { public static Map<String, FieldEntity> getFileds(Object object) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); Map<String, FieldEntity> map = new HashMap<String, FieldEntity> (); for (int i = 0; i < fields.length; i++) { Object resultObject = invokeMethod(object, fields[i].getName(), null); map.put(fields[i].getName(), new FieldEntity(fields[i].getName(), resultObject, fields[i].getType())); } return map; } public static Object invokeMethod(Object owner, String fieldname, Object[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class ownerClass = owner.getClass(); Method method = null; method = ownerClass.getMethod(GetterUtil.toGetter(fieldname)); Object object = null; object = method.invoke(owner); return object; }}
主類,通過這個類的靜態(tài)方法獲取結(jié)果Map
FieldsCollector
package com.nicchagil.util.fields;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;public class FieldsCollector { public static Map<String, FieldEntity> getFileds(Object object) throws SecurityException, IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { Class clazz = object.getClass(); Field[] fields = clazz.getDeclaredFields(); Map<String, FieldEntity> map = new HashMap<String, FieldEntity> (); for (int i = 0; i < fields.length; i++) { Object resultObject = invokeMethod(object, fields[i].getName(), null); map.put(fields[i].getName(), new FieldEntity(fields[i].getName(), resultObject, fields[i].getType())); } return map; } public static Object invokeMethod(Object owner, String fieldname, Object[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class ownerClass = owner.getClass(); Method method = null; method = ownerClass.getMethod(GetterUtil.toGetter(fieldname)); Object object = null; object = method.invoke(owner); return object; }}
為了代碼清楚些,將一些工具方法獨立一下,如field name到getter name的轉(zhuǎn)換方法
GetterUtil
package com.nicchagil.util.fields;public class GetterUtil { /** * Get getter method name by field name * @param fieldname * @return */ public static String toGetter(String fieldname) { if (fieldname == null || fieldname.length() == 0) { return null; } /* If the second char is upper, make 'get' + field name as getter name. For example, eBlog -> geteBlog */ if (fieldname.length() > 2) { String second = fieldname.substring(1, 2); if (second.equals(second.toUpperCase())) { return new StringBuffer("get").append(fieldname).toString(); } } /* Common situation */ fieldname = new StringBuffer("get").append(fieldname.substring(0, 1).toUpperCase()) .append(fieldname.substring(1)).toString(); return fieldname; }}
大功告成?。?!
現(xiàn)在,寫個VO作為模擬數(shù)據(jù)
User
import java.util.Date;public class User { private String username; private String password; private String eBlog; private Date registrationDate; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String geteBlog() { return eBlog; } public void seteBlog(String eBlog) { this.eBlog = eBlog; } public Date getRegistrationDate() { return registrationDate; } public void setRegistrationDate(Date registrationDate) { this.registrationDate = registrationDate; }}
最后,測試類,此類將直接調(diào)用FieldsCollector~~
Call
import java.util.Date;import java.util.Map;import com.nicchagil.util.fields.FieldEntity;import com.nicchagil.util.fields.FieldsCollector;public class Call { public static void main(String[] args) throws Exception { User user = new User(); user.setUsername("user109"); user.setPassword("pwd109"); user.seteBlog("http://www.cnblogs.com/nick-huang/"); user.setRegistrationDate(new Date()); Map<String, FieldEntity> map = FieldsCollector.getFileds(user); System.out.println(map); }}
Oh year, 成功了~~~
關(guān)于“Java反射通過Getter方法獲取對象VO的屬性值過程的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
文章題目:Java反射通過Getter方法獲取對象VO的屬性值過程的示例分析
轉(zhuǎn)載注明:http://jinyejixie.com/article24/igogce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計、網(wǎng)站改版、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)