1. import java.beans.BeanInfo;
  2. import java.beans.IntrospectionException;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. import com.google.common.base.Preconditions;
  11. import com.google.common.collect.Lists;
  12.  
  13. public abstract class AbstractBuilder<T> {
  14.  
  15.     private final Class<T> beanClass;
  16.     private final BeanInfo beanInfo;
  17.     private final Map<String, Object> propertyMap;
  18.  
  19.     /**
  20.      * {@link java.lang.Object Object}
  21.      *
  22.      * @param beanClass
  23.      */
  24.     protected AbstractBuilder(final Class<T> beanClass){
  25.         super();
  26.         this.beanClass = beanClass;
  27.         try{
  28.             this.beanInfo = Introspector.getBeanInfo(beanClass);
  29.             this.propertyMap = new HashMap<String, Object>();
  30.         } catch(final IntrospectionException e){
  31.             throw new IllegalStateException(e);
  32.         }
  33.     }
  34.  
  35.     public final T build(){
  36.         try{
  37.             final T bean = this.beanClass.newInstance();
  38.             for(final PropertyDescriptor descriptor : this.beanInfo
  39.                 .getPropertyDescriptors()){
  40.                 final String propertyName = descriptor.getName();
  41.                 if(this.propertyMap.containsKey(propertyName)){
  42.                     descriptor.getWriteMethod()//
  43.                         .invoke(bean, this.propertyMap.get(propertyName));
  44.                 }
  45.             }
  46.  
  47.             return bean;
  48.  
  49.             // CATCH:OFF
  50.         } catch(final RuntimeException e){
  51.             // CATCH:ON
  52.             throw new IllegalStateException(e);
  53.         } catch(final IllegalAccessException e){
  54.             throw new IllegalStateException(e);
  55.         } catch(final InvocationTargetException e){
  56.             throw new IllegalStateException(e);
  57.         } catch(final InstantiationException e){
  58.             throw new IllegalStateException(e);
  59.         }
  60.     }
  61.  
  62.     @SuppressWarnings("unchecked")
  63.     protected final void addPropertyListItem(final String name,
  64.         final Object value){
  65.         final Object property = this.propertyMap.get(name);
  66.         Preconditions.checkState(property == null || property instanceof List,
  67.             "Expected property " + name + "to be of type List, but was "
  68.                 + (property == null ? null : property.getClass()));
  69.         if(property == null){
  70.             propertyMap.put(name, Lists.newArrayList(value));
  71.         } else{
  72.             ((List<Object>) property).add(value);
  73.         }
  74.     }
  75.  
  76.     protected final void setProperty(final String name, final Object value){
  77.         this.propertyMap.put(name, value);
  78.     }
  79.  
  80. }