InstanceUtil.java 文件源码

java
阅读 26 收藏 0 点赞 0 评论 0

项目:iBase4J-Common 作者:
public static <T> T transMap2Bean(Map<String, Object> map, Class<T> clazz) {
    T bean = null;
    try {
        bean = clazz.newInstance();
        BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
        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(bean, TypeParseUtil.convert(value, property.getPropertyType(), null));
            }
        }
    } catch (Exception e) {
        logger.error("transMap2Bean Error ", e);
    }
    return bean;
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号