Package org.mapstruct
Annotation Type SubclassMapping
-
@Repeatable(SubclassMappings.class) @Retention(CLASS) @Target({METHOD,ANNOTATION_TYPE}) @Experimental public @interface 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 viatarget()
.This annotation can be combined with @Mapping annotations.
Below follow examples of the implementation for the mapParent method. Example 1: For parents that cannot be created. (e.g. abstract classes or interfaces)@Mapper public interface MyMapper { @SubclassMapping (target = TargetSubclass.class, source = SourceSubclass.class) TargetParent mapParent(SourceParent parent); TargetSubclass mapSubclass(SourceSubclass subInstant); }
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) { if (parent instanceof SourceSubclass) { return mapSubclass( (SourceSubclass) parent ); } else { throw new IllegalArgumentException("Not all subclasses are supported for this mapping. Missing for " + parent.getClass()); } }
// 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
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description Class<? extends Annotation>[]
qualifiedBy
A qualifier can be specified to aid the selection process of a suitable mapper.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) aNamed
annotation for each of the specified qualifier names.
-
-
-
Element Detail
-
source
Class<?> source
- Returns:
- the source subclass to check for before using the default mapping as fallback.
-
-
-
target
Class<?> target
- 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:
Qualifier
- 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) aNamed
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:
qualifiedBy()
,Named
- Default:
- {}
-
-