集册 Java实例教程 恢复Java Bean属性

恢复Java Bean属性

欢马劈雪     最近更新时间:2020-01-02 10:19:05

437
恢复Java Bean属性
/*nowjava 提 供*/


//package com.nowjava;

import java.beans.BeanInfo;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Method;


import java.util.Map;


public class Main {

    public static void restoreProp(Object bean, Map<String, Object> propMap) {

        try {

            BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());

            PropertyDescriptor[] descriptors = beanInfo

                    .getPropertyDescriptors();

            for (PropertyDescriptor des : descriptors) {/** 时   代    Java - nowjava.com 提 供 **/

                String fieldName = des.getName();

                if (propMap.containsKey(fieldName)) {

                    Method setter = des.getWriteMethod();

                    setter.invoke(bean,

                       
展开阅读全文