All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onebusaway.gtfs.csv.schema.BeanWrapperFactory Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
package org.onebusaway.gtfs.csv.schema;

import java.beans.BeanInfo;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BeanWrapperFactory {

  private static Map, BeanClassWrapperImpl> _classWrappers = new HashMap, BeanClassWrapperImpl>();

  public static BeanWrapper wrap(Object object) {
    Class c = object.getClass();
    BeanClassWrapperImpl classWrapper = _classWrappers.get(c);
    if (classWrapper == null) {
      try {
        BeanInfo beanInfo = java.beans.Introspector.getBeanInfo(c);
        classWrapper = new BeanClassWrapperImpl(beanInfo);
        _classWrappers.put(c, classWrapper);
      } catch (Exception ex) {
        throw new IllegalStateException("introspection error for type " + c, ex);
      }
    }

    return new BeanWrapperImpl(classWrapper, object);
  }

  private static class BeanClassWrapperImpl {

    private Map _readMethods = new HashMap();

    private Map _writeMethods = new HashMap();

    public BeanClassWrapperImpl(BeanInfo info) {
      PropertyDescriptor[] properties = info.getPropertyDescriptors();
      for (PropertyDescriptor property : properties) {
        String name = property.getName();
        _readMethods.put(name, property.getReadMethod());
        _writeMethods.put(name, property.getWriteMethod());
      }
    }
    

    public Class getPropertyType(Object object, String propertyName) {
      Method method = _readMethods.get(propertyName);
      if (method == null)
        throw new IllegalArgumentException("no such property \"" + propertyName
            + "\" for type " + object.getClass());
      return method.getReturnType();
    }

    public Object getPropertyValue(Object object, String propertyName) {
      Method method = _readMethods.get(propertyName);
      if (method == null)
        throw new IllegalArgumentException("no such property \"" + propertyName
            + "\" for type " + object.getClass());
      try {
        return method.invoke(object);
      } catch (Exception ex) {
        throw new IllegalStateException("error invoking getter for property \""
            + propertyName + "\" for type " + object.getClass(), ex);
      }
    }

    public void setPropertyValue(Object object, String propertyName,
        Object value) {
      Method method = _writeMethods.get(propertyName);
      if (method == null)
        throw new IllegalArgumentException("no such property \"" + propertyName
            + "\" for type " + object.getClass());
      try {
        method.invoke(object, value);
      } catch (Exception ex) {
        throw new IllegalStateException("error invoking setter for property \""
            + propertyName + "\" for type " + object.getClass(), ex);
      }
    }


  }

  private static class BeanWrapperImpl implements BeanWrapper {

    private BeanClassWrapperImpl _classWrapper;

    private Object _wrappedInstance;

    public BeanWrapperImpl(BeanClassWrapperImpl classWrapper,
        Object wrappedInstance) {
      _classWrapper = classWrapper;
      _wrappedInstance = wrappedInstance;
    }
    
    @SuppressWarnings("unchecked")
    public  T getWrappedInstance(Class type) {
      return (T) _wrappedInstance;
    }

    public Class getPropertyType(String propertyName) {
      return _classWrapper.getPropertyType(_wrappedInstance, propertyName);
    }

    public Object getPropertyValue(String propertyName) {
      return _classWrapper.getPropertyValue(_wrappedInstance, propertyName);
    }

    public void setPropertyValue(String propertyName, Object value) {
      _classWrapper.setPropertyValue(_wrappedInstance, propertyName, value);
    }

  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy