集册 Java实例教程 设置给定bean中指定属性的值

设置给定bean中指定属性的值

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

713
设置给定bean中指定属性的值

/**

 * JLibs: Common Utilities for Java

 * Copyright (C) 2009  Santhosh Kumar T <santhosh.tekuri@gmail.com>

 *

 * This library is free software; you can redistribute it and/or

 * modify it under the terms of the GNU Lesser General Public

 * License as published by the Free Software Foundation; either

 * version 2.1 of the License, or (at your option) any later version.

 *

 * This library is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

 * Lesser General Public License for more details.

 */

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;

import java.util.Locale;/** 来 自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/


public class Main{

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

        Object bean = "nowjava.com";

        String property = "nowjava.com";

        Object value = "nowjava.com";

        setProperty(bean,property,value);

    }

    /** prefix used by non-boolean getter methods */

    public static final String GET = "get";

    /** prefix used by boolean getter methods */

    public static final String IS = "is";
    /*
    N o w J a v a . c o m
    */

    /** prefix used by setter methods */

    public static final String SET = "set";

    /**

     * Sets the value of the specified <code>property</code> in given <code>bean</code>

     *

     * @param bean      bean object

     * @param property  property name whose value needs to be set

     * @param value     value to be set

     *

     * @throws InvocationTargetException if method invocation fails

     * @throws NullPointerException if <code>property</code> is not found in <code>bean</code> or it is readonly property

     */

    public static void setProperty(Object bean, String property,

            Object value) throws InvocationTargetException {

        try {

            getSetterMethod(bean.getClass(), property).invoke(bean, value);

        } catch (IllegalAccessException ex) {

            throw new ImpossibleException(ex); // because setter method is public

        }

    }

    /**

     * Returns setter method for <code>property</code> in specified <code>beanClass</code>

     *

     * @param beanClass bean class

     * @param property  name of the property

     *

     * @return setter method. null if <code>property</code> is not found, or it is readonly property

     *

     * @see #getSetterMethod(Class, String, Class)

     */

    public static Method getSetterMethod(Class<?> beanClass, String property) {

        Method getter = getGetterMethod(beanClass, property);

        if (getter == null)

            return null;

        else

            return getSetterMethod(beanClass, property,

                    getter.getReturnType());

    }

    /**

     * Returns setter method for <code>property</code> in specified <code>beanClass</code>

     *

     * @param beanClass bean class

     * @param property  name of the property

     * @param propertyType type of the property. This is used to compute setter method name.

     *

     * @return setter method. null if <code>property</code> is not found, or it is readonly property

     *

     * @see #getSetterMethod(Class, String)

     */

    public static Method getSetterMethod(Class<?> beanClass,

            String property, Class propertyType) {

        try {

            return beanClass.getMethod(SET + getMethodSuffix(property),

                    propertyType);

        } catch (NoSuchMethodException ex) {

            return null;

        }

    }

    /**

     * Returns getter method for <code>property</code> in specified <code>beanClass</code>

     *

     * @param beanClass bean class

     * @param property  name of the property

     *

     * @return getter method. null if <code>property</code> is not found

     *

     * @see #getGetterMethod(Class, String, Class)

     */

    public static Method getGetterMethod(Class<?> beanClass, String property) {

        try {

            return beanClass.getMethod(GET + getMethodSuffix(property));

        } catch (NoSuchMethodException ex1) {

            try {

                return beanClass.getMethod(IS + getMethodSuffix(property));

            } catch (NoSuchMethodException ex2) {

                return null;

            }

        }

    }

    /**

     * Returns getter method for <code>property</code> in specified <code>beanClass</code>

     *

     * @param beanClass bean class

     * @param property  name of the property

     * @param propertyType type of the property. This is used to compute getter method name.

     *

     * @return getter method. null if <code>property</code> is not found

     *

     * @see #getGetterMethod(Class, String)

     */

    public static Method getGetterMethod(Class<?> beanClass,

            String property, Class propertyType) {

        try {

            try {

                if (propertyType == boolean.class)

                    return beanClass.getMethod(IS

                            + getMethodSuffix(property));

            } catch (NoSuchMethodException ignore) {

                // ignore

            }

            return beanClass.getMethod(GET + getMethodSuffix(property));

        } catch (NoSuchMethodException ex) {

            return null;

        }

    }

    public static String getMethodSuffix(String property) {

        switch (property.length()) {

        case 0:

            throw new IllegalArgumentException("invalid property name: "

                    + property);

       
展开阅读全文