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

备份Java Bean属性

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

447
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
备份Java Bean属性


//package com.nowjava;

import java.beans.BeanInfo;//NowJava.com - 时  代  Java 提 供

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<>();

        try {

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

            PropertyDescriptor[] propertyDescriptors = beanInfo
            /**
            来 自 时 代 J a v a - nowjava.com
            **/

                    .getPropertyDescriptors();

            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {

                String fieldName = propertyDescriptor.getName();

                Method readMethod = propertyDescriptor.getReadMethod();

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

                
展开阅读全文