Hello, I’m trying to use the
StringToBigDecimalConverter
that was added in Vaadin 7.2 but it seems that is bugged.
It takes a
String
as input and should return a
BigDecimal
as output.
Looking at the code I see that it extends from
AbstractStringToNumberConverter
.
It calls the parent method
convertToNumber
with as targetType
BigDecimal
.
I see that the targetType that is given is not even used in the method at all, which makes no sense.
Next I see that the
String
that is passed is converted to a
Number
using the default Java NumberFormat parse method.
This correctly returns a
Number
, which is the passed to the subclass that
tries to cast that Number to a BigDecimal
and return that. This of course will never work, as a Number cannot always be casted to BigDecimal and so it throws a ClassCastException when used in some cases (when the NumberFormat.parse() returns a Long or a Double for example)
Instead of doing this:
@Override
public BigDecimal convertToModel(String value, Class<? extends BigDecimal> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException {
return (BigDecimal) convertToNumber(value, BigDecimal.class, locale);
}
StringToBigDecimalConverter should instead do something like this:
@Override
public BigDecimal convertToModel(String value, Class<? extends BigDecimal> targetType, Locale locale) throws ConversionException {
return new BigDecimal(convertToNumber(value, BigDecimal.class, locale).doubleValue());
}