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.feilong.core.bean; import java.beans.PropertyDescriptor; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.BasicDynaClass; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.ConvertUtilsBean; import org.apache.commons.beanutils.DynaBean; import org.apache.commons.beanutils.DynaClass; import org.apache.commons.beanutils.DynaProperty; import org.apache.commons.lang3.Validate; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.feilong.test.User; import com.feilong.tools.jsonlib.JsonUtil; /** * The Class BeanUtilTest. * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> */ public class BeanUtilTemp { /** The Constant LOGGER. */ private static final Logger LOGGER = LoggerFactory.getLogger(BeanUtilTemp.class); /** * Describe. */ @Test public void describe() { User user = new User(); user.setId(5L); user.setDate(new Date()); LOGGER.debug(JsonUtil.format(describe(user))); } /** * Describe1. */ @Test public void describe1() { LOGGER.debug(JsonUtil.format(describe(null))); } // [start] describe BeanMap? /** * <code>bean</code>?(read method),??/Map. * * <h3>:</h3> * <blockquote> * * <pre class="code"> * User user = new User(); * user.setId(5L); * user.setDate(new Date()); * * LOGGER.info(JsonUtil.format(BeanUtil.describe(user))); * </pre> * * <b>:</b> * * <pre class="code"> * { * "id": "5", * "date": "Mon Apr 11 00:37:56 CST 2016" * } * </pre> * * </blockquote> * * <p> * ???class,Object??,classjava.lang.Object. * </p> * * <p> * <span style="color:red">:<br> * ConvertUtils.register(dateTimeConverter, java.util.Date.class)?</span><br> * * , {@link BeanUtilsBean#getNestedProperty(Object, String)}, ConvertUtilsBean?? <br> * {@link ConvertUtilsBean#ConvertUtilsBean()} ? * </p> * * @param bean * Bean whose properties are to be extracted * @return <code>bean</code> null, empty HashMap,see {@link BeanUtilsBean#describe(Object)} * @throws BeanUtilException * ,?{@link BeanUtilException} * @see org.apache.commons.beanutils.BeanUtils#describe(Object) * @see org.apache.commons.beanutils.PropertyUtils#describe(Object) * @see PropertyUtil#describe(Object, String...) * @see PropertyDescriptor * @see BeanUtil#populate(Object, Map) */ public static Map<String, String> describe(Object bean) { try { //Return the entire set of properties for which the specified bean provides a read method. return BeanUtils.describe(bean); } catch (Exception e) { throw new BeanUtilException(e); } } // [end] /** * dyna bean. * * <h3>:</h3> * * <blockquote> * * <pre class="code"> * * Map{@code <String, Class<?>>} typeMap = new HashMap<>(); * typeMap.put("address", java.util.Map.class); * typeMap.put("firstName", String.class); * typeMap.put("lastName", String.class); * * Map{@code <String, Object>} valueMap = new HashMap<>(); * valueMap.put("address", new HashMap()); * valueMap.put("firstName", "Fred"); * valueMap.put("lastName", "Flintstone"); * * DynaBean dynaBean = BeanUtil.newDynaBean(typeMap, valueMap); * LOGGER.debug(JsonUtil.format(dynaBean)); * * </pre> * * <b>:</b> * * <pre class="code"> * { * "lastName": "Flintstone", * "address": {}, * "firstName": "Fred" * } * </pre> * * </blockquote> * * @param typeMap * ?? map * @param valueMap * ?? map * @return the dyna bean * @throws NullPointerException * <code>typeMap </code> <code>valueMap</code> null * @throws IllegalArgumentException * <code>typeMap.size() != valueMap.size()</code> * @since 1.8.1 change name */ public static DynaBean newDynaBean(Map<String, Class<?>> typeMap, Map<String, Object> valueMap) { Validate.notNull(typeMap, "typeMap can't be null!"); Validate.notNull(valueMap, "valueMap can't be null!"); Validate.isTrue(typeMap.size() == valueMap.size(), "typeMap size:[%s] != valueMap size:[%s]", typeMap.size(), valueMap.size()); //********************************************************************************* try { DynaClass dynaClass = new BasicDynaClass(null, null, toDynaPropertyArray(typeMap)); DynaBean dynaBean = dynaClass.newInstance(); for (Map.Entry<String, Object> entry : valueMap.entrySet()) { dynaBean.set(entry.getKey(), entry.getValue()); } return dynaBean; } catch (IllegalAccessException | InstantiationException e) { LOGGER.error("", e); throw new BeanUtilException(e); } } /** * To dyna property array. * * @param typeMap * the type map * @return the dyna property[] * @since 1.8.0 */ private static DynaProperty[] toDynaPropertyArray(Map<String, Class<?>> typeMap) { DynaProperty[] dynaPropertys = new DynaProperty[typeMap.size()]; int i = 0; for (Map.Entry<String, Class<?>> entry : typeMap.entrySet()) { dynaPropertys[i] = new DynaProperty(entry.getKey(), entry.getValue()); i++; } return dynaPropertys; } @Test(expected = NullPointerException.class) public void testBasicDynaClass1() { Map<String, Object> valueMap = new HashMap<>(); valueMap.put("address", new HashMap()); valueMap.put("firstName", "Fred"); valueMap.put("lastName", "Flintstone"); newDynaBean(null, valueMap); } @Test(expected = NullPointerException.class) public void testBasicDynaClass2() { Map<String, Class<?>> typeMap = new HashMap<>(); typeMap.put("address", java.util.Map.class); typeMap.put("firstName", String.class); typeMap.put("lastName", String.class); newDynaBean(typeMap, null); } @Test(expected = IllegalArgumentException.class) public void testBasicDynaClass4() { Map<String, Class<?>> typeMap = new HashMap<>(); typeMap.put("address", java.util.Map.class); typeMap.put("firstName", String.class); typeMap.put("lastName", String.class); Map<String, Object> valueMap = new HashMap<>(); valueMap.put("address", new HashMap()); valueMap.put("firstName", "Fred"); newDynaBean(typeMap, valueMap); } @Test public void testBasicDynaClass() { Map<String, Class<?>> typeMap = new HashMap<>(); typeMap.put("address", java.util.Map.class); typeMap.put("firstName", String.class); typeMap.put("lastName", String.class); Map<String, Object> valueMap = new HashMap<>(); valueMap.put("address", new HashMap()); valueMap.put("firstName", "Fred"); valueMap.put("lastName", "Flintstone"); DynaBean dynaBean = newDynaBean(typeMap, valueMap); LOGGER.debug(JsonUtil.format(dynaBean)); } }