集册 Java实例教程 将Map转换为Java Bean

将Map转换为Java Bean

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

470
将Map转换为Java Bean


//package com.nowjava;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;/** 来 自 时代Java - nowjava.com**/

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


import java.util.Map;


public class Main {

    public static Object convertMap2Bean(Class type, Map map)

            throws IntrospectionException, IllegalAccessException,

            InstantiationException, InvocationTargetException {/*来 自 时 代 J a v a - N o w J a v a . c o m*/

        BeanInfo beanInfo = Introspector.getBeanInfo(type);

        Object obj = type.newInstance();

        PropertyDescriptor[] propertyDescriptors = beanInfo

                .getPropertyDescriptors();

        for (PropertyDescriptor pro : propertyDescriptors) {

            String propertyName = pro.getName();

            if (pro.getPropertyType().getName().equals("java.lang.Class")) {

                continue;

            }

            
展开阅读全文