Package org.mapstruct

Annotation Type TargetType

  • @Target(PARAMETER)
    public @interface TargetType
    Declares a parameter of a custom mapping method to be populated with the target type of the mapping.

    Not more than one parameter can be declared as TargetType and that parameter needs to be of type Class (may be parameterized), or a super-type of it.


     public class EntityFactory {
        public <T extends BaseEntity> T createEntity(@TargetType Class<T> entityClass) {
             return // ... custom factory logic
     @Mapper(uses = EntityFactory.class)
     public interface CarMapper {
         CarEntity carDtoToCar(CarDto dto);
     // generates
     public class CarMapperImpl implements CarMapper {
         private final EntityFactory entityFactory = new EntityFactory();
         public CarEntity carDtoToCar(CarDto dto) {
             if ( dto == null ) {
                 return null;
             CarEntity carEntity = entityFactory.createEntity( CarEntity.class );
             return carEntity;
    Andreas Gudian