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

恢复Java Bean属性

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

462
恢复Java Bean属性
/**
 * 时代Java - nowjava.com 提 供 
**/


//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());
            /* 
             来自 
            *时 代 J a v a 公 众 号*/

            PropertyDescriptor[] propertyDescriptors = beanInfo

                    .getPropertyDescriptors();

            for (PropertyDescriptor descriptor : propertyDescriptors) {

                String fieldName = descriptor.getName();

                if (propMap.containsKey(fieldName)) {

                    Method writeMethod = descriptor.getWriteMethod();

                    writeMethod.invoke(bean,

       
展开阅读全文