22
33import com .squareup .javapoet .*;
44import org .apache .commons .lang3 .StringUtils ;
5+ import org .apache .commons .lang3 .tuple .Pair ;
56
67import java .io .IOException ;
78import java .io .UncheckedIOException ;
89import java .io .Writer ;
910import java .time .Clock ;
1011import java .time .ZonedDateTime ;
11- import java .time .format .DateTimeFormatter ;
1212
13+ import static java .time .format .DateTimeFormatter .ISO_INSTANT ;
1314import static java .util .stream .Collectors .toList ;
1415import static javax .lang .model .element .Modifier .*;
1516
1617public class ConversionServiceAdapterGenerator {
18+ private static final String CONVERSION_SERVICE_PACKAGE_NAME = "org.springframework.core.convert" ;
19+ private static final String CONVERSION_SERVICE_CLASS_NAME = "ConversionService" ;
20+ private static final String CONVERSION_SERVICE_FIELD_NAME = "conversionService" ;
21+ private static final String QUALIFIER_ANNOTATION_PACKAGE_NAME =
22+ "org.springframework.beans.factory.annotation" ;
23+ private static final String QUALIFIER_ANNOTATION_CLASSS_NAME = "Qualifier" ;
24+ private static final String LAZY_ANNOTATION_PACKAGE_NAME =
25+ "org.springframework.context.annotation" ;
26+ private static final String LAZY_ANNOTATION_CLASS_NAME = "Lazy" ;
27+
1728 private final Clock clock ;
1829
1930 public ConversionServiceAdapterGenerator (final Clock clock ) {
2031 this .clock = clock ;
2132 }
2233
2334 public void writeConversionServiceAdapter (
24- ConversionServiceAdapterDescriptor descriptor , Writer out ) {
35+ final ConversionServiceAdapterDescriptor descriptor , final Writer out ) {
2536 try {
2637 JavaFile .builder (
2738 descriptor .getAdapterClassName ().packageName (),
@@ -65,24 +76,25 @@ private static ParameterSpec buildConstructorParameterSpec(
6576 ParameterSpec .builder (
6677 conversionServiceFieldSpec .type , conversionServiceFieldSpec .name , FINAL );
6778 if (StringUtils .isNotEmpty (descriptor .getConversionServiceBeanName ())) {
68- parameterBuilder .addAnnotation (buildQualifierANnotation (descriptor ));
79+ parameterBuilder .addAnnotation (buildQualifierAnnotation (descriptor ));
6980 }
7081 if (Boolean .TRUE .equals (descriptor .isLazyAnnotatedConversionServiceBean ())) {
7182 parameterBuilder .addAnnotation (buildLazyAnnotation ());
7283 }
7384 return parameterBuilder .build ();
7485 }
7586
76- private static AnnotationSpec buildQualifierANnotation (
87+ private static AnnotationSpec buildQualifierAnnotation (
7788 ConversionServiceAdapterDescriptor descriptor ) {
7889 return AnnotationSpec .builder (
79- ClassName .get ("org.springframework.beans.factory.annotation" , "Qualifier" ))
90+ ClassName .get (QUALIFIER_ANNOTATION_PACKAGE_NAME , QUALIFIER_ANNOTATION_CLASSS_NAME ))
8091 .addMember ("value" , "$S" , descriptor .getConversionServiceBeanName ())
8192 .build ();
8293 }
8394
8495 private static AnnotationSpec buildLazyAnnotation () {
85- return AnnotationSpec .builder (ClassName .get ("org.springframework.context.annotation" , "Lazy" ))
96+ return AnnotationSpec .builder (
97+ ClassName .get (LAZY_ANNOTATION_PACKAGE_NAME , LAZY_ANNOTATION_CLASS_NAME ))
8698 .build ();
8799 }
88100
@@ -91,9 +103,8 @@ private static String simpleName(final TypeName typeName) {
91103 if (rawType instanceof ArrayTypeName ) {
92104 return arraySimpleName ((ArrayTypeName ) rawType );
93105 } else if (rawType instanceof ClassName ) {
94- return ((ClassName )rawType ).simpleName ();
95- }
96- else return String .valueOf (typeName );
106+ return ((ClassName ) rawType ).simpleName ();
107+ } else return String .valueOf (typeName );
97108 }
98109
99110 private static String arraySimpleName (ArrayTypeName arrayTypeName ) {
@@ -115,35 +126,38 @@ private static Iterable<MethodSpec> buildMappingMethods(
115126 final FieldSpec injectedConversionServiceFieldSpec ) {
116127 return descriptor .getFromToMappings ().stream ()
117128 .map (
118- sourceTargetPair -> {
119- final ParameterSpec sourceParameterSpec =
120- buildSourceParameterSpec (sourceTargetPair .getLeft ());
121- return MethodSpec .methodBuilder (
122- "map"
123- + simpleName (sourceTargetPair .getLeft ())
124- + "To"
125- + simpleName (sourceTargetPair .getRight ()))
126- .addParameter (sourceParameterSpec )
127- .addModifiers (PUBLIC )
128- .returns (sourceTargetPair .getRight ())
129- .addStatement (
130- "return $N.convert($N, $T.class)" ,
131- injectedConversionServiceFieldSpec ,
132- sourceParameterSpec ,
133- rawType (sourceTargetPair .getRight ()))
134- .build ();
135- })
129+ sourceTargetPair ->
130+ toMappingMethodSpec (injectedConversionServiceFieldSpec , sourceTargetPair ))
136131 .collect (toList ());
137132 }
138133
134+ private static MethodSpec toMappingMethodSpec (
135+ final FieldSpec injectedConversionServiceFieldSpec ,
136+ final Pair <TypeName , TypeName > sourceTargetPair ) {
137+ final ParameterSpec sourceParameterSpec = buildSourceParameterSpec (sourceTargetPair .getLeft ());
138+ return MethodSpec .methodBuilder (
139+ String .format (
140+ "map%sTo%s" ,
141+ simpleName (sourceTargetPair .getLeft ()), simpleName (sourceTargetPair .getRight ())))
142+ .addParameter (sourceParameterSpec )
143+ .addModifiers (PUBLIC )
144+ .returns (sourceTargetPair .getRight ())
145+ .addStatement (
146+ "return $N.convert($N, $T.class)" ,
147+ injectedConversionServiceFieldSpec ,
148+ sourceParameterSpec ,
149+ rawType (sourceTargetPair .getRight ()))
150+ .build ();
151+ }
152+
139153 private static ParameterSpec buildSourceParameterSpec (final TypeName sourceClassName ) {
140154 return ParameterSpec .builder (sourceClassName , "source" , FINAL ).build ();
141155 }
142156
143157 private static FieldSpec buildConversionServiceFieldSpec () {
144158 return FieldSpec .builder (
145- ClassName .get ("org.springframework.core.convert" , "ConversionService" ),
146- "conversionService" ,
159+ ClassName .get (CONVERSION_SERVICE_PACKAGE_NAME , CONVERSION_SERVICE_CLASS_NAME ),
160+ CONVERSION_SERVICE_FIELD_NAME ,
147161 PRIVATE ,
148162 FINAL )
149163 .build ();
@@ -152,7 +166,7 @@ private static FieldSpec buildConversionServiceFieldSpec() {
152166 private AnnotationSpec buildGeneratedAnnotationSpec () {
153167 return AnnotationSpec .builder (ClassName .get ("javax.annotation" , "Generated" ))
154168 .addMember ("value" , "$S" , ConversionServiceAdapterGenerator .class .getName ())
155- .addMember ("date" , "$S" , DateTimeFormatter . ISO_INSTANT .format (ZonedDateTime .now (clock )))
169+ .addMember ("date" , "$S" , ISO_INSTANT .format (ZonedDateTime .now (clock )))
156170 .build ();
157171 }
158172}
0 commit comments