Is it actually possible to use generics when defining mappings in Orika?
It is possible, you need to use the MapperFactory#classMap(Type<A>, Type<B>)
API instead of MapperFactory#classMap(Class<A>, Class<B>)
.
You can find a lot of examples in Orika tests in the generics
package.
To construct a Type
instance you can use an in-place anonymous subclass of TypeBuilder
:
Type<MyGenericClass<GenericParam1, GenericParam2>> type =
new TypeBuilder<MyGenericClass<GenericParam1, GenericParam2>>() {}.build();
Note the brackets {}
after the constructor which create the anonymous subclass. That way Orika can find out the actual MyGenericClass<GenericParam1, GenericParam2>
type parameter using ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()
.