ClassCastException when using ModelMapper to map a field that extends a generic type

I have the following class structure (code at the end):

  • abstract class AbstractThing: I have some different kinds of things with common fields
  • class Foo extends AbstractThing: a foo is one kind of thing
  • abstract class AbstractReq <T extends AbstractThing>: I have requests that relate to things, where the subject of the request is a field (T subject)
  • FooReq extends AbstractReq<Foo>: Requests about foos, specifically
  • FooReqDto: a DTO class with all of the fields in a FooReq, flattened

I’m trying to map an instance of FooReq to FooReqDto using ModelMapper. If I don’t need to customize my type mappings, everything works great. But I do (in my actual code) need to customize some things, represented by the field called somethingDifferent instead of subjectFooField in FooReqDto. Now when I run my test, I get the following error:

java.lang.ClassCastException: class com.example.test.TempTest$AbstractThing$ByteBuddy$PsTTXfw5 cannot be cast to class com.example.test.TempTest$Foo (com.example.test.TempTest$AbstractThing$ByteBuddy$PsTTXfw5 and com.example.test.TempTest$Foo are in unnamed module of loader ‘app’)

What am I missing when creating my type map? It doesn’t seem like there’s any inherent problem with mapping classes that have inheritance & generics involved, since it works when there’s no mapping. And the actual mappings I’m using worked fine before I pulled out the abstract classes (which is necessary to cut down on a lot of duplicate code).

Here’s the full code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>package com.example.test;
import org.junit.Test;
import org.modelmapper.ModelMapper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class TempTest {
@Test
public void testStuff() {
final ModelMapper mapper = new ModelMapper();
mapper.createTypeMap(FooReq.class, FooReqDto.class)
.addMappings(map -> {
map.map(req -> req.getSubject().getFooField(), FooReqDto::setSomethingDifferent);
});
final FooReq req = new FooReq();
req.setSubmitted(true);
req.getSubject().setName("testFoo");
req.getSubject().setFooField("bar");
final FooReqDto dto = mapper.map(req, FooReqDto.class);
assertTrue(dto.isSubmitted());
assertEquals("testFoo", dto.getSubjectName());
assertEquals("bar", dto.getSomethingDifferent());
}
public static abstract class AbstractThing {
private String name;
// getters and setters omitted
}
public static class Foo extends AbstractThing {
private String fooField;
// getters and setters omitted
}
public static abstract class AbstractReq <T extends AbstractThing> {
protected T subject;
private boolean submitted;
// getters and setters omitted
}
public static class FooReq extends AbstractReq<Foo> {
public FooReq() {
super();
subject = new Foo();
}
}
public static class FooReqDto {
private boolean submitted;
private String subjectName;
private String somethingDifferent;
public FooReqDto() {
}
// getters and setters omitted
}
}
</code>
<code>package com.example.test; import org.junit.Test; import org.modelmapper.ModelMapper; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class TempTest { @Test public void testStuff() { final ModelMapper mapper = new ModelMapper(); mapper.createTypeMap(FooReq.class, FooReqDto.class) .addMappings(map -> { map.map(req -> req.getSubject().getFooField(), FooReqDto::setSomethingDifferent); }); final FooReq req = new FooReq(); req.setSubmitted(true); req.getSubject().setName("testFoo"); req.getSubject().setFooField("bar"); final FooReqDto dto = mapper.map(req, FooReqDto.class); assertTrue(dto.isSubmitted()); assertEquals("testFoo", dto.getSubjectName()); assertEquals("bar", dto.getSomethingDifferent()); } public static abstract class AbstractThing { private String name; // getters and setters omitted } public static class Foo extends AbstractThing { private String fooField; // getters and setters omitted } public static abstract class AbstractReq <T extends AbstractThing> { protected T subject; private boolean submitted; // getters and setters omitted } public static class FooReq extends AbstractReq<Foo> { public FooReq() { super(); subject = new Foo(); } } public static class FooReqDto { private boolean submitted; private String subjectName; private String somethingDifferent; public FooReqDto() { } // getters and setters omitted } } </code>
package com.example.test;

import org.junit.Test;
import org.modelmapper.ModelMapper;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TempTest {

    @Test
    public void testStuff() {
        final ModelMapper mapper = new ModelMapper();
        mapper.createTypeMap(FooReq.class, FooReqDto.class)
                .addMappings(map -> {
                    map.map(req -> req.getSubject().getFooField(), FooReqDto::setSomethingDifferent);
                });
        final FooReq req = new FooReq();
        req.setSubmitted(true);
        req.getSubject().setName("testFoo");
        req.getSubject().setFooField("bar");
        final FooReqDto dto = mapper.map(req, FooReqDto.class);
        assertTrue(dto.isSubmitted());
        assertEquals("testFoo", dto.getSubjectName());
        assertEquals("bar", dto.getSomethingDifferent());
    }

    public static abstract class AbstractThing {
        private String name;

        // getters and setters omitted
    }

    public static class Foo extends AbstractThing {
        private String fooField;

        // getters and setters omitted
    }

    public static abstract class AbstractReq <T extends AbstractThing> {
        protected T subject;
        private boolean submitted;

        // getters and setters omitted
    }

    public static class FooReq extends AbstractReq<Foo> {
        public FooReq() {
            super();
            subject = new Foo();
        }
    }

    public static class FooReqDto {
        private boolean submitted;
        private String subjectName;
        private String somethingDifferent;

        public FooReqDto() {

        }

        // getters and setters omitted
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật