Package org.mapstruct

Annotation Type InheritInverseConfiguration


  • @Target(METHOD)
    @Retention(CLASS)
    public @interface InheritInverseConfiguration
    Advises the code generator to apply all the Mappings from an inverse mapping method to the annotated method as well. An inverse mapping method is a method which has the annotated method's source type as target type (return type or indicated through a parameter annotated with MappingTarget) and the annotated method's target type as source type.

    Any mappings given on the annotated method itself are added to those mappings inherited from the inverse method. In case of a conflict local mappings take precedence over inherited mappings.

    If more than one matching inverse method exists, the name of the method to inherit the configuration from must be specified via name()

    Mapping.expression(), Mapping.constant(), Mapping.defaultExpression() and Mapping.defaultValue() are not inverse inherited

    Examples

    
     @Mapper
     public interface HumanMapper {
          Human toHuman(HumanDto humanDto);
          @InheritInverseConfiguration
          HumanDto toHumanDto(Human human);
     }
     
    
     // generates
     public class HumanMapperImpl implements HumanMapper {
          @Override
          public Human toHuman(HumanDto humanDto) {
              if ( humanDto == null ) {
                  return null;
               }
              Human human = new Human();
              human.setName( humanDto.getName() );
              return human;
          }
          @Override
          public HumanDto toHumanDto(Human human) {
              if ( human == null ) {
                  return null;
              }
              HumanDto humanDto = new HumanDto();
              humanDto.setName( human.getName() );
              return humanDto;
          }
     }
     
    
     @Mapper
     public interface CarMapper {
    
     @Mapping( target = "seatCount", source = "numberOfSeats")
     @Mapping( target = "enginePower", source = "engineClass", ignore=true) // NOTE: source specified as well
     CarDto carToDto(Car car);
    
     @InheritInverseConfiguration
     @Mapping(target = "numberOfSeats", ignore = true)
     // no need to specify a mapping with ignore for "engineClass": specifying source above will assume
     Car carDtoToCar(CarDto carDto);
     }
     
    Author:
    Sjaak Derksen
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element Description
      String name
      The name of the inverse mapping method to inherit the mappings from.
    • Element Detail

      • name

        String name
        The name of the inverse mapping method to inherit the mappings from. Needs to be specified only in case more than one inverse method exists with a matching source and target type exists.
        Returns:
        The name of the inverse mapping method to inherit the mappings from.
        Default:
        ""