// Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean public static <T> T transMap2Bean(Map<String, Object> map, T t) { try { BeanInfo beanInfo = Introspector.getBeanInfo(t.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (map.containsKey(key)) { Object value = map.get(key); // 得到property对应的setter方法 Method setter = property.getWriteMethod(); setter.invoke(t, value); } } } catch (Exception e) { System.out.println("transMap2Bean Error " + e); } return t;
java反射获取父类和本类属性并赋值
最新推荐文章于 2024-07-27 04:27:12 发布