Package org.mapstruct

Annotation Type SubclassMapping


Configures the mapping to handle hierarchy of the source type.

The subclass to be mapped is to be specified via source(). The subclass to map to is to be specified via target().

This annotation can be combined with @Mapping annotations.


 @Mapper
 public interface MyMapper {
    @SubclassMapping (target = TargetSubclass.class, source = SourceSubclass.class)
    TargetParent mapParent(SourceParent parent);

    TargetSubclass mapSubclass(SourceSubclass subInstant);
 }
 
Below follow examples of the implementation for the mapParent method. Example 1: For parents that cannot be created. (e.g. abstract classes or interfaces)

 // generates
 @Override
 public TargetParent mapParent(SourceParent parent) {
     if (parent instanceof SourceSubclass) {
         return mapSubclass( (SourceSubclass) parent );
     }
     else {
         throw new IllegalArgumentException("Not all subclasses are supported for this mapping. Missing for "
                    + parent.getClass());
     }
 }
 
Example 2: For parents that can be created. (e.g. normal classes or interfaces with @Mapper( uses = ObjectFactory.class ) )

 // generates
 @Override
 public TargetParent mapParent(SourceParent parent) {
     TargetParent targetParent1;
     if (parent instanceof SourceSubclass) {
         targetParent1 = mapSubclass( (SourceSubclass) parent );
     }
     else {
         targetParent1 = new TargetParent();
         // ...
     }
 }
 
Since:
1.5
Author:
Ben Zegveld
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The source subclass to check for before using the default mapping as fallback.
    The target subclass to map the source to.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<? extends Annotation>[]
    A qualifier can be specified to aid the selection process of a suitable mapper.
    String-based form of qualifiers; When looking for a suitable mapping method for a given property, MapStruct will only consider those methods carrying directly or indirectly (i.e. on the class-level) a Named annotation for each of the specified qualifier names.
  • Element Details

    • source

      Class<?> source
      The source subclass to check for before using the default mapping as fallback.
      Returns:
      the source subclass to check for before using the default mapping as fallback.
    • target

      Class<?> target
      The target subclass to map the source to.
      Returns:
      the target subclass to map the source to.
    • qualifiedBy

      Class<? extends Annotation>[] qualifiedBy
      A qualifier can be specified to aid the selection process of a suitable mapper. This is useful in case multiple mapping methods (hand written or generated) qualify and thus would result in an 'Ambiguous mapping methods found' error. A qualifier is a custom annotation and can be placed on a hand written mapper class or a method.
      Returns:
      the qualifiers
      See Also:
      Default:
      {}
    • qualifiedByName

      String[] qualifiedByName
      String-based form of qualifiers; When looking for a suitable mapping method for a given property, MapStruct will only consider those methods carrying directly or indirectly (i.e. on the class-level) a Named annotation for each of the specified qualifier names.

      Note that annotation-based qualifiers are generally preferable as they allow more easily to find references and are safe for refactorings, but name-based qualifiers can be a less verbose alternative when requiring a large number of qualifiers as no custom annotation types are needed.

      Returns:
      One or more qualifier name(s)
      See Also:
      Default:
      {}