Package org.mapstruct

Annotation Type SourceParameterCondition


  • @Target(METHOD)
    @Retention(CLASS)
    public @interface SourceParameterCondition
    This annotation marks a method as a check method to check if a source parameter needs to be mapped.

    By default, source parameters are checked against null, unless they are primitives.

    Check methods have to return boolean. The following parameters are accepted for the presence check methods:

    • The mapping source parameter
    • @Context parameter
    Note: The usage of this annotation is mandatory for a method to be considered as a source check method.
    
     public class PresenceCheckUtils {
    
       @SourceParameterCondition
       public static boolean isDefined(Car car) {
          return car != null && car.getId() != null;
       }
     }
    
     @Mapper(uses = PresenceCheckUtils.class)
     public interface CarMapper {
    
         CarDto map(Car car);
     }
     
    The following implementation of CarMapper will be generated:
    
     public class CarMapperImpl implements CarMapper {
    
         @Override
         public CarDto map(Car car) {
             if ( !PresenceCheckUtils.isDefined( car ) ) {
                 return null;
             }
    
             CarDto carDto = new CarDto();
    
             carDto.setId( car.getId() );
             // ...
    
             return carDto;
         }
     }
     
    Since:
    1.6
    Author:
    Filip Hrisafov
    See Also:
    @Condition