11package org .mapstruct .extensions .spring .converter ;
22
3- import static java .util .stream .Collectors .toList ;
4- import static javax .lang .model .element .ElementKind .METHOD ;
5- import static javax .lang .model .element .Modifier .PUBLIC ;
6- import static javax .lang .model .type .TypeKind .DECLARED ;
7- import static javax .tools .Diagnostic .Kind .ERROR ;
8-
93import com .squareup .javapoet .ClassName ;
10- import java .io .IOException ;
11- import java .io .Writer ;
12- import java .time .Clock ;
13- import java .util .List ;
14- import java .util .Objects ;
15- import java .util .Optional ;
16- import java .util .Set ;
17- import java .util .concurrent .atomic .AtomicReference ;
4+ import org .apache .commons .lang3 .StringUtils ;
5+ import org .apache .commons .lang3 .tuple .MutablePair ;
6+ import org .apache .commons .lang3 .tuple .Pair ;
7+ import org .mapstruct .extensions .spring .SpringMapperConfig ;
8+
189import javax .annotation .processing .AbstractProcessor ;
1910import javax .annotation .processing .RoundEnvironment ;
2011import javax .annotation .processing .SupportedAnnotationTypes ;
2516import javax .lang .model .type .DeclaredType ;
2617import javax .lang .model .type .TypeMirror ;
2718import javax .lang .model .util .Types ;
28- import org .apache .commons .lang3 .StringUtils ;
29- import org .apache .commons .lang3 .tuple .MutablePair ;
30- import org .apache .commons .lang3 .tuple .Pair ;
31- import org .mapstruct .extensions .spring .SpringMapperConfig ;
19+ import java .io .IOException ;
20+ import java .io .Writer ;
21+ import java .time .Clock ;
22+ import java .util .*;
23+
24+ import static java .util .stream .Collectors .toList ;
25+ import static javax .lang .model .element .ElementKind .METHOD ;
26+ import static javax .lang .model .element .Modifier .PUBLIC ;
27+ import static javax .lang .model .type .TypeKind .DECLARED ;
28+ import static javax .tools .Diagnostic .Kind .ERROR ;
3229
3330@ SupportedAnnotationTypes ({
3431 ConverterMapperProcessor .MAPPER ,
@@ -38,7 +35,8 @@ public class ConverterMapperProcessor extends AbstractProcessor {
3835 protected static final String MAPPER = "org.mapstruct.Mapper" ;
3936 protected static final String SPRING_MAPPER_CONFIG =
4037 "org.mapstruct.extensions.spring.SpringMapperConfig" ;
41- protected static final String SPRING_CONVERTER_FULL_NAME = "org.springframework.core.convert.converter.Converter" ;
38+ protected static final String SPRING_CONVERTER_FULL_NAME =
39+ "org.springframework.core.convert.converter.Converter" ;
4240
4341 private final ConversionServiceAdapterGenerator adapterGenerator ;
4442
@@ -66,22 +64,26 @@ public boolean process(
6664 ClassName .get (adapterPackageAndClass .getLeft (), adapterPackageAndClass .getRight ()));
6765 descriptor .setConversionServiceBeanName (getConversionServiceName (annotations , roundEnv ));
6866 annotations .stream ()
69- .filter (annotation -> MAPPER . contentEquals ( annotation . getQualifiedName ()) )
67+ .filter (this :: isMapperAnnotation )
7068 .forEach (
7169 annotation ->
7270 processMapperAnnotation (roundEnv , descriptor , adapterPackageAndClass , annotation ));
7371 return false ;
7472 }
7573
74+ private boolean isMapperAnnotation (TypeElement annotation ) {
75+ return MAPPER .contentEquals (annotation .getQualifiedName ());
76+ }
77+
7678 private void processMapperAnnotation (
7779 final RoundEnvironment roundEnv ,
7880 final ConversionServiceAdapterDescriptor descriptor ,
7981 final Pair <String , String > adapterPackageAndClass ,
8082 final TypeElement annotation ) {
8183 final List <Pair <ClassName , ClassName >> fromToMappings =
8284 roundEnv .getElementsAnnotatedWith (annotation ).stream ()
83- .filter (mapper -> mapper . asType (). getKind () == DECLARED )
84- .filter (mapper -> getConverterSupertype ( mapper ). isPresent () )
85+ .filter (this :: isKindDeclared )
86+ .filter (this :: hasConverterSupertype )
8587 .map (this ::toConvertMethod )
8688 .filter (Objects ::nonNull )
8789 .map (ExecutableElement .class ::cast )
@@ -91,14 +93,22 @@ private void processMapperAnnotation(
9193 writeAdapterClassFile (descriptor , adapterPackageAndClass );
9294 }
9395
96+ private boolean hasConverterSupertype (Element mapper ) {
97+ return getConverterSupertype (mapper ).isPresent ();
98+ }
99+
100+ private boolean isKindDeclared (Element mapper ) {
101+ return mapper .asType ().getKind () == DECLARED ;
102+ }
103+
94104 private Pair <ClassName , ClassName > toFromToMapping (final ExecutableElement convert ) {
95105 return Pair .of (
96106 (ClassName )
97107 convert .getParameters ().stream ()
98108 .map (Element ::asType )
99109 .map (ClassName ::get )
100110 .findFirst ()
101- .get ( ),
111+ .orElseThrow ( NoSuchElementException :: new ),
102112 (ClassName ) ClassName .get (convert .getReturnType ()));
103113 }
104114
@@ -169,20 +179,23 @@ private void updateFromDeclaration(
169179 }
170180
171181 private String getConversionServiceName (
172- final Set <? extends TypeElement > annotations , final RoundEnvironment roundEnv ) {
173- AtomicReference <String > beanName = new AtomicReference <>();
174- for (final TypeElement annotation : annotations ) {
175- if (SPRING_MAPPER_CONFIG .contentEquals (annotation .getQualifiedName ())) {
176- roundEnv
177- .getElementsAnnotatedWith (annotation )
178- .stream ().findFirst ()
179- .ifPresent (element -> {
180- final SpringMapperConfig springMapperConfig = element .getAnnotation (SpringMapperConfig .class );
181- beanName .set (springMapperConfig .conversionServiceBeanName ());
182- });
183- }
184- }
185- return beanName .get ();
182+ final Set <? extends TypeElement > annotations , final RoundEnvironment roundEnv ) {
183+ return annotations .stream ()
184+ .filter (annotation -> SPRING_MAPPER_CONFIG .contentEquals (annotation .getQualifiedName ()))
185+ .findFirst ()
186+ .flatMap (annotation -> findFirstElementAnnotatedWith (roundEnv , annotation ))
187+ .map (this ::toSpringMapperConfig )
188+ .map (SpringMapperConfig ::conversionServiceBeanName )
189+ .orElse (null );
190+ }
191+
192+ private Optional <? extends Element > findFirstElementAnnotatedWith (
193+ final RoundEnvironment roundEnv , final TypeElement annotation ) {
194+ return roundEnv .getElementsAnnotatedWith (annotation ).stream ().findFirst ();
195+ }
196+
197+ private SpringMapperConfig toSpringMapperConfig (final Element element ) {
198+ return element .getAnnotation (SpringMapperConfig .class );
186199 }
187200
188201 private Optional <? extends TypeMirror > getConverterSupertype (final Element mapper ) {
0 commit comments