public Object convertSourceToTargetClass(Object source, Class targetClass) throws Exception { if (source == null) { return null; } Class collectionImplClass = getCollectionImplClass(targetClass); Constructor constructor = collectionImplClass.getConstructor(null); Collection collection = (Collection) constructor.newInstance(null); ConversionExecutor converter = getElementConverter(source, targetClass); Object value; if (converter != null) { value = converter.execute(source); } else { value = source; } collection.add(value); return collection; }
elementConverter = new StaticConversionExecutor(sourceClass, targetClass, new ObjectToCollection( elementConverter));
@SuppressWarnings({ "rawtypes", "unchecked" }) public Object convertSourceToTargetClass(Object source, Class<?> targetClass) { if (source == null) { return null; } Collection collection = CollectionFactory.createCollection(targetClass, DEFAULT_INITIAL_CAPACITY); ConversionExecutor converter = getElementConverter(source, (Class<? extends Collection<?>>) targetClass); Object value; if (converter != null) { value = converter.execute(source); } else { value = source; } collection.add(value); return collection; }
elementConverter = new StaticConversionExecutor(sourceClass, targetClass, new ObjectToCollection( elementConverter));
@SuppressWarnings({ "rawtypes", "unchecked" }) public Object convertSourceToTargetClass(Object source, Class<?> targetClass) { if (source == null) { return null; } Collection collection = CollectionFactory.createCollection(targetClass, DEFAULT_INITIAL_CAPACITY); ConversionExecutor converter = getElementConverter(source, (Class<? extends Collection<?>>) targetClass); Object value; if (converter != null) { value = converter.execute(source); } else { value = source; } collection.add(value); return collection; }
/** * Add all default converters to the conversion service. */ protected void addDefaultConverters() { addConverter(new StringToByte()); addConverter(new StringToBoolean()); addConverter(new StringToCharacter()); addConverter(new StringToShort()); addConverter(new StringToInteger()); addConverter(new StringToLong()); addConverter(new StringToFloat()); addConverter(new StringToDouble()); addConverter(new StringToBigInteger()); addConverter(new StringToBigDecimal()); addConverter(new StringToLocale()); addConverter(new StringToDate()); addConverter(new StringToLabeledEnum()); addConverter(new ObjectToCollection(this)); addConverter(new NumberToNumber()); if (ClassUtils.isPresent("java.lang.Enum", this.getClass().getClassLoader())) { addConverter(new StringToEnum()); } }