Skip to content

Commit

Permalink
Store TypeDescriptors as fields in generated ConversionServiceAdapter (
Browse files Browse the repository at this point in the history
…#108)

* Store TypeDescriptors as fields in generated ConversionServiceAdapter to reduce garbage

* Store TypeDescriptors as fields in generated ConversionServiceAdapter to reduce garbage

* Handle Arrays
  • Loading branch information
jbonzohln authored Jul 15, 2024
1 parent 84bd656 commit da772e6
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.squareup.javapoet.*;
import java.time.Clock;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Stream;

Expand Down Expand Up @@ -39,11 +41,36 @@ protected TypeSpec createMainTypeSpec(
return adapterClassTypeSpec
.addAnnotation(COMPONENT_ANNOTATION_CLASS_NAME)
.addField(conversionServiceFieldSpec)
.addFields(buildTypeDescriptorFields(descriptor, conversionServiceFieldSpec))
.addMethod(buildConstructorSpec(descriptor, conversionServiceFieldSpec))
.addMethods(buildMappingMethods(descriptor, conversionServiceFieldSpec))
.build();
}

private List<FieldSpec> buildTypeDescriptorFields(ConversionServiceAdapterDescriptor descriptor, FieldSpec conversionServiceFieldSpec)
{
return descriptor.getFromToMappings().stream()
.flatMap(fromToMapping -> Stream.of(fromToMapping.getSource(), fromToMapping.getTarget()))
.distinct()
.map(typeName -> FieldSpec.builder(TYPE_DESCRIPTOR_CLASS_NAME, fieldName(typeName), PRIVATE, STATIC, FINAL)
.initializer(String.format("%s", typeDescriptorFormat(typeName)), typeDescriptorArguments(typeName).toArray()).build())
.collect(toList());
}

private String fieldName(TypeName typeName) {
String fieldName = "TYPE_DESCRIPTOR_" + typeName.toString()
.replace('.', '_')
.replace('<', '_')
.replace('>', '_')
.replace("[]", "_ARRAY")
.toUpperCase(Locale.ROOT);
if (fieldName.lastIndexOf('_') == fieldName.length() - 1) {
return fieldName.substring(0, fieldName.length() - 1);
} else {
return fieldName;
}
}

private static MethodSpec buildConstructorSpec(
final ConversionServiceAdapterDescriptor descriptor,
final FieldSpec conversionServiceFieldSpec) {
Expand Down Expand Up @@ -110,28 +137,12 @@ private MethodSpec toMappingMethodSpec(
.addStatement(
String.format(
"return ($T) $N.convert($N, %s, %s)",
typeDescriptorFormat(fromToMapping.getSource()),
typeDescriptorFormat(fromToMapping.getTarget())),
allTypeDescriptorArguments(
injectedConversionServiceFieldSpec, sourceParameterSpec, fromToMapping))
fieldName(fromToMapping.getSource()),
fieldName(fromToMapping.getTarget())),
fromToMapping.getTarget(), injectedConversionServiceFieldSpec, sourceParameterSpec)
.build();
}

private Object[] allTypeDescriptorArguments(
final FieldSpec injectedConversionServiceFieldSpec,
final ParameterSpec sourceParameterSpec,
final FromToMapping fromToMapping) {
return concat(
concat(
Stream.of(
fromToMapping.getTarget(),
injectedConversionServiceFieldSpec,
sourceParameterSpec),
typeDescriptorArguments(fromToMapping.getSource())),
typeDescriptorArguments(fromToMapping.getTarget()))
.toArray();
}

private String typeDescriptorFormat(final TypeName typeName) {
if (typeName instanceof ParameterizedTypeName
&& isCollectionWithGenericParameter((ParameterizedTypeName) typeName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
date = "2020-03-29T15:21:34.236Z")
@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(
Expand All @@ -23,10 +31,10 @@ public ConversionServiceAdapter(
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
date = "2020-03-29T15:21:34.236Z")
@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(
Expand All @@ -23,10 +31,10 @@ public ConversionServiceAdapter(
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@

@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(
Expand All @@ -19,10 +27,10 @@ public ConversionServiceAdapter(
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@
date = "2020-03-29T15:21:34.236Z")
@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
this.conversionService = conversionService;
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@
@Generated("org.mapstruct.extensions.spring.converter.ConversionServiceAdapterGenerator")
@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
public ConversionServiceAdapter(
@Lazy final ConversionService conversionService) {
this.conversionService = conversionService;
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@
date = "2020-03-29T15:21:34.236Z")
@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
public ConversionServiceAdapter(
@Lazy final ConversionService conversionService) {
this.conversionService = conversionService;
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,26 @@
@Generated("org.mapstruct.extensions.spring.converter.ConversionServiceAdapterGenerator")
@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
public ConversionServiceAdapter(
@Lazy final ConversionService conversionService) {
this.conversionService = conversionService;
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,26 @@

@Component
public class ConversionServiceAdapter {
private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CAR = TypeDescriptor.valueOf(Car.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_TEST_CARDTO = TypeDescriptor.valueOf(CarDto.class);

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class));

private static final TypeDescriptor TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO = TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class));

private final ConversionService conversionService;

public ConversionServiceAdapter(@Lazy final ConversionService conversionService) {
public ConversionServiceAdapter(
@Lazy final ConversionService conversionService) {
this.conversionService = conversionService;
}

public CarDto toDto(final Car source) {
return (CarDto) conversionService.convert(source, TypeDescriptor.valueOf(Car.class), TypeDescriptor.valueOf(CarDto.class));
return (CarDto) conversionService.convert(source, TYPE_DESCRIPTOR_TEST_CAR, TYPE_DESCRIPTOR_TEST_CARDTO);
}

public List<CarDto> mapListOfCarToListOfCarDto(final List<Car> source) {
return (List<CarDto>) conversionService.convert(source, TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(Car.class)), TypeDescriptor.collection(List.class, TypeDescriptor.valueOf(CarDto.class)));
return (List<CarDto>) conversionService.convert(source, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CAR, TYPE_DESCRIPTOR_JAVA_UTIL_LIST_TEST_CARDTO);
}
}

0 comments on commit da772e6

Please sign in to comment.