集册 Java实例教程 返回给定bean的给定属性的值。

返回给定bean的给定属性的值。

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

416
返回给定bean的给定属性的值。

/*******************************************************************************

 * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com>, Pascal Leclercq <pascal.leclercq@gmail.com>

 * All rights reserved. This program and the accompanying materials

 * are made available under the terms of the Eclipse Public License v1.0

 * which accompanies this distribution, and is available at

 * http://www.eclipse.org/legal/epl-v10.html

 *

 * Contributors:

 *     Angelo ZERR - initial API and implementation

 *     Pascal Leclercq - initial API and implementation

 *******************************************************************************//*时 代 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.lang.reflect.Method;

import java.util.ArrayList;

import java.util.List;


public class Main {


    /**

     * Returns the value of the given property for the given bean.

     * 

     * @param source

     *            the source bean

     * @param property

     *            the property name to retrieve

     * @return the value of the given property for the given bean.

     */

    public static Object getValue(Object source, String property) {

        if (property == null) {

            return source;

        }
        /**
         * n o w    j a v a  . c o m 提 供 
        **/

        if (property.indexOf('.') == -1) {

            if (source == null) {

                return null;

            }

            PropertyDescriptor propertyDescriptor = getPropertyDescriptor(

                    source.getClass(), property);

            return getValue(source, propertyDescriptor);

        }


        String[] properies = property.split("[.]");

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

            source = getValue(source, properies[i]);

        }

        return source;

    }


    /**

     * Returns the value of the given property for the given bean.

     * 

     * @param source

     *            the source bean

     * @param propertyDescriptor

     *            the property to retrieve

     * @return the contents of the given property for the given bean.

     */

    private static Object getValue(Object source,

            PropertyDescriptor propertyDescriptor) {

        try {

            Method readMethod = propertyDescriptor.getReadMethod();

            if (readMethod == null) {

                throw new IllegalArgumentException(

                        propertyDescriptor.getName()

                                + " property does not have a read method."); //$NON-NLS-1$

            }

            if (!readMethod.isAccessible()) {

                readMethod.setAccessible(true);

            }

            return readMethod.invoke(source, null);

        } catch (InvocationTargetException e) {

            /*

             * InvocationTargetException wraps any exception thrown by the

             * invoked method.

             */

            throw new RuntimeException(e.getCause());

        } catch (Exception e) {

            return null;

        }

    }


    /**

     * Returns the property descriptor of the given bean class and the given

     * property.

     * 

     * @param beanClass

     * @param propertyName

     * @return the PropertyDescriptor for the named property on the given bean

     *         class

     */

    private static PropertyDescriptor getPropertyDescriptor(

            Class beanClass, String propertyName) {

        if (!beanClass.isInterface()) {

            BeanInfo beanInfo;

            try {

                beanInfo = Introspector.getBeanInfo(beanClass);

            } catch (IntrospectionException e) {

                // cannot introspect, give up

                return null;

            }

            PropertyDescriptor[] propertyDescriptors = beanInfo

                    .getPropertyDescriptors();

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

                PropertyDescriptor descriptor = propertyDescriptors[i];

                if (descriptor.getName().equals(propertyName)) {

                    return descriptor;

                }

            }

        } else {

            try {

                PropertyDescriptor propertyDescriptors[];

                List pds = new ArrayList();

                getInterfacePropertyDescriptors(pds, beanClass);

                if (pds.size() > 0) {

                    propertyDescriptors = (PropertyDescriptor[]) pds

                            .toArray(new PropertyDescriptor[pds.size()]);

                    PropertyDescriptor descriptor;

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

                        descriptor = propertyDescriptors[i];

                        if (descriptor.getName().equals(propertyName))

                            return descriptor;

                    }

                }

            } catch (IntrospectionException e) {

                // cannot introspect, give up

                return null;

            }

        }

        throw new IllegalArgumentException(

                "Could not find property with name " + propertyName + " in class " + beanClass); //$NON-NLS-1$ //$NON-NLS-2$

    }


    
展开阅读全文