All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mapstruct.InheritInverseConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.3
Show newest version
/*
 * Copyright MapStruct Authors.
 *
 * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */
package org.mapstruct;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Advises the code generator to apply all the {@link Mapping}s 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 {@link 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 {@link #name()} *

* {@link Mapping#expression()}, {@link Mapping#constant()}, {@link Mapping#defaultExpression()} and * {@link 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 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.CLASS) public @interface InheritInverseConfiguration { /** * The name of the inverse mapping method to inherit the mappings from. Needs only to be specified in case more than * one inverse method with matching source and target type exists. * * @return The name of the inverse mapping method to inherit the mappings from. */ String name() default ""; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy