Question:
Please note: even though I mention Dozer in this question, I do believe its really just a pure Java generics question at heart. There may be a Dozer-specific solution out there, but I think anyone with strong working knowledge of Java (11) generics/captures/erasures should be able to help me out!Java 11 and Dozer here. Dozer is great for applying default bean mapping rules to field names, but anytime you have specialized, custom mapping logic you need to implement a Dozer
CustomConverter
and register it. That would be great, except the Dozer API for CustomConverter
isn’t genericized, is monolithic and leads to nasty code like this:I’m trying to make this a wee bit more palatable:
mapper.map(source)
line at the very end:"Required type: capture of ?; Provided: Object"
What can I do to fix this compiler error? I’m not married to this API/approach, but I do like the simplicity it adds over the
MyMonolithicConverter
example above, which is the approach Dozer sort of forces on you. It is important to note that I am using Dozer elsewhere for simple bean mappings so I would prefer to use a CustomConverter
impl and leverage Dozer for this instead of bringing in a whole other dependency/library for these custom/complex mappings. If Dozer offers a different solution I might be happy with that as well. Otherwise I just need to fix this capture issue. Thanks for any help here!Answer:
The issue seems to come from thebeanMappers
. You have a set of mappers of various types. The compiler cannot infer what types the found mapper
will have.You can make the compiler believe you by casting the result and suppress the warning it gives you.
Casting to a
<?,?>
isn’t going to happen, so I’ve added symbols for the convert method. At least it can then be assumed that when you get a BeanMapper<S,T>
, map
will indeed return a T
upon an S
source.If you have better answer, please add a comment about this, thank you!