@Target(value=ANNOTATION_TYPE) @Retention(value=CLASS) public @interface Qualifier
Can be used in:
Mapping.qualifiedBy()
BeanMapping.qualifiedBy()
IterableMapping.qualifiedBy()
MapMapping.keyQualifiedBy()
MapMapping.valueQualifiedBy()
Example:
// create qualifiers
@Qualifier
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface TitleTranslator {}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface EnglishToGerman {}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface GermanToEnglish {}
// we can create class with map methods
@TitleTranslator
public class Titles {
@EnglishToGerman
public String translateTitleEnglishToGerman(String title) {
// some mapping logic
}
@GermanToEnglish
public String translateTitleGermanToEnglish(String title) {
// some mapping logic
}
}
// usage
@Mapper( uses = Titles.class )
public interface MovieMapper {
@Mapping( target = "title", qualifiedBy = { TitleTranslator.class, EnglishToGerman.class } )
GermanRelease toGerman( OriginalRelease movies );
}
// generates
public class MovieMapperImpl implements MovieMapper {
private final Titles titles = new Titles();
@Override
public GermanRelease toGerman(OriginalRelease movies) {
if ( movies == null ) {
return null;
}
GermanRelease germanRelease = new GermanRelease();
germanRelease.setTitle( titles.translateTitleEnglishToGerman( movies.getTitle() ) );
return germanRelease;
}
}
NOTE: Qualifiers should have RetentionPolicy.CLASS
.Named
Copyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.