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 
	* @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 
	* @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();
		}
	}
}

 

 

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值