Object对象转换成MAP

private Map<String, Object> beanToMap throws Exception {
		//使用Introspector将实体对象转换成map
		if (item == null)
			return null;
		GzWhiteListInfoItem gzWhiteListInfoItem = item.getLineObject();
		Map<String, Object> map = new HashMap<String, Object>();
		BeanInfo beanInfo = Introspector.getBeanInfo(gzWhiteListInfoItem.getClass());
		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor property : propertyDescriptors) {
			String key = property.getName();
			//过滤class属性
			if (key.compareToIgnoreCase("class") == 0) {
				continue;
			}
			Method getter = property.getReadMethod();
			Object value = getter != null ? getter.invoke(gzWhiteListInfoItem) : null;
			map.put(key, value);
			
			//取注解(CChar自己定义的注解@CChar(value=3))
			CChar annoChar = GzWhiteListInfoItem.class.getField(key).getAnnotation(CChar.class);
			//下面的是取注解的长度和传参字段长度的比较(做笔记用,大家可以忽略),好处是在一个for循环里面可以比较所有的字段长度,不用一个个比较
			//取注解的长度annoChar.value(); 解析的长度:map.get(key).toString().length()
			if(annoChar != null && annoChar.value() < map.get(key).toString().length()) {
				//做相应的逻辑处理。。。。。
			}
		}
		return map;
	}			
    原文作者:Catch_error
    原文地址: https://blog.csdn.net/qq_40825071/article/details/80076691
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞