mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-07 17:48:35 +08:00
修改避免对象循环嵌套的属性名称
This commit is contained in:
parent
d34c417681
commit
7cf7132d1d
@ -5,7 +5,7 @@ import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AutoMapper(target = EmployeeDto.class, cycles = true)
|
||||
@AutoMapper(target = EmployeeDto.class, cycleAvoiding = true)
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
|
||||
@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@AutoMapper(target = ProductDto.class, cycles = true)
|
||||
@AutoMapper(target = ProductDto.class, cycleAvoiding = true)
|
||||
public class Product {
|
||||
|
||||
private Long id;
|
||||
|
||||
@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@AutoMapper(target = Product.class, cycles = true)
|
||||
@AutoMapper(target = Product.class, cycleAvoiding = true)
|
||||
public class ProductDto {
|
||||
|
||||
private Long id;
|
||||
|
||||
@ -6,7 +6,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@AutoMapper(target = ProductPropertyDto.class, cycles = true)
|
||||
@AutoMapper(target = ProductPropertyDto.class, cycleAvoiding = true)
|
||||
public class ProductProperty {
|
||||
|
||||
private Long id;
|
||||
|
||||
@ -6,7 +6,7 @@ import lombok.EqualsAndHashCode;
|
||||
import lombok.ToString;
|
||||
|
||||
@Data
|
||||
@AutoMapper(target = ProductProperty.class, cycles = true)
|
||||
@AutoMapper(target = ProductProperty.class, cycleAvoiding = true)
|
||||
public class ProductPropertyDto {
|
||||
|
||||
private Long id;
|
||||
|
||||
@ -32,8 +32,8 @@ public abstract class AbstractAdapterMapperGenerator {
|
||||
public void write(ProcessingEnvironment processingEnv,
|
||||
Collection<AbstractAdapterMethodMetadata> adapterMethods,
|
||||
String adapterClassName,
|
||||
boolean cycle) {
|
||||
write(processingEnv, createAdapterTypeSpec(adapterClassName, adapterMethods, cycle));
|
||||
boolean cycleAvoiding) {
|
||||
write(processingEnv, createAdapterTypeSpec(adapterClassName, adapterMethods, cycleAvoiding));
|
||||
}
|
||||
|
||||
private void write(ProcessingEnvironment processingEnv, TypeSpec typeSpec) {
|
||||
@ -55,17 +55,17 @@ public abstract class AbstractAdapterMapperGenerator {
|
||||
|
||||
protected TypeSpec createAdapterTypeSpec(String adapterClassName,
|
||||
Collection<AbstractAdapterMethodMetadata> adapterMethods,
|
||||
boolean cycle) {
|
||||
boolean cycleAvoiding) {
|
||||
List<MethodSpec> methods = adapterMethods.stream()
|
||||
.filter(method -> !cycle || method.needCycleAvoiding())
|
||||
.map(method -> buildProxyMethod(method, cycle))
|
||||
.filter(method -> !cycleAvoiding || method.needCycleAvoiding())
|
||||
.map(method -> buildProxyMethod(method, cycleAvoiding))
|
||||
.flatMap(Collection::stream)
|
||||
.collect(Collectors.toList());
|
||||
if (methods.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return createTypeSpec(methods, adapterClassName,
|
||||
cycle ? ClassName.get(adapterPackage(), AutoMapperProperties.getAdapterClassName()) : null);
|
||||
cycleAvoiding ? ClassName.get(adapterPackage(), AutoMapperProperties.getAdapterClassName()) : null);
|
||||
}
|
||||
|
||||
protected TypeSpec createTypeSpec(List<MethodSpec> methods, String adapterClassName, ClassName superClass) {
|
||||
@ -97,9 +97,9 @@ public abstract class AbstractAdapterMapperGenerator {
|
||||
return source;
|
||||
}
|
||||
|
||||
private List<MethodSpec> buildProxyMethod(AbstractAdapterMethodMetadata adapterMethod, boolean cycle) {
|
||||
private List<MethodSpec> buildProxyMethod(AbstractAdapterMethodMetadata adapterMethod, boolean cycleAvoiding) {
|
||||
List<MethodSpec> methodSpecs = new ArrayList<>();
|
||||
if (cycle) {
|
||||
if (cycleAvoiding) {
|
||||
methodSpecs.addAll(buildCycleAvoidingProxyMethod(adapterMethod));
|
||||
} else {
|
||||
methodSpecs.addAll(buildDefaultProxyMethod(adapterMethod, null));
|
||||
|
||||
@ -540,12 +540,12 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
AutoMapperMetadata reverseMapperMetadata =
|
||||
initAutoMapperMetadata(autoMapperMetadata.getTargetClassName(),
|
||||
autoMapperMetadata.getSourceClassName(),
|
||||
autoMapperMetadata.isCycles());
|
||||
autoMapperMetadata.isCycleAvoiding());
|
||||
reverseMapperMetadata.setConvertGenerate(autoMapperMetadata.isReverseConvertGenerate());
|
||||
reverseMapperMetadata.setUsesClassNameList(autoMapperMetadata.getUsesClassNameList());
|
||||
reverseMapperMetadata.setImportsClassNameList(autoMapperMetadata.getImportsClassNameList());
|
||||
reverseMapperMetadata.setCycles(autoMapperMetadata.isCycles());
|
||||
if (reverseMapperMetadata.isCycles()) {
|
||||
reverseMapperMetadata.setCycleAvoiding(autoMapperMetadata.isCycleAvoiding());
|
||||
if (reverseMapperMetadata.isCycleAvoiding()) {
|
||||
reverseMapperMetadata.setSuperClass(ClassName.get(ContextConstants.BaseCycleAvoidingMapper.packageName,
|
||||
ContextConstants.BaseCycleAvoidingMapper.className));
|
||||
} else {
|
||||
@ -588,7 +588,7 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
}
|
||||
AdapterMethodMetadata adapterMethodMetadata =
|
||||
AdapterMethodMetadata.newInstance(metadata.getSourceClassName(), metadata.getTargetClassName(),
|
||||
metadata.mapperClass(), metadata.isCycles());
|
||||
metadata.mapperClass(), metadata.isCycleAvoiding());
|
||||
methodMap.putIfAbsent(adapterMethodMetadata.getMethodName(), adapterMethodMetadata);
|
||||
}
|
||||
|
||||
@ -598,14 +598,14 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
mapMethodMap.putIfAbsent(adapterMapMethodMetadata.getMethodName(), adapterMapMethodMetadata);
|
||||
}
|
||||
|
||||
private AutoMapperMetadata initAutoMapperMetadata(ClassName source, ClassName target, boolean cycles) {
|
||||
private AutoMapperMetadata initAutoMapperMetadata(ClassName source, ClassName target, boolean cycleAvoiding) {
|
||||
AutoMapperMetadata metadata = new AutoMapperMetadata();
|
||||
|
||||
metadata.setSourceClassName(source);
|
||||
metadata.setTargetClassName(target);
|
||||
metadata.setSuperGenerics(new ClassName[] {source, target});
|
||||
ClassName mapStructConfigClass;
|
||||
if (cycles) {
|
||||
if (cycleAvoiding) {
|
||||
mapStructConfigClass = ClassName.get(AutoMapperProperties.getConfigPackage(),
|
||||
AutoMapperProperties.getCycleAvoidingConfigClassName());
|
||||
} else {
|
||||
@ -680,7 +680,7 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
List<AutoMappingMetadata> reverseMappingMetadataList = buildFieldReverseMappingMetadata((TypeElement) ele);
|
||||
reverseMappingMetadataList.removeIf(mappingMetadata -> !isTargetFieldMapping(target, mappingMetadata));
|
||||
|
||||
AutoMapperMetadata metadata = initAutoMapperMetadata(source, target, autoMapper.cycles());
|
||||
AutoMapperMetadata metadata = initAutoMapperMetadata(source, target, autoMapper.cycleAvoiding());
|
||||
|
||||
metadata.setUsesClassNameList(uses);
|
||||
metadata.setImportsClassNameList(importsClassNameList);
|
||||
@ -688,8 +688,8 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
metadata.setFieldReverseMappingList(reverseMappingMetadataList);
|
||||
metadata.setConvertGenerate(autoMapper.convertGenerate());
|
||||
metadata.setReverseConvertGenerate(autoMapper.reverseConvertGenerate());
|
||||
metadata.setCycles(autoMapper.cycles());
|
||||
if (metadata.isCycles()) {
|
||||
metadata.setCycleAvoiding(autoMapper.cycleAvoiding());
|
||||
if (metadata.isCycleAvoiding()) {
|
||||
metadata.setSuperClass(ClassName.get(ContextConstants.BaseCycleAvoidingMapper.packageName,
|
||||
ContextConstants.BaseCycleAvoidingMapper.className));
|
||||
} else {
|
||||
|
||||
@ -59,7 +59,7 @@ public class AutoMapperGenerator {
|
||||
.build();
|
||||
if (metadata.getFieldMappingList() != null && !metadata.getFieldMappingList().isEmpty()) {
|
||||
builder.addMethod(addConvertMethodSpec(
|
||||
metadata.isCycles() ? CollectionUtils.newArrayList(source, context) : Collections.singletonList(source),
|
||||
metadata.isCycleAvoiding() ? CollectionUtils.newArrayList(source, context) : Collections.singletonList(source),
|
||||
metadata.getFieldMappingList(),
|
||||
targetClassName,
|
||||
CONVERT_METHOD_NAME));
|
||||
@ -69,13 +69,13 @@ public class AutoMapperGenerator {
|
||||
if (targetIsImmutable) {
|
||||
builder.addMethod(
|
||||
addEmptyConvertMethodForImmutableEntity(
|
||||
metadata.isCycles() ? CollectionUtils.newArrayList(source, target,
|
||||
metadata.isCycleAvoiding() ? CollectionUtils.newArrayList(source, target,
|
||||
context) : CollectionUtils.newArrayList(source, target),
|
||||
targetClassName,
|
||||
CONVERT_METHOD_NAME));
|
||||
} else if (metadata.getFieldMappingList() != null && !metadata.getFieldMappingList().isEmpty()) {
|
||||
builder.addMethod(addConvertMethodSpec(
|
||||
metadata.isCycles() ? CollectionUtils.newArrayList(source, target,
|
||||
metadata.isCycleAvoiding() ? CollectionUtils.newArrayList(source, target,
|
||||
context) : CollectionUtils.newArrayList(source, target),
|
||||
metadata.getFieldMappingList(),
|
||||
targetClassName,
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
package io.github.linpeilie.processor.metadata;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import io.github.linpeilie.processor.AutoMapperProperties;
|
||||
import java.util.List;
|
||||
import org.mapstruct.ReportingPolicy;
|
||||
|
||||
public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
|
||||
@ -27,7 +25,7 @@ public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
|
||||
private boolean reverseConvertGenerate;
|
||||
|
||||
private boolean cycles;
|
||||
private boolean cycleAvoiding;
|
||||
|
||||
public String mapperName() {
|
||||
return sourceClassName.simpleName() + "To" + targetClassName.simpleName() + "Mapper";
|
||||
@ -116,11 +114,11 @@ public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
this.convertGenerate = convertGenerate;
|
||||
}
|
||||
|
||||
public boolean isCycles() {
|
||||
return cycles;
|
||||
public boolean isCycleAvoiding() {
|
||||
return cycleAvoiding;
|
||||
}
|
||||
|
||||
public void setCycles(boolean cycles) {
|
||||
this.cycles = cycles;
|
||||
public void setCycleAvoiding(boolean cycleAvoiding) {
|
||||
this.cycleAvoiding = cycleAvoiding;
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,10 +33,13 @@ public @interface AutoMapper {
|
||||
boolean reverseConvertGenerate() default true;
|
||||
|
||||
/**
|
||||
* 是否有循环依赖对象
|
||||
* 是否需要避免对象循环嵌套
|
||||
* <p>
|
||||
* 循环嵌套:A中有属性类型B,B中有属性类型A,且可能对象之间互相引用
|
||||
* </p>
|
||||
*
|
||||
* @return true: 有循环依赖对象 false: 没有循环依赖对象
|
||||
* @return true: 需要避免对象循环嵌套;false:不需要
|
||||
*/
|
||||
boolean cycles() default false;
|
||||
boolean cycleAvoiding() default false;
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user