Java tutorial
/* * Copyright (C) 2008 feilong * * 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.discovery.darchrow.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 feilong * @version 1.0.8 2014-7-21 17:45:30 * @see org.apache.commons.beanutils.PropertyUtils * @see com.baozun.nebulaplus.bean.BeanUtil */ public final class PropertyUtil { /** The Constant LOGGER. */ private static final Logger LOGGER = 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!"); } /** * <code>bean</code>???/Map. * * <p> * ???classObject??classjava.lang.Object * </p> * * @param bean * Bean whose properties are to be extracted * @return The set of properties for the bean * @see org.apache.commons.beanutils.BeanUtils#describe(Object) * @see org.apache.commons.beanutils.PropertyUtils#describe(Object) * @see com.baozun.nebulaplus.bean.BeanUtil#describe(Object) */ public static Map<String, Object> describe(Object bean) { 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) { LOGGER.error(e.getClass().getName(), e); throw new BeanUtilException(e); } } /** * {@link PropertyUtils#setProperty(Object, String, Object)} ?(<b>??</b>). * * <pre> * BeanUtils.setProperty(pt1, "x", "9"); // 9String * PropertyUtils.setProperty(pt1, "x", 9); // int * // BeanUtilsPropertyUtils,?int?? * </pre> * * * <pre> * {@code * getPropertysetProperty,?2?JavaBean????. * Company c = new Company(); * c.setName("Simple"); * * Simple????? * //Simple * LOGGER.debug(BeanUtils.getProperty(c, "name")); * * Map???key?? * //Map * LOGGER.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); * LOGGER.debug(BeanUtils.getProperty(c, "telephone (2)")); * * Indexed??[]??ArrayList???. * //index * LOGGER.debug(BeanUtils.getProperty(c, "otherInfo[2]")); * BeanUtils.setProperty(c, "product[1]", "NOTES SERVER"); * LOGGER.debug(BeanUtils.getProperty(c, "product[1]")); * * 3???? * //nest * LOGGER.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 * @see org.apache.commons.beanutils.BeanUtils#setProperty(Object, String, Object) * @see org.apache.commons.beanutils.PropertyUtils#setProperty(Object, String, Object) * @see com.baozun.nebulaplus.bean.BeanUtil#setProperty(Object, String, Object) */ public static void setProperty(Object bean, String name, Object value) { 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) { LOGGER.error(e.getClass().getName(), e); throw new BeanUtilException(e); } } // [start] getProperty /** * {@link PropertyUtils#getProperty(Object, String)} ?. * * <h3>{@link BeanUtils#getProperty(Object, String) BeanUtils.getProperty}& {@link PropertyUtils#getProperty(Object, String) * PropertyUtils.getProperty}:</h3> * * <blockquote> * <p> * {@link BeanUtils#getProperty(Object, String)} ?String,<br> * {@link PropertyUtils#getProperty(Object, String)} Object,??? * </p> * </blockquote> * * * <h3>:</h3> * * <pre> * {@code * getPropertysetProperty,?2?JavaBean????. * Company c = new Company(); * c.setName("Simple"); * * Simple????? * //Simple * LOGGER.debug(BeanUtils.getProperty(c, "name")); * * Map???key?? * //Map * LOGGER.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); * LOGGER.debug(BeanUtils.getProperty(c, "telephone (2)")); * * Indexed??[]??ArrayList???. * //index * LOGGER.debug(BeanUtils.getProperty(c, "otherInfo[2]")); * BeanUtils.setProperty(c, "product[1]", "NOTES SERVER"); * LOGGER.debug(BeanUtils.getProperty(c, "product[1]")); * * 3???? * //nest * LOGGER.debug(BeanUtils.getProperty(c, "employee[1].name")); * * } * </pre> * * @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)} ? * @see com.baozun.nebulaplus.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) { //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) { LOGGER.error(e.getClass().getName(), e); throw new BeanUtilException(e); } } // [end] }