/** * Converters are used by {@link PropertyUtils#setProperty(Object, String, Object)} to coerce * generic Object values into the specific type expected by the named setter. * * @param type * the Class to convert a value to; the destination type * @param converter * a converter used to convert the value from Object to T * @since 4.0 */ public static <T> void addConverter(Class<? super T> type, Converter<T> converter) { factory._addConverter(type, converter); }
/** * @since 3.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) public void setValue(Object object, Object newValue) throws PropertyException { if (writeMethod == null) { throw new PropertyException("Property '" + propertyName + "' is not writable", this, object); } Class type = writeMethod.getParameterTypes()[0]; Converter<?> converter = ConverterFactory.factory.getConverter(type); try { newValue = (converter != null) ? converter.convert(newValue, type) : newValue; // this will take care of primitives. if (newValue == null) { newValue = this.nullValue; } writeMethod.invoke(object, newValue); } catch (Throwable th) { throw new PropertyException("Error writing property: " + propertyName, this, object, th); } } }
_addConverter(Boolean.class, toBooleanConverter); _addConverter(boolean.class, toBooleanConverter); _addConverter(Short.class, toShortConverter); _addConverter(short.class, toShortConverter); _addConverter(Byte.class, toByteConverter); _addConverter(byte.class, toByteConverter); _addConverter(Integer.class, toIntConverter); _addConverter(int.class, toIntConverter); _addConverter(Long.class, toLongConverter); _addConverter(long.class, toLongConverter); _addConverter(Double.class, toDoubleConverter); _addConverter(double.class, toDoubleConverter); _addConverter(Float.class, toFloatConverter); _addConverter(float.class, toFloatConverter); _addConverter(Character.class, toCharConverter); _addConverter(char.class, toCharConverter); _addConverter(BigDecimal.class, toBigDecimalConverter); _addConverter(BigInteger.class, toBigIntegerConverter); _addConverter(Number.class, toBigDecimalConverter); _addConverter(String.class, toStringConverter); _addConverter(Date.class, toDateConverter);