|
14 | 14 | import static javax.lang.model.element.Modifier.*; |
15 | 15 |
|
16 | 16 | public class ConversionServiceAdapterGenerator { |
17 | | - private final Clock clock; |
18 | | - |
19 | | - public ConversionServiceAdapterGenerator(final Clock clock) { |
20 | | - this.clock = clock; |
21 | | - } |
22 | | - |
23 | | - public void writeConversionServiceAdapter( |
24 | | - ConversionServiceAdapterDescriptor descriptor, Writer out) { |
25 | | - try { |
26 | | - JavaFile.builder( |
27 | | - descriptor.getAdapterClassName().packageName(), |
28 | | - createConversionServiceTypeSpec(descriptor)) |
29 | | - .build() |
30 | | - .writeTo(out); |
31 | | - } catch (IOException e) { |
32 | | - throw new UncheckedIOException(e); |
33 | | - } |
| 17 | + private final Clock clock; |
| 18 | + |
| 19 | + public ConversionServiceAdapterGenerator(final Clock clock) { |
| 20 | + this.clock = clock; |
| 21 | + } |
| 22 | + |
| 23 | + public void writeConversionServiceAdapter( |
| 24 | + ConversionServiceAdapterDescriptor descriptor, Writer out) { |
| 25 | + try { |
| 26 | + JavaFile.builder( |
| 27 | + descriptor.getAdapterClassName().packageName(), |
| 28 | + createConversionServiceTypeSpec(descriptor)) |
| 29 | + .build() |
| 30 | + .writeTo(out); |
| 31 | + } catch (IOException e) { |
| 32 | + throw new UncheckedIOException(e); |
34 | 33 | } |
35 | | - |
36 | | - private TypeSpec createConversionServiceTypeSpec( |
37 | | - final ConversionServiceAdapterDescriptor descriptor) { |
38 | | - final FieldSpec conversionServiceFieldSpec = buildConversionServiceFieldSpec(); |
39 | | - return TypeSpec.classBuilder(descriptor.getAdapterClassName()) |
40 | | - .addModifiers(PUBLIC) |
41 | | - .addAnnotation(buildGeneratedAnnotationSpec()) |
42 | | - .addAnnotation(ClassName.get("org.springframework.stereotype", "Component")) |
43 | | - .addField(conversionServiceFieldSpec) |
44 | | - .addMethod(buildConstructorSpec(descriptor, conversionServiceFieldSpec)) |
45 | | - .addMethods(buildMappingMethods(descriptor, conversionServiceFieldSpec)) |
46 | | - .build(); |
| 34 | + } |
| 35 | + |
| 36 | + private TypeSpec createConversionServiceTypeSpec( |
| 37 | + final ConversionServiceAdapterDescriptor descriptor) { |
| 38 | + final FieldSpec conversionServiceFieldSpec = buildConversionServiceFieldSpec(); |
| 39 | + return TypeSpec.classBuilder(descriptor.getAdapterClassName()) |
| 40 | + .addModifiers(PUBLIC) |
| 41 | + .addAnnotation(buildGeneratedAnnotationSpec()) |
| 42 | + .addAnnotation(ClassName.get("org.springframework.stereotype", "Component")) |
| 43 | + .addField(conversionServiceFieldSpec) |
| 44 | + .addMethod(buildConstructorSpec(descriptor, conversionServiceFieldSpec)) |
| 45 | + .addMethods(buildMappingMethods(descriptor, conversionServiceFieldSpec)) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + private static MethodSpec buildConstructorSpec( |
| 50 | + final ConversionServiceAdapterDescriptor descriptor, |
| 51 | + final FieldSpec conversionServiceFieldSpec) { |
| 52 | + final ParameterSpec constructorParameterSpec = |
| 53 | + buildConstructorParameterSpec(descriptor, conversionServiceFieldSpec); |
| 54 | + return MethodSpec.constructorBuilder() |
| 55 | + .addModifiers(PUBLIC) |
| 56 | + .addParameter(constructorParameterSpec) |
| 57 | + .addStatement("this.$N = $N", conversionServiceFieldSpec, constructorParameterSpec) |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + private static ParameterSpec buildConstructorParameterSpec( |
| 62 | + final ConversionServiceAdapterDescriptor descriptor, |
| 63 | + final FieldSpec conversionServiceFieldSpec) { |
| 64 | + final ParameterSpec.Builder parameterBuilder = |
| 65 | + ParameterSpec.builder( |
| 66 | + conversionServiceFieldSpec.type, conversionServiceFieldSpec.name, FINAL); |
| 67 | + if (StringUtils.isNotEmpty(descriptor.getConversionServiceBeanName())) { |
| 68 | + parameterBuilder.addAnnotation(buildQualifierANnotation(descriptor)); |
47 | 69 | } |
48 | | - |
49 | | - private static MethodSpec buildConstructorSpec(final ConversionServiceAdapterDescriptor descriptor, final FieldSpec conversionServiceFieldSpec) { |
50 | | - final ParameterSpec constructorParameterSpec = buildConstructorParameterSpec(descriptor, conversionServiceFieldSpec); |
51 | | - return MethodSpec.constructorBuilder().addModifiers(PUBLIC).addParameter(constructorParameterSpec).addStatement("this.$N = $N", conversionServiceFieldSpec, constructorParameterSpec).build(); |
| 70 | + if (Boolean.TRUE.equals(descriptor.isLazyAnnotatedConversionServiceBean())) { |
| 71 | + parameterBuilder.addAnnotation(buildLazyAnnotation()); |
52 | 72 | } |
53 | | - |
54 | | - private static ParameterSpec buildConstructorParameterSpec(final ConversionServiceAdapterDescriptor descriptor, final FieldSpec conversionServiceFieldSpec) { |
55 | | - final ParameterSpec.Builder parameterBuilder = ParameterSpec.builder(conversionServiceFieldSpec.type, conversionServiceFieldSpec.name, FINAL); |
56 | | - if (StringUtils.isNotEmpty(descriptor.getConversionServiceBeanName())) { |
57 | | - parameterBuilder.addAnnotation(buildQualifierANnotation(descriptor)); |
58 | | - } |
59 | | - if (Boolean.TRUE.equals(descriptor.isLazyAnnotatedConversionServiceBean())) { |
60 | | - parameterBuilder.addAnnotation(buildLazyAnnotation()); |
61 | | - } |
62 | | - return parameterBuilder.build(); |
| 73 | + return parameterBuilder.build(); |
| 74 | + } |
| 75 | + |
| 76 | + private static AnnotationSpec buildQualifierANnotation( |
| 77 | + ConversionServiceAdapterDescriptor descriptor) { |
| 78 | + return AnnotationSpec.builder( |
| 79 | + ClassName.get("org.springframework.beans.factory.annotation", "Qualifier")) |
| 80 | + .addMember("value", "$S", descriptor.getConversionServiceBeanName()) |
| 81 | + .build(); |
| 82 | + } |
| 83 | + |
| 84 | + private static AnnotationSpec buildLazyAnnotation() { |
| 85 | + return AnnotationSpec.builder(ClassName.get("org.springframework.context.annotation", "Lazy")) |
| 86 | + .build(); |
| 87 | + } |
| 88 | + |
| 89 | + private static String simpleName(final TypeName typeName) { |
| 90 | + final TypeName rawType = rawType(typeName); |
| 91 | + if (rawType instanceof ArrayTypeName) { |
| 92 | + return arraySimpleName((ArrayTypeName) rawType); |
| 93 | + } else if (rawType instanceof ClassName) { |
| 94 | + return ((ClassName)rawType).simpleName(); |
63 | 95 | } |
64 | | - |
65 | | - private static AnnotationSpec buildQualifierANnotation(ConversionServiceAdapterDescriptor descriptor) { |
66 | | - return AnnotationSpec |
67 | | - .builder(ClassName.get("org.springframework.beans.factory.annotation", "Qualifier")) |
68 | | - .addMember("value", "$S", descriptor.getConversionServiceBeanName()) |
69 | | - .build(); |
70 | | - } |
71 | | - |
72 | | - private static AnnotationSpec buildLazyAnnotation() { |
73 | | - return AnnotationSpec |
74 | | - .builder(ClassName.get("org.springframework.context.annotation","Lazy")) |
75 | | - .build(); |
76 | | - } |
77 | | - |
78 | | - private static String simpleName(final TypeName typeName) { |
79 | | - return rawType(typeName).simpleName(); |
80 | | - } |
81 | | - |
82 | | - private static ClassName rawType(final TypeName typeName) { |
83 | | - if (typeName instanceof ParameterizedTypeName) { |
84 | | - return ((ParameterizedTypeName)typeName).rawType; |
85 | | - } |
86 | | - return (ClassName) typeName; |
87 | | - } |
88 | | - |
89 | | - private static Iterable<MethodSpec> buildMappingMethods( |
90 | | - final ConversionServiceAdapterDescriptor descriptor, |
91 | | - final FieldSpec injectedConversionServiceFieldSpec) { |
92 | | - return descriptor.getFromToMappings().stream() |
93 | | - .map( |
94 | | - sourceTargetPair -> { |
95 | | - final ParameterSpec sourceParameterSpec = |
96 | | - buildSourceParameterSpec(sourceTargetPair.getLeft()); |
97 | | - return MethodSpec.methodBuilder( |
98 | | - "map" |
99 | | - + simpleName(sourceTargetPair.getLeft()) |
100 | | - + "To" |
101 | | - + simpleName(sourceTargetPair.getRight())) |
102 | | - .addParameter(sourceParameterSpec) |
103 | | - .addModifiers(PUBLIC) |
104 | | - .returns(sourceTargetPair.getRight()) |
105 | | - .addStatement( |
106 | | - "return $N.convert($N, $T.class)", |
107 | | - injectedConversionServiceFieldSpec, |
108 | | - sourceParameterSpec, |
109 | | - rawType(sourceTargetPair.getRight())) |
110 | | - .build(); |
111 | | - }) |
112 | | - .collect(toList()); |
113 | | - } |
114 | | - |
115 | | - private static ParameterSpec buildSourceParameterSpec(final TypeName sourceClassName) { |
116 | | - return ParameterSpec.builder(sourceClassName, "source", FINAL).build(); |
117 | | - } |
118 | | - |
119 | | - private static FieldSpec buildConversionServiceFieldSpec() { |
120 | | - return FieldSpec.builder(ClassName.get("org.springframework.core.convert", "ConversionService"), "conversionService", PRIVATE, FINAL).build(); |
121 | | - } |
122 | | - |
123 | | - private AnnotationSpec buildGeneratedAnnotationSpec() { |
124 | | - return AnnotationSpec.builder(ClassName.get("javax.annotation", "Generated")) |
125 | | - .addMember("value", "$S", ConversionServiceAdapterGenerator.class.getName()) |
126 | | - .addMember("date", "$S", DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now(clock))) |
127 | | - .build(); |
| 96 | + else return String.valueOf(typeName); |
| 97 | + } |
| 98 | + |
| 99 | + private static String arraySimpleName(ArrayTypeName arrayTypeName) { |
| 100 | + return "ArrayOf" |
| 101 | + + (arrayTypeName.componentType instanceof ArrayTypeName |
| 102 | + ? arraySimpleName((ArrayTypeName) arrayTypeName.componentType) |
| 103 | + : arrayTypeName.componentType); |
| 104 | + } |
| 105 | + |
| 106 | + private static TypeName rawType(final TypeName typeName) { |
| 107 | + if (typeName instanceof ParameterizedTypeName) { |
| 108 | + return ((ParameterizedTypeName) typeName).rawType; |
128 | 109 | } |
| 110 | + return typeName; |
| 111 | + } |
| 112 | + |
| 113 | + private static Iterable<MethodSpec> buildMappingMethods( |
| 114 | + final ConversionServiceAdapterDescriptor descriptor, |
| 115 | + final FieldSpec injectedConversionServiceFieldSpec) { |
| 116 | + return descriptor.getFromToMappings().stream() |
| 117 | + .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 | + }) |
| 136 | + .collect(toList()); |
| 137 | + } |
| 138 | + |
| 139 | + private static ParameterSpec buildSourceParameterSpec(final TypeName sourceClassName) { |
| 140 | + return ParameterSpec.builder(sourceClassName, "source", FINAL).build(); |
| 141 | + } |
| 142 | + |
| 143 | + private static FieldSpec buildConversionServiceFieldSpec() { |
| 144 | + return FieldSpec.builder( |
| 145 | + ClassName.get("org.springframework.core.convert", "ConversionService"), |
| 146 | + "conversionService", |
| 147 | + PRIVATE, |
| 148 | + FINAL) |
| 149 | + .build(); |
| 150 | + } |
| 151 | + |
| 152 | + private AnnotationSpec buildGeneratedAnnotationSpec() { |
| 153 | + return AnnotationSpec.builder(ClassName.get("javax.annotation", "Generated")) |
| 154 | + .addMember("value", "$S", ConversionServiceAdapterGenerator.class.getName()) |
| 155 | + .addMember("date", "$S", DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now(clock))) |
| 156 | + .build(); |
| 157 | + } |
129 | 158 | } |
0 commit comments