Package org.apache.commons.beanutils

Examples of org.apache.commons.beanutils.ConvertUtilsBean


        String propertyName = methodName.substring(4);
        propertyName = Character.toLowerCase(firstChar) + propertyName;

        System.out.println("Type: " + type + ", property " + propertyName);

        Object convert = new ConvertUtilsBean().convert(someValue, type);

        Method method = this.getClass().getMethod(methodName, new Class[]
        {
          type
        });
View Full Code Here


public class MapConverter {

  private ConvertUtilsBean convertUtils;

  public MapConverter() {
    this(new ConvertUtilsBean());
  }
View Full Code Here

import org.apache.commons.beanutils.converters.LongConverter;

public final class Converter {

  public static final ConvertUtilsBean getDefault() {
    ConvertUtilsBean convertUtils = new ConvertUtilsBean();
    convertUtils.register(new SqlDateConverter(), java.sql.Date.class);
    convertUtils.register(new DateConverter(), java.util.Date.class);
    convertUtils.register(new BooleanConverter(null), Boolean.class);
    convertUtils.register(new IntegerConverter(null), Integer.class);
    convertUtils.register(new LongConverter(null), Long.class);
    convertUtils.register(new FloatConverter(null), Float.class);
    convertUtils.register(new DoubleConverter(null), Double.class);
    return convertUtils;
  }
View Full Code Here

    try {
      Class<?> type = null;
      type = Class.forName(field.getType());
      List<T> rs = CollectUtils.newArrayList();
      if (properties.isEmpty()) {
        ConvertUtilsBean converter = Converter.getDefault();
        for (String data : datas) {
          rs.add((T) converter.convert(data, type));
        }
        return rs;
      } else {
        properties.clear();
        int startIndex=0;
View Full Code Here

        Map<String, String> subset = getSubset(keyPrefix);
        setConfigurables(o, subset);
    }

    public void setConfigurables(Object o, Map<String, String> config) {
        ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
        Map<Method, PostConfigurationValidator> validatorMap = new HashMap<Method, PostConfigurationValidator>();

        for (Method m : o.getClass().getMethods()) {
            Configurable configurableAnnotation = m.getAnnotation(Configurable.class);
            if (configurableAnnotation != null) {
                if (m.getParameterTypes().length != 1) {
                    throw new LumifyException("Invalid method to be configurable. Expected 1 argument. Found " + m.getParameterTypes().length + " arguments");
                }

                String propName = m.getName().substring("set" .length());
                if (propName.length() > 1) {
                    propName = propName.substring(0, 1).toLowerCase() + propName.substring(1);
                }

                String name;
                String defaultValue;
                if (configurableAnnotation.name() != null) {
                    name = configurableAnnotation.name();
                    defaultValue = configurableAnnotation.defaultValue();
                } else {
                    name = propName;
                    defaultValue = null;
                }
                String val;
                if (config.containsKey(name)) {
                    val = config.get(name);
                } else {
                    if (Configurable.DEFAULT_VALUE.equals(defaultValue)) {
                        if (configurableAnnotation.required()) {
                            throw new LumifyException("Could not find property " + name + " for " + o.getClass().getName() + " and no default value was specified.");
                        } else {
                            continue;
                        }
                    }
                    val = defaultValue;
                }
                try {
                    Object convertedValue = convertUtilsBean.convert(val, m.getParameterTypes()[0]);
                    m.invoke(o, convertedValue);
                } catch (Exception ex) {
                    throw new LumifyException("Could not set property " + m.getName() + " on " + o.getClass().getName());
                }
            }
View Full Code Here

    @Test
    public void testDigester()
        throws IOException, SAXException
    {
        PropertyUtilsBean propertyUtils = new MyPropertyUtilsBean();
        ConvertUtilsBean convertUtils = new ConvertUtilsBean();
        BeanUtilsBean beanUtils = new BeanUtilsBean( convertUtils, propertyUtils );
        BeanUtilsBean.setInstance( beanUtils );

        final String xml = "<myclass flag='true' />";
        final Digester digester = new Digester();
View Full Code Here

  }

  @Test
  public void testPaddedInt() throws Exception
  {
    ConvertUtilsBean convertUtils = new ConvertUtilsBean();
    Integer intValue = (Integer)convertUtils.convert("456", int.class);

    assertEquals("correct int value", 456, intValue.intValue());
    BeanUtilsBean beanUtils = new BeanUtilsBean();
    PropertyDescriptor propDesc = beanUtils.getPropertyUtils().getPropertyDescriptor(_configBuilder,
                                                                                     "intSetting");
View Full Code Here

     */
    private static BeanUtilsBean initBeanUtilsBean()
    {
        PropertyUtilsBean propUtilsBean = new PropertyUtilsBean();
        propUtilsBean.addBeanIntrospector(new FluentPropertyBeanIntrospector());
        return new BeanUtilsBean(new ConvertUtilsBean(), propUtilsBean);
    }
View Full Code Here

     *
     * @return a configured BeanUtilsBean
     */
    private static BeanUtilsBean createBeanUtilsBean()
    {
        final ConvertUtilsBean cub = new ConvertUtilsBean();

        // TODO: is there a smarter way to tell beanutils not to use defaults?

        final boolean[] booleanArray = new boolean[0];
        final byte[] byteArray = new byte[0];
        final char[] charArray = new char[0];
        final double[] doubleArray = new double[0];
        final float[] floatArray = new float[0];
        final int[] intArray = new int[0];
        final long[] longArray = new long[0];
        final short[] shortArray = new short[0];


        cub.register(new BooleanConverter(), Boolean.TYPE);
        cub.register(new BooleanConverter(), Boolean.class);
        cub.register(
            new BooleanArrayConverter(), booleanArray.getClass());
        cub.register(new ByteConverter(), Byte.TYPE);
        cub.register(new ByteConverter(), Byte.class);
        cub.register(
            new ByteArrayConverter(byteArray), byteArray.getClass());
        cub.register(new CharacterConverter(), Character.TYPE);
        cub.register(new CharacterConverter(), Character.class);
        cub.register(
            new CharacterArrayConverter(), charArray.getClass());
        cub.register(new DoubleConverter(), Double.TYPE);
        cub.register(new DoubleConverter(), Double.class);
        cub.register(
            new DoubleArrayConverter(doubleArray), doubleArray.getClass());
        cub.register(new FloatConverter(), Float.TYPE);
        cub.register(new FloatConverter(), Float.class);
        cub.register(new FloatArrayConverter(), floatArray.getClass());
        cub.register(new IntegerConverter(), Integer.TYPE);
        cub.register(new IntegerConverter(), Integer.class);
        cub.register(new IntegerArrayConverter(), intArray.getClass());
        cub.register(new LongConverter(), Long.TYPE);
        cub.register(new LongConverter(), Long.class);
        cub.register(new LongArrayConverter(), longArray.getClass());
        cub.register(new ShortConverter(), Short.TYPE);
        cub.register(new ShortConverter(), Short.class);
        cub.register(new ShortArrayConverter(), shortArray.getClass());
        // TODO: investigate:
        // StringArrayConverter doesn't properly convert an array of tokens with
        // elements containing an underscore, "_".
        // Hacked a replacement class :(
        //        cub.register(new StringArrayConverter(),
        //                        String[].class);
        cub.register(new StrArrayConverter(), String[].class);
        cub.register(new IntegerArrayConverter(), Integer[].class);

        // BigDecimal, BigInteger, Class, Date, String, Time, TimeStamp
        // do not use defaults in the default configuration of ConvertUtilsBean

        return new BeanUtilsBean(cub, new PropertyUtilsBean());
View Full Code Here

  @SuppressWarnings("unchecked")
  public <T> List<T> getData(Class<T> type, String source) {
    if (StringUtils.isEmpty(source)) { return Collections.emptyList(); }
    String[] datas = StrUtils.split(source);
    List<T> rs = CollectUtils.newArrayList();
    ConvertUtilsBean converter = Converter.getDefault();
    for (String data : datas) {
      rs.add((T) converter.convert(data, type));
    }
    return rs;
  }
View Full Code Here

TOP

Related Classes of org.apache.commons.beanutils.ConvertUtilsBean

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.