集册 Java实例教程 备份Java Bean属性

备份Java Bean属性

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

540
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
备份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.HashMap;

import java.util.Map;


public class Main {

    public static void main(String[] argv) throws Exception {

        Object bean = "nowjava.com";

        System.out.println(backupProp(bean));

    }


    public static Map<String, Object> backupProp(Object bean) {

        Map<String, Object> result = new HashMap<String, Object>();

        try {
        /**来自 
         N o w  J a v a  .   c o m**/

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

            PropertyDescriptor[] descriptors = beanInfo

                    .getPropertyDescriptors();

            for (PropertyDescriptor des : descriptors) {

                String fieldName = des.getName();

                Method getter = des.getReadMethod();

                Object fieldValue = getter.invoke(bean, new Object[] {});

                if (!fi
展开阅读全文