orika-mapper / orika

Simpler, better and faster Java bean mapping framework
http://orika-mapper.github.io/orika-docs/
Apache License 2.0
1.3k stars 268 forks source link

Custom convertor do not work #338

Open blueskydhy opened 4 years ago

blueskydhy commented 4 years ago

hi I encounter a problem which source filed type is Long and dest field type is BigDecimal.I want to convert long to bigdecmial and add a custom convert to mapperfactory , but it's do not work. here is the code

`public class Long2BigDecimalConverter extends BidirectionalConverter<Long, BigDecimal> {

@Override
public BigDecimal convertTo(Long aLong, Type<BigDecimal> type, MappingContext mappingContext) {
    return aLong == null ? null : new BigDecimal(aLong);
}

@Override
public Long convertFrom(BigDecimal bigDecimal, Type<Long> type, MappingContext mappingContext) {
    return bigDecimal == null ? null : bigDecimal.longValue();
}

}`

`private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().mapNulls(false).build();

static {
    mapperFactory.getConverterFactory().registerConverter(new Long2BigDecimalConverter());
}`

the exception is

ma.glasnost.orika.MappingException: java.lang.ClassCastException: java.lang.Long cannot be cast to java.math.BigDecimal at ma.glasnost.orika.impl.ExceptionUtility.newMappingException(ExceptionUtility.java:55) ~[orika-core-1.5.4.jar:?] at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:264) ~[orika-core-1.5.4.jar:?] at ma.glasnost.orika.impl.MapperFacadeImpl.mapElement(MapperFacadeImpl.java:799) ~[orika-core-1.5.4.jar:?] at ma.glasnost.orika.impl.MapperFacadeImpl.mapAsCollection(MapperFacadeImpl.java:622) ~[orika-core-1.5.4.jar:?] at ma.glasnost.orika.impl.MapperFacadeImpl.mapAsList(MapperFacadeImpl.java:420) ~[orika-core-1.5.4.jar:?] at ma.glasnost.orika.impl.MapperFacadeImpl.mapAsList(MapperFacadeImpl.java:707) ~[orika-core-1.5.4.jar:?] at com.meituan.trip.performance.calculator.common.util.converter.ModelConverter.convert(ModelConverter.java:54) ~[classes/:?]

expect your reply thanks

stapetro commented 4 years ago

Hey, could you please provide full source and destination class definitions? It would be also useful to reproduce the issue with an unit test. Thanks.