orika-mapper/orika

【BUG】Generic object mapping ClassCastException occut.

RobertoHuang opened this issue · 0 comments

Version: 1.5.4

Code:

import java.util.List;
import java.util.Objects;

import com.google.common.collect.Lists;
import lombok.Data;
import ma.glasnost.orika.MapperFacade;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.Type;
import ma.glasnost.orika.metadata.TypeBuilder;
import ma.glasnost.orika.metadata.TypeFactory;

public class BeanMapperUtils {
    private static MapperFacade defaultMapper;

    static {
        defaultMapper = new DefaultMapperFactory.Builder().build().getMapperFacade();
    }

    private BeanMapperUtils() {

    }

    public static <E> Type<E> getType(final Class<E> rawType) {
        return TypeFactory.valueOf(rawType);
    }

    public static <S, D> void map(S source, D destination) {
        if (Objects.nonNull(source) && Objects.nonNull(destination)) {
            defaultMapper.map(source, destination);
        }
    }

    public static <S, D> D map(S source, Class<D> destinationClass) {
        return Objects.isNull(source) ? null : defaultMapper.map(source, destinationClass);
    }

    public static <S, D> D map(S source, Type<S> sourceType, Type<D> destinationType) {
        return Objects.isNull(source) ? null : defaultMapper.map(source, sourceType, destinationType);
    }

    public static <S, D> List<D> mapList(Iterable<S> sourceList, Type<S> sourceType, Type<D> destinationType) {
        List<D> destinationList = Lists.newArrayList();
        if (Objects.nonNull(sourceList) && sourceList.iterator().hasNext()) {
            destinationList = defaultMapper.mapAsList(sourceList, sourceType, destinationType);
        }
        return destinationList;
    }

    @Data
    public static class Job {
        private String name = "开发";
    }

    @Data
    public static class Employee<T> {
        private T t;

        public Employee(T t) {
            this.t = t;
        }
    }

    @Data
    public static class JobDTO {
        private String name;
    }

    @Data
    public static class EmployeeDTO<T> {
        private T t;
    }

    public static void main(String[] args) {
        Employee<Job> employee = new Employee<Job>(new Job());
        EmployeeDTO<JobDTO> employeeDto = BeanMapperUtils.map(employee, EmployeeDTO.class);
        employeeDto = BeanMapperUtils.map(employee, new TypeBuilder<Employee<Job>>() {}.build(), new TypeBuilder<EmployeeDTO<JobDTO>>() {}.build());
    }
}

Error info:
Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to BeanMapperUtils$JobDTO
at ma.glasnost.orika.generated.Orika_EmployeeDTO_Employee_Mapper1049691661111333$2.mapAtoB(Orika_EmployeeDTO_Employee_Mapper1049691661111333$2.java)
at ma.glasnost.orika.impl.mapping.strategy.UseCustomMapperStrategy.map(UseCustomMapperStrategy.java:77)
at ma.glasnost.orika.impl.MapperFacadeImpl.map(MapperFacadeImpl.java:254)

How to slove this problem? It seem a bug?