集册 Java实例教程 获取Java Bean简单属性写入方法

获取Java Bean简单属性写入方法

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

395
获取Java Bean简单属性写入方法
/*from 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.lang.reflect.Method;


public class Main {

    public static Method getSimplePropertyWriteMethod(Object bean,

            String name) throws IllegalArgumentException,/*来 自 n o w j a v a . c o m - 时  代  Java*/

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        return propertyDescriptor.getWriteMethod();

    }


    public static PropertyDescriptor getPropertyDescriptor(Object bean,

            String name) {

        PropertyDescriptor[] descriptors = getPropertyDescriptors(bean);

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

            if (name.equals(descriptors[i].getName()))

                return descriptors[i];

        }

        return null;

    }


    public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {

        BeanInfo beanInfo = null;

        try {

            beanInfo = 
展开阅读全文