Java利用反射机制过滤集合内对象的属性字段

直接上代码:


package com.demo.test;



import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import com.demo.po.EzsGoods;


public class FieldFilterUtil<T> {
/**
* 字段过滤 
* @author zhaibin
* @param list
* @param filterFields
* @param clazz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public List<T> getFieldFilterList(List<T> list,String filterFields,Class<T> clazz) 
throws InstantiationException,IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
List<T> tempList = new ArrayList<>();
if(list.size()<=0){
return tempList;
}
if(filterFields==null||filterFields.trim().equals("")){
return tempList;
}
//获取字段数组
Field[] fields = clazz.getDeclaredFields();
for (int i=0;i<list.size();i++) {
//创建对象
T ob = clazz.newInstance();
for (Field field : fields) {
//取消每个属性的安全检查 ,否則无法获取private字段值
field.setAccessible(true);  
//存在即保留(确定需要筛选出来的字段)
if(filterFields.indexOf(field.getName())<0)
continue;

/*//字段名称
field.getName();
//字段值
field.get(list.get(i));*/

//获取字段的属性
String type = field.getGenericType().toString();
Class typeTemp = getFieldType(type);

//获取属性名称
String name = field.getName();
String fieldName = name.substring(0, 1).toUpperCase()+name.substring(1,name.length());
//读取字段值,暂时不用
//Method methodGet = ob.getClass().getMethod("get"+fieldName);
//String value = (String) methodGet.invoke(ob);
//设置字段值
Method methodSet = ob.getClass().getMethod("set"+fieldName, typeTemp);
methodSet.invoke(ob,field.get(list.get(i)));
}
tempList.add(ob);
}
return tempList;
}
/**
* 返回字段类型
* @author zhaibin
* @param type
* @return
*/
public static Class getFieldType(String type){
Class typeTemp = null;
if(type.equals("class java.lang.String"))
typeTemp = String.class;
else if(type.equals("class java.lang.Integer"))
typeTemp = Integer.class;
else if(type.equals("class java.lang.Boolean"))
typeTemp = Boolean.class;
else if(type.equals("class java.util.Date"))
typeTemp = java.util.Date.class;
else if(type.equals("class java.lang.Double"))
typeTemp = Double.class;
else if(type.equals("class java.lang.Long"))
typeTemp = Long.class;
else
typeTemp = Object.class;
return typeTemp;
}

/**
* 测试类
* @param args
* @throws NoSuchMethodException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {


List<EzsGoods> userList = new ArrayList<>();
EzsGoods good01 = new EzsGoods();
EzsGoods good02 = new EzsGoods();
EzsGoods good03 = new EzsGoods();
good01.setName("zhang001");
good01.setAddess("河南");
good01.setAddtime(new Date());
good02.setName("zhang002");
good02.setAddess("北京");
good02.setAddtime(new Date());
good03.setName("zhang003");
good03.setAddess("罗亚");
good03.setAddtime(new Date());
userList.add(good01);
userList.add(good02);
userList.add(good03);

FieldFilterUtil<EzsGoods> fieldFilterUtil = new FieldFilterUtil<>();
String filterFields = "name,addess,addtime";
//String filterFields = "name";
try {
List<EzsGoods> ulist = fieldFilterUtil.getFieldFilterList(userList, filterFields, EzsGoods.class);
for (EzsGoods ezsGoods : ulist) {
System.out.println("过滤的结果:"+ezsGoods);
}

} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

猜你喜欢

转载自blog.csdn.net/weixin_42130892/article/details/80781760