com.feilong.commons.core.bean.PropertyUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.commons.core.bean.PropertyUtil.java

Source

/*
 * Copyright (C) 2008 feilong (venusdrogon@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.commons.core.bean;

import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ? {@link PropertyUtils}.
 * 
 * <h3>{@link PropertyUtils} {@link BeanUtils}:</h3>
 * 
 * <blockquote>
 * <p>
 * {@link PropertyUtils} {@link BeanUtils}????<br>
 * BeanUtils??"Bean"String,<br>
 * PropertyUtils??Object
 * </p>
 * </blockquote>
 * 
 * @author <a href="mailto:venusdrogon@163.com">feilong</a>
 * @version 1.0.8 2014-7-21 17:45:30
 * @see org.apache.commons.beanutils.PropertyUtils
 * @see com.feilong.commons.core.bean.BeanUtil
 */
public final class PropertyUtil {

    /** The Constant log. */
    private static final Logger log = LoggerFactory.getLogger(PropertyUtil.class);

    /** Don't let anyone instantiate this class. */
    private PropertyUtil() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * <p>
     * <code>bean</code>???/Map.
     * </p>
     * 
     * ???classObject??classjava.lang.Object
     * 
     * @param bean
     *            Bean whose properties are to be extracted
     * @return The set of properties for the bean
     * @throws BeanUtilException
     *             if IllegalAccessException | InvocationTargetException | NoSuchMethodException
     * @see org.apache.commons.beanutils.BeanUtils#describe(Object)
     * @see org.apache.commons.beanutils.PropertyUtils#describe(Object)
     * @see com.feilong.commons.core.bean.BeanUtil#describe(Object)
     */
    public static Map<String, Object> describe(Object bean) throws BeanUtilException {
        try {
            //Return the entire set of properties for which the specified bean provides a read method.
            Map<String, Object> propertyMap = PropertyUtils.describe(bean);
            return propertyMap;
        } catch (Exception e) {
            log.error(e.getClass().getName(), e);
            throw new BeanUtilException(e);
        }
    }

    /**
     *  {@link PropertyUtils#setProperty(Object, String, Object)} ?(<b>??</b>).
     * 
     * <pre>
     * 
     * BeanUtils.setProperty(pt1, &quot;x&quot;, &quot;9&quot;); // 9String
     * PropertyUtils.setProperty(pt1, &quot;x&quot;, 9); // int
     * // BeanUtilsPropertyUtils,?int??
     * </pre>
     * 
     * 
     * <pre>
     * {@code
     * getPropertysetProperty,?2?JavaBean????.
     * Company c = new Company();
     * c.setName("Simple");
     * 
     * Simple?????
     * //Simple
     * log.debug(BeanUtils.getProperty(c, "name"));
     * 
     * Map???key??
     * //Map
     *     log.debug(BeanUtils.getProperty(c, "address (A2)"));
     *     HashMap am = new HashMap();
     *     am.put("1","234-222-1222211");
     *     am.put("2","021-086-1232323");
     *     BeanUtils.setProperty(c,"telephone",am);
     * log.debug(BeanUtils.getProperty(c, "telephone (2)"));
     * 
     * Indexed??[]??ArrayList???.
     * //index
     *     log.debug(BeanUtils.getProperty(c, "otherInfo[2]"));
     *     BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");
     *     log.debug(BeanUtils.getProperty(c, "product[1]"));
     * 
     * 3????
     * //nest
     *     log.debug(BeanUtils.getProperty(c, "employee[1].name"));
     * 
     * }
     * </pre>
     * 
     * @param bean
     *            Bean whose property is to be modified
     * @param name
     *            Possibly indexed and/or nested name of the property to be modified
     * @param value
     *            Value to which this property is to be set
     * @throws BeanUtilException
     *             if IllegalAccessException | InvocationTargetException | NoSuchMethodException
     * @see org.apache.commons.beanutils.BeanUtils#setProperty(Object, String, Object)
     * @see org.apache.commons.beanutils.PropertyUtils#setProperty(Object, String, Object)
     * @see com.feilong.commons.core.bean.BeanUtil#setProperty(Object, String, Object)
     */
    public static void setProperty(Object bean, String name, Object value) throws BeanUtilException {
        try {
            //Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

            // PropertyUtilsBeanUtils,?????
            PropertyUtils.setProperty(bean, name, value);
        } catch (Exception e) {
            log.error(e.getClass().getName(), e);
            throw new BeanUtilException(e);
        }
    }

    // [start] getProperty

    /**
     *  {@link PropertyUtils#getProperty(Object, String)} ?.
     * 
     * <pre>
     * {@code
     * getPropertysetProperty,?2?JavaBean????.
     * Company c = new Company();
     * c.setName("Simple");
     * 
     * Simple?????
     * //Simple
     * log.debug(BeanUtils.getProperty(c, "name"));
     * 
     * Map???key??
     * //Map
     *     log.debug(BeanUtils.getProperty(c, "address (A2)"));
     *     HashMap am = new HashMap();
     *     am.put("1","234-222-1222211");
     *     am.put("2","021-086-1232323");
     *     BeanUtils.setProperty(c,"telephone",am);
     * log.debug(BeanUtils.getProperty(c, "telephone (2)"));
     * 
     * Indexed??[]??ArrayList???.
     * //index
     *     log.debug(BeanUtils.getProperty(c, "otherInfo[2]"));
     *     BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");
     *     log.debug(BeanUtils.getProperty(c, "product[1]"));
     * 
     * 3????
     * //nest
     *     log.debug(BeanUtils.getProperty(c, "employee[1].name"));
     * 
     * }
     * </pre>
     * 
     * <h3>{@link BeanUtils#getProperty(Object, String)}&{@link PropertyUtils#getProperty(Object, String)}</h3>
     * 
     * <blockquote>
     * <p>
     * {@link BeanUtils#getProperty(Object, String)} ?String,<br>
     * {@link PropertyUtils#getProperty(Object, String)} Object,???
     * </p>
     * </blockquote>
     * 
     * @param <T>
     *            the generic type
     * @param bean
     *            Bean whose property is to be extracted
     * @param name
     *            Possibly indexed and/or nested name of the property to be extracted
     * 
     * @return {@link PropertyUtils#getProperty(Object, String)} ?
     * @throws BeanUtilException
     *             if IllegalAccessException | InvocationTargetException | NoSuchMethodException
     * @see com.feilong.commons.core.bean.BeanUtil#getProperty(Object, String)
     * @see org.apache.commons.beanutils.BeanUtils#getProperty(Object, String)
     * @see org.apache.commons.beanutils.PropertyUtils#getProperty(Object, String)
     * @see org.apache.commons.beanutils.PropertyUtilsBean
     */
    public static <T> T getProperty(Object bean, String name) throws BeanUtilException {
        //Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
        //For more details see PropertyUtilsBean.
        try {
            @SuppressWarnings("unchecked")
            T propertyValue = (T) PropertyUtils.getProperty(bean, name);
            return propertyValue;
        } catch (Exception e) {
            log.error(e.getClass().getName(), e);
            throw new BeanUtilException(e);
        }
    }
    // [end]
}