Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【BUG】Generic object mapping ClassCastException occut. #396

Open
RobertoHuang opened this issue Jul 21, 2023 · 0 comments
Open

【BUG】Generic object mapping ClassCastException occut. #396

RobertoHuang opened this issue Jul 21, 2023 · 0 comments

Comments

@RobertoHuang
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant