java动态去掉对象的值为空

package com.jzlife.nurse.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.jzlife.nurse.entity.servant.dto.MatchingDto;

public class EntityToList {

	/**
	 * List<String> entiyList =EntityToList.getList(VBonusReportStore.class);
	 * 
	 * @param c
	 * @return
	 */
	public static List<String> getList(Class<?> c) {

		return getList(c, null);
	}

	/**
	 * type:如String ,double,int...
	 * 
	 * @param c
	 * @param type
	 * @return
	 */
	public static List<String> getList(Class<?> c, String type) {
		List<String> entiyList = new ArrayList<String>();

		Field[] fields = c.getDeclaredFields();

		for (Field field : fields) {
			field.setAccessible(true);

			try {
				if (type != null) {
					String name = field.getName();
					if (name.indexOf("ajc$tjp") < 0 && name.indexOf("entityManager") < 0) {
						String myType = field.getType().toString();
						if (myType.contains(type)) {
							entiyList.add(field.getName());
						}
					}
				} else {
					String name = field.getName();
					if (name.indexOf("ajc$tjp") < 0 && name.indexOf("entityManager") < 0) {
						entiyList.add(field.getName());
					}
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return entiyList;
	}

	public static String getString(Object o, Class<?> c) {
		String result = c.getSimpleName() + ":";

		// 获取父类,判断是否为实体类
		if (c.getSuperclass().getName().indexOf("entity") >= 0) {
			result += "\n<" + getString(o, c.getSuperclass()) + ">,\n";
		}

		// 获取类中的所有定义字段
		Field[] fields = c.getDeclaredFields();

		// 循环遍历字段,获取字段对应的属性值
		for (Field field : fields) {
			// 如果不为空,设置可见性,然后返回
			field.setAccessible(true);

			try {
				// 设置字段可见,即可用get方法获取属性值。
				result += field.getName() + "=" + field.get(o) + ",\n";
			} catch (Exception e) {
				// System.out.println("error--------"+methodName+".Reason is:"+e.getMessage());
			}
		}
		if (result.indexOf(",") >= 0)
			result = result.substring(0, result.length() - 2);
		return result;
	}

	 

	/**
	 * 获取对象中字段的值
	 * 
	 * @param cs    对象
	 * @param filed 字段
	 * @return
	 * @throws Exception
	 */
	public static Object getFieldValue(Object cs, String filed) throws Exception {
		if (null == filed) {
			return null;
		}
		Field name = cs.getClass().getDeclaredField(filed);
		name.setAccessible(true);
		Object object = name.get(cs);
		name.setAccessible(false);
		return object;
	}


	/**
	 * 获取对象中有值的字段
	 * 
	 * @param cs 对象
	 * @throws Exception
	 */
	public static List<String> getValueField(Object cs) throws Exception {
		List<String> entiyList = EntityToList.getList(cs.getClass());
		List<String> valueField = new ArrayList<String>();
		for (String s : entiyList) {
			Object value = getFieldValue(cs, s);
			if (value != null) {
				valueField.add(s);
			}
		}

		return valueField;
	}
	
	/**
	 * 去掉对象中的值,不能有int类型
	 * 
	 * @param cs    对象
	 * @param filed 要去掉值的字段
	 * @return
	 * @return
	 * @throws Exception
	 */
	public static void cleanFieldValue(Object cs, String filed) throws Exception {
		if(null!=filed) {
			Field name = cs.getClass().getDeclaredField(filed);
			name.setAccessible(true);
			name.set(cs,null);
			name.setAccessible(false);
		}
	}
	
	
	public static void main(String[] args) throws Exception {
		MatchingDto md=new MatchingDto();
		md.setCountSize(100);
		md.setServiceType("呵呵");
		md.setPageNum(20);
		md.setPageSize(25);
		List<String> valueField = getValueField(md);//对象中哪些有值
		for(String s:valueField) {
			cleanFieldValue(md,s);//去掉
			System.out.println(s);
		}
	}
	

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值