您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Java反射通過Getter方法獲取對象VO的屬性值過程的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
比如,給你一個List,要你遍歷這個List的對象的屬性,而這個List里的對象并不固定。比如,這次User,下次可能是Company。
e.g. 這次我需要做一個Excel導出的工具類,導出的批量數據是以List類型傳入的,List里的對象自然每次都不同,這取決于需要導出什么信息。
為了使用方便,將對象的屬性名與屬性值存于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; }}
主類,通過這個類的靜態方法獲取結果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的轉換方法
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; }}
大功告成!!!
現在,寫個VO作為模擬數據
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; }}
最后,測試類,此類將直接調用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, 成功了~~~
關于“Java反射通過Getter方法獲取對象VO的屬性值過程的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。