Java反射通过Getter方法获取对象VO的属性值过程解析
这篇文章主要介绍了Java反射通过Getter方法获取对象VO的属性值过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
有时候,需要动态获取对象的属性值。
比如,给你一个List,要你遍历这个List的对象的属性,而这个List里的对象并不固定。比如,这次User,下次可能是Company。
e.g.这次我需要做一个Excel导出的工具类,导出的批量数据是以List类型传入的,List里的对象自然每次都不同,这取决于需要导出什么信息。
为了使用方便,将对象的属性名与属性值存于Map当中,使用时就可以直接遍历Map了。
此次的思路是通过反射和Getter方法取得值,然后记录在一个Map当中。
Kickstart...
将对象的属性名与属性值存于Map当中,以key,value的形式存在,而value并不希望以单一类型(如String)存在(因为涉及多种类型),所以用一个FieldEntity的自定义类(此类包含属性名,属性值,属性值类型等属性)
FieldEntity
packagecom.nicchagil.util.fields;
importjava.lang.reflect.Field;
importjava.lang.reflect.InvocationTargetException;
importjava.lang.reflect.Method;
importjava.util.HashMap;
importjava.util.Map;
publicclassFieldsCollector{
publicstaticMapgetFileds(Objectobject)
throwsSecurityException,IllegalArgumentException,NoSuchMethodException,
IllegalAccessException,InvocationTargetException{
Classclazz=object.getClass();
Field[]fields=clazz.getDeclaredFields();
Mapmap=newHashMap();
for(inti=0;i
主类,通过这个类的静态方法获取结果Map
FieldsCollector
packagecom.nicchagil.util.fields;
importjava.lang.reflect.Field;
importjava.lang.reflect.InvocationTargetException;
importjava.lang.reflect.Method;
importjava.util.HashMap;
importjava.util.Map;
publicclassFieldsCollector{
publicstaticMapgetFileds(Objectobject)
throwsSecurityException,IllegalArgumentException,NoSuchMethodException,
IllegalAccessException,InvocationTargetException{
Classclazz=object.getClass();
Field[]fields=clazz.getDeclaredFields();
Mapmap=newHashMap();
for(inti=0;i
为了代码清楚些,将一些工具方法独立一下,如fieldname到gettername的转换方法
GetterUtil
packagecom.nicchagil.util.fields;
publicclassGetterUtil{
/**
*Getgettermethodnamebyfieldname
*@paramfieldname
*@return
*/
publicstaticStringtoGetter(Stringfieldname){
if(fieldname==null||fieldname.length()==0){
returnnull;
}
/*Ifthesecondcharisupper,make'get'+fieldnameasgettername.Forexample,eBlog->geteBlog*/
if(fieldname.length()>2){
Stringsecond=fieldname.substring(1,2);
if(second.equals(second.toUpperCase())){
returnnewStringBuffer("get").append(fieldname).toString();
}
}
/*Commonsituation*/
fieldname=newStringBuffer("get").append(fieldname.substring(0,1).toUpperCase())
.append(fieldname.substring(1)).toString();
returnfieldname;
}
}
大功告成!!!
现在,写个VO作为模拟数据
User
importjava.util.Date;
publicclassUser{
privateStringusername;
privateStringpassword;
privateStringeBlog;
privateDateregistrationDate;
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgeteBlog(){
returneBlog;
}
publicvoidseteBlog(StringeBlog){
this.eBlog=eBlog;
}
publicDategetRegistrationDate(){
returnregistrationDate;
}
publicvoidsetRegistrationDate(DateregistrationDate){
this.registrationDate=registrationDate;
}
}
最后,测试类,此类将直接调用FieldsCollector~~
Call
importjava.util.Date;
importjava.util.Map;
importcom.nicchagil.util.fields.FieldEntity;
importcom.nicchagil.util.fields.FieldsCollector;
publicclassCall{
publicstaticvoidmain(String[]args)throwsException{
Useruser=newUser();
user.setUsername("user109");
user.setPassword("pwd109");
user.seteBlog("http://www.cnblogs.com/nick-huang/");
user.setRegistrationDate(newDate());
Mapmap=FieldsCollector.getFileds(user);
System.out.println(map);
}
}
Ohyear,成功了~~~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。