Android Open Source - colibri Reflector






From Project

Back to project page colibri.

License

The source code is released under:

Apache License

If you think the Android project colibri listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package labrom.colibri.xml;
/*from  ww w  .ja va 2  s.com*/
import java.util.HashMap;


/**
 * Is responsible for instantiating objects in a {@link Cursor}.
 * This can be done in three different ways:<ol>
 * <li>By instantiating a class (use {@link #Reflector(Class)})</li>
 * <li>By using a {@link ObjectFactory} (use {@link #Reflector(ObjectFactory)})</li>
 * <li>By creating a Map (use {@link #Reflector()})</li>
 * </ol>
 * 
 * @author Romain Laboisse labrom@gmail.com
 *
 * @param <T> The type of objects to create. If this Reflector is intended to work with a Map, T MUST be Map<String, Object> for things to work as expected.
 */
public class Reflector<T> {


  T object;
  private Class<T> clazz;
  private ObjectFactory<T> factory;
  
  /**
   * This reflector will use a Map. This is the same as calling any of the
   * other constructors will a null argument.
   */
  public Reflector() {
    // In this case we'll use a map
  }
  
  /**
   * 
   * @param clazz The class to instantiate. If null, will use a Map (same a {@link #Reflector()}).
   */
  public Reflector(Class<T> clazz) {
    this.clazz = clazz;
  }
  
  /**
   * 
   * @param f The object factory. If null, will use a Map (same a {@link #Reflector()}).
   */
  public Reflector(ObjectFactory<T> f) {
    this.factory = f;
  }
  
  
  @SuppressWarnings("unchecked")
  T createObject() {
    // If there's a factory, use it
    if(factory != null) {
      object =  factory.createObject();
      return object;
    }
    
    // If there's no class, use a Map
    if(clazz == null) {
      // T must be Map<String, Object> for things to work as expected.
      object = (T)new HashMap<String, Object>();
      return object;
    }
    
    // There's a class, try to instantiate from it
    try {
      object = clazz.newInstance();
    } catch (IllegalAccessException e) {
      throw new CursorException(e);
    } catch (InstantiationException e) {
      throw new CursorException(e);
    }
    return object;
  }
  
  public void set(FieldSetter<T> setter, Object value) {
    setter.set(object, value);
  }
  

}




Java Source Code List

labrom.colibri.Util.java
labrom.colibri.cache.CacheEntry.java
labrom.colibri.cache.Cache.java
labrom.colibri.cache.Entries.java
labrom.colibri.data.ActiveContentProvider.java
labrom.colibri.data.ActiveRecordList.java
labrom.colibri.data.ActiveRecord.java
labrom.colibri.data.Database.java
labrom.colibri.data.SelectionArgs.java
labrom.colibri.xml.AttributeContext.java
labrom.colibri.xml.CursorDef.java
labrom.colibri.xml.CursorException.java
labrom.colibri.xml.Cursor.java
labrom.colibri.xml.CustomMap.java
labrom.colibri.xml.ElementContext.java
labrom.colibri.xml.FieldSetter.java
labrom.colibri.xml.Item.java
labrom.colibri.xml.NodeContext.java
labrom.colibri.xml.ObjectFactory.java
labrom.colibri.xml.ParserFactory.java
labrom.colibri.xml.ParserUtil.java
labrom.colibri.xml.PullTest.java
labrom.colibri.xml.ReflectorTest.java
labrom.colibri.xml.Reflector.java
labrom.colibri.xml.RootContext.java
labrom.colibri.xml.SetterTest.java
labrom.colibri.xml.StringToDateTransformer.java
labrom.colibri.xml.StringTransformer.java
labrom.colibri.xml.maps.Cursor4Maps.java
labrom.colibri.xml.maps.CursorDef4Maps.java
labrom.colibri.xml.maps.ElementContext4Maps.java
labrom.colibri.xml.maps.RootContext4Maps.java
labrom.colibri.xml.maps.package-info.java
labrom.colibri.xml.package-info.java