Java Code Examples for org.apache.commons.beanutils.Converter

The following examples show how to use org.apache.commons.beanutils.Converter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Bats   Source File: StringCodecs.java    License: Apache License 2.0 6 votes vote down vote up
public static void check()
{
  if (classLoaders.putIfAbsent(Thread.currentThread().getContextClassLoader(), Boolean.TRUE) == null) {
    loadDefaultConverters();
    for (Map.Entry<Class<?>, Class<? extends StringCodec<?>>> entry : codecs.entrySet()) {
      try {
        final StringCodec<?> codecInstance = entry.getValue().newInstance();
        ConvertUtils.register(new Converter()
        {
          @Override
          public Object convert(Class type, Object value)
          {
            return value == null ? null : codecInstance.fromString(value.toString());
          }

        }, entry.getKey());
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    }
  }
}
 
Example 2
Source Project: attic-apex-core   Source File: StringCodecs.java    License: Apache License 2.0 6 votes vote down vote up
public static void check()
{
  if (classLoaders.putIfAbsent(Thread.currentThread().getContextClassLoader(), Boolean.TRUE) == null) {
    loadDefaultConverters();
    for (Map.Entry<Class<?>, Class<? extends StringCodec<?>>> entry : codecs.entrySet()) {
      try {
        final StringCodec<?> codecInstance = entry.getValue().newInstance();
        ConvertUtils.register(new Converter()
        {
          @Override
          public Object convert(Class type, Object value)
          {
            return value == null ? null : codecInstance.fromString(value.toString());
          }

        }, entry.getKey());
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    }
  }
}
 
Example 3
Source Project: attic-apex-core   Source File: InjectConfigTest.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void injectMembers(T t)
{
  try {
    LOG.debug("Processing " + annotation + " for field " + field);
    String value = conf.get(annotation.key());
    if (value == null) {
      if (annotation.optional() == false) {
        throw new IllegalArgumentException("Cannot inject " + annotation);
      }
      return;
    }
    Converter c = converters.lookup(field.getType());
    if (c == null) {
      throw new IllegalArgumentException("Cannot find a converter for: " + field);
    }
    field.set(t, c.convert(field.getType(), value));
  } catch (IllegalAccessException e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source Project: Bats   Source File: StringCodecs.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> void register(final Class<? extends StringCodec<?>> codec, final Class<T> clazz) throws InstantiationException, IllegalAccessException
{
  check();
  final StringCodec<?> codecInstance = codec.newInstance();
  ConvertUtils.register(new Converter()
  {
    @Override
    public Object convert(Class type, Object value)
    {
      return value == null ? null : codecInstance.fromString(value.toString());
    }

  }, clazz);
  codecs.put(clazz, codec);
}
 
Example 5
Source Project: spring-boot   Source File: MyBeanUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * map to bean
 * 转换过程中,由于属性的类型不同,需要分别转换。
 * java 反射机制,转换过程中属性的类型默认都是 String 类型,否则会抛出异常,而 BeanUtils 项目,做了大量转换工作,比 java 反射机制好用
 * BeanUtils 的 populate 方法,对 Date 属性转换,支持不好,需要自己编写转换器
 *
 * @param map  待转换的 map
 * @param bean 满足 bean 格式,且需要有无参的构造方法; bean 属性的名字应该和 map 的 key 相同
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private static void mapToBean(Map<String, Object> map, Object bean) throws IllegalAccessException, InvocationTargetException {

    //注册几个转换器
    ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);
    ConvertUtils.register(new SqlTimestampConverter(null), java.sql.Timestamp.class);
    //注册一个类型转换器  解决 common-beanutils 为 Date 类型赋值问题
    ConvertUtils.register(new Converter() {
        //  @Override
        public Object convert(Class type, Object value) { // type : 目前所遇到的数据类型。  value :目前参数的值。
            // System.out.println(String.format("value = %s", value));

            if (value == null || value.equals("") || value.equals("null"))
                return null;
            Date date = null;
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = dateFormat.parse((String) value);
            } catch (Exception e) {

                e.printStackTrace();
            }

            return date;
        }

    }, Date.class);

    BeanUtils.populate(bean, map);
}
 
Example 6
Source Project: attic-apex-core   Source File: StringCodecs.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> void register(final Class<? extends StringCodec<?>> codec, final Class<T> clazz) throws InstantiationException, IllegalAccessException
{
  check();
  final StringCodec<?> codecInstance = codec.newInstance();
  ConvertUtils.register(new Converter()
  {
    @Override
    public Object convert(Class type, Object value)
    {
      return value == null ? null : codecInstance.fromString(value.toString());
    }

  }, clazz);
  codecs.put(clazz, codec);
}
 
Example 7
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 5 votes vote down vote up
public void setConverter(Class clazz)
{
    try
    {
        convertWith((Converter)clazz.newInstance());
    }
    catch (Exception e)
    {
        throw new IllegalArgumentException("Class "+clazz+" is not a valid "+Converter.class, e);
    }
}
 
Example 8
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 5 votes vote down vote up
public void setConverter(String classname)
{
    try
    {
        convertWith((Converter)ClassUtils.getInstance(classname));
    }
    catch (Exception e)
    {
        throw new IllegalArgumentException("Class "+classname+" is not a valid "+Converter.class, e);
    }
}
 
Example 9
Source Project: activemq-artemis   Source File: BeanSupport.java    License: Apache License 2.0 4 votes vote down vote up
public static void registerConverter(Converter converter, Class type) {
   synchronized (beanUtils) {
      beanUtils.getConvertUtils().register(converter, type);
   }
}
 
Example 10
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 4 votes vote down vote up
public Converter getConverter()
{
    return this.converter;
}
 
Example 11
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 4 votes vote down vote up
Type(Class t, Converter c)
{
    this(t, c, false);
}
 
Example 12
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 4 votes vote down vote up
Type(Class t, Converter c, boolean lst)
{
    this.target = t;
    this.converter = c;
    this.isList = lst;
}
 
Example 13
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 4 votes vote down vote up
public Converter getConverter()
{
    return this.converter;
}
 
Example 14
Source Project: velocity-tools   Source File: Data.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * This is a convenience method for those doing configuration in java.
 * It cannot be named setConverter(), or else it would confuse BeanUtils.
 * @param converter value converter
 */
public void convertWith(Converter converter)
{
    this.converter = converter;
}