Smail_
2016-07-01 09:21:53
java Object对象通过反射转成Map
/** * 将Object对象转成Map<String,Object> String 对象属性名,Object 对象属性值 * @param object * @author yangtao * @return */ public static Map<String,Object> getAllProperties(Object target) { Map<String, Object> map = new HashMap<>(); Assert.notNull(target, "Target must not be null"); Class<?> actualEditable = target.getClass(); PropertyDescriptor[] targetPds = getPropertyDescriptors(actualEditable); for (PropertyDescriptor targetPd : targetPds) { if (targetPd.getReadMethod()!=null) { try { Method readMethod = targetPd.getReadMethod(); if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(target); if (value != null) { map.put(targetPd.getName(), value); } } catch (Exception e) { e.printStackTrace(); } } } return map; }
评论
最近浏览
wangxinshui LV1
2022年1月15日
sulei1992 LV2
2021年12月13日
heshiyang LV1
2021年8月4日
xtd-2018
2021年6月18日
暂无贡献等级
nbvnnv LV1
2021年4月14日
小叶SFSDAFSA LV3
2020年7月2日
531190177 LV1
2020年2月21日
jxzzk001
2020年1月4日
暂无贡献等级
qiuxu123 LV2
2019年11月14日
xiaxige LV1
2019年9月9日