Skip to content

Commit da772e6

Browse files
authored
Store TypeDescriptors as fields in generated ConversionServiceAdapter (#108)
* Store TypeDescriptors as fields in generated ConversionServiceAdapter to reduce garbage * Store TypeDescriptors as fields in generated ConversionServiceAdapter to reduce garbage * Handle Arrays
1 parent 84bd656 commit da772e6

9 files changed

+118
-39
lines changed

extensions/src/main/java/org/mapstruct/extensions/spring/converter/ConversionServiceAdapterGenerator.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import com.squareup.javapoet.*;
1010
import java.time.Clock;
11+
import java.util.List;
12+
import java.util.Locale;
1113
import java.util.Optional;
1214
import java.util.stream.Stream;
1315

@@ -39,11 +41,36 @@ protected TypeSpec createMainTypeSpec(
3941
return adapterClassTypeSpec
4042
.addAnnotation(COMPONENT_ANNOTATION_CLASS_NAME)
4143
.addField(conversionServiceFieldSpec)
44+
.addFields(buildTypeDescriptorFields(descriptor, conversionServiceFieldSpec))
4245
.addMethod(buildConstructorSpec(descriptor, conversionServiceFieldSpec))
4346
.addMethods(buildMappingMethods(descriptor, conversionServiceFieldSpec))
4447
.build();
4548
}
4649

50+
private List<FieldSpec> buildTypeDescriptorFields(ConversionServiceAdapterDescriptor descriptor, FieldSpec conversionServiceFieldSpec)
51+
{
52+
return descriptor.getFromToMappings().stream()
53+
.flatMap(fromToMapping -> Stream.of(fromToMapping.getSource(), fromToMapping.getTarget()))
54+
.distinct()
55+
.map(typeName -> FieldSpec.builder(TYPE_DESCRIPTOR_CLASS_NAME, fieldName(typeName), PRIVATE, STATIC, FINAL)
56+
.initializer(String.format("%s", typeDescriptorFormat(typeName)), typeDescriptorArguments(typeName).toArray()).build())
57+
.collect(toList());
58+
}
59+
60+
private String fieldName(TypeName typeName) {
61+
String fieldName = "TYPE_DESCRIPTOR_" + typeName.toString()
62+
.replace('.', '_')
63+
.replace('<', '_')
64+
.replace('>', '_')
65+
.replace("[]", "_ARRAY")
66+
.toUpperCase(Locale.ROOT);
67+
if (fieldName.lastIndexOf('_') == fieldName.length() - 1) {
68+
return fieldName.substring(0, fieldName.length() - 1);
69+
} else {
70+
return fieldName;
71+
}
72+
}
73+
4774
private static MethodSpec buildConstructorSpec(
4875
final ConversionServiceAdapterDescriptor descriptor,
4976
final FieldSpec conversionServiceFieldSpec) {
@@ -110,28 +137,12 @@ private MethodSpec toMappingMethodSpec(
110137
.addStatement(
111138
String.format(
112139
"return ($T) $N.convert($N, %s, %s)",
113-
typeDescriptorFormat(fromToMapping.getSource()),
114-
typeDescriptorFormat(fromToMapping.getTarget())),
115-
allTypeDescriptorArguments(
116-
injectedConversionServiceFieldSpec, sourceParameterSpec, fromToMapping))
140+
fieldName(fromToMapping.getSource()),
141+
fieldName(fromToMapping.getTarget())),
142+
fromToMapping.getTarget(), injectedConversionServiceFieldSpec, sourceParameterSpec)
117143
.build();
118144
}
119145

120-
private Object[] allTypeDescriptorArguments(
121-
final FieldSpec injectedConversionServiceFieldSpec,
122-
final ParameterSpec sourceParameterSpec,
123-
final FromToMapping fromToMapping) {
124-
return concat(
125-
concat(
126-
Stream.of(
127-
fromToMapping.getTarget(),
128-
injectedConversionServiceFieldSpec,
129-
sourceParameterSpec),
130-
typeDescriptorArguments(fromToMapping.getSource())),
131-
typeDescriptorArguments(fromToMapping.getTarget()))
132-
.toArray();
133-
}
134-
135146
private String typeDescriptorFormat(final TypeName typeName) {
136147
if (typeName instanceof ParameterizedTypeName
137148
&& isCollectionWithGenericParameter((ParameterizedTypeName) typeName)) {

extensions/src/test/resources/ConversionServiceAdapterCustomBeanJava8Generated.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
date = "2020-03-29T15:21:34.236Z")
1616
@Component
1717
public class ConversionServiceAdapter {
18+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
19+
20+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
21+
22+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
23+
24+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
25+
1826
private final ConversionService conversionService;
1927

2028
public ConversionServiceAdapter(
@@ -23,10 +31,10 @@ public ConversionServiceAdapter(
2331
}
2432

2533
public CarDto toDto(final Car source) {
26-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
34+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2735
}
2836

2937
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
30-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
38+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
3139
}
3240
}

extensions/src/test/resources/ConversionServiceAdapterCustomBeanJava9PlusGenerated.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515
date = "2020-03-29T15:21:34.236Z")
1616
@Component
1717
public class ConversionServiceAdapter {
18+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
19+
20+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
21+
22+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
23+
24+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
25+
1826
private final ConversionService conversionService;
1927

2028
public ConversionServiceAdapter(
@@ -23,10 +31,10 @@ public ConversionServiceAdapter(
2331
}
2432

2533
public CarDto toDto(final Car source) {
26-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
34+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2735
}
2836

2937
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
30-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
38+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
3139
}
3240
}

extensions/src/test/resources/ConversionServiceAdapterCustomBeanNoGenerated.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
@Component
1313
public class ConversionServiceAdapter {
14+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
15+
16+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
17+
18+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
19+
20+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
21+
1422
private final ConversionService conversionService;
1523

1624
public ConversionServiceAdapter(
@@ -19,10 +27,10 @@ public ConversionServiceAdapter(
1927
}
2028

2129
public CarDto toDto(final Car source) {
22-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
30+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2331
}
2432

2533
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
26-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
34+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
2735
}
2836
}

extensions/src/test/resources/ConversionServiceAdapterJava8Generated.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
date = "2020-03-29T15:21:34.236Z")
1515
@Component
1616
public class ConversionServiceAdapter {
17+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
18+
19+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
20+
21+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
22+
23+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
24+
1725
private final ConversionService conversionService;
1826

1927
public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
2028
this.conversionService = conversionService;
2129
}
2230

2331
public CarDto toDto(final Car source) {
24-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
32+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2533
}
2634

2735
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
28-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
36+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
2937
}
3038
}

extensions/src/test/resources/ConversionServiceAdapterJava8GeneratedNoDate.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@
1212
@Generated("org.mapstruct.extensions.spring.converter.ConversionServiceAdapterGenerator")
1313
@Component
1414
public class ConversionServiceAdapter {
15+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
16+
17+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
18+
19+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
20+
21+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
22+
1523
private final ConversionService conversionService;
1624

17-
public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
25+
public ConversionServiceAdapter(
26+
@Lazy final ConversionService conversionService) {
1827
this.conversionService = conversionService;
1928
}
2029

2130
public CarDto toDto(final Car source) {
22-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
31+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2332
}
2433

2534
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
26-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
35+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
2736
}
2837
}

extensions/src/test/resources/ConversionServiceAdapterJava9PlusGenerated.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@
1414
date = "2020-03-29T15:21:34.236Z")
1515
@Component
1616
public class ConversionServiceAdapter {
17+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
18+
19+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
20+
21+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
22+
23+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
24+
1725
private final ConversionService conversionService;
1826

19-
public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
27+
public ConversionServiceAdapter(
28+
@Lazy final ConversionService conversionService) {
2029
this.conversionService = conversionService;
2130
}
2231

2332
public CarDto toDto(final Car source) {
24-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
33+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2534
}
2635

2736
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
28-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
37+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
2938
}
3039
}

extensions/src/test/resources/ConversionServiceAdapterJava9PlusGeneratedNoDate.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,26 @@
1212
@Generated("org.mapstruct.extensions.spring.converter.ConversionServiceAdapterGenerator")
1313
@Component
1414
public class ConversionServiceAdapter {
15+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
16+
17+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
18+
19+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
20+
21+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
22+
1523
private final ConversionService conversionService;
1624

17-
public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
25+
public ConversionServiceAdapter(
26+
@Lazy final ConversionService conversionService) {
1827
this.conversionService = conversionService;
1928
}
2029

2130
public CarDto toDto(final Car source) {
22-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
31+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2332
}
2433

2534
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
26-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
35+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
2736
}
2837
}

extensions/src/test/resources/ConversionServiceAdapterNoGenerated.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,26 @@
1010

1111
@Component
1212
public class ConversionServiceAdapter {
13+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);
14+
15+
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);
16+
17+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));
18+
19+
private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));
20+
1321
private final ConversionService conversionService;
1422

15-
public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
23+
public ConversionServiceAdapter(
24+
@Lazy final ConversionService conversionService) {
1625
this.conversionService = conversionService;
1726
}
1827

1928
public CarDto toDto(final Car source) {
20-
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
29+
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
2130
}
2231

2332
public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
24-
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
33+
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
2534
}
2635
}

0 commit comments

Comments
 (0)