集册 Java实例教程 将地图更改为Java Bean

将地图更改为Java Bean

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

532
将地图更改为Java Bean
/*
时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提 供
*/


//package com.nowjava;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;


import java.util.Map;


public class Main {

    /**

     * change a Map to a JavaBean

     *

     */

    public static Object convertMap(Class type, Map map)

            throws IntrospectionException, IllegalAccessException,

            InstantiationException, InvocationTargetException {

        BeanInfo beanInfo = Introspector.getBeanInfo(type);

        Object obj = type.newInstance();


        PropertyDescriptor[] propertyDescriptors = beanInfo
        /**
        来 自 N o w  J a v a  . c o m
        **/

                .getPropertyDescriptors();

        for (int i = 0; i < propertyDescriptors.length; i++) {

            PropertyDescriptor descriptor = propertyDescriptors[i];

            String propertyName = descriptor.getName();


            if (map.containsKey(propertyName)) {


                Object va
展开阅读全文