您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。