mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-06 17:18:43 +08:00
AutoMapper 增加 useEnums 属性,支持手动配置转换时需要的枚举,解决跨模块枚举无法自动转换的问题。
This commit is contained in:
parent
5c571181a2
commit
01b72c92ab
@ -12,7 +12,6 @@ import io.github.linpeilie.processor.metadata.AbstractAdapterMethodMetadata;
|
||||
import io.github.linpeilie.processor.metadata.AdapterMapMethodMetadata;
|
||||
import io.github.linpeilie.processor.metadata.AdapterMethodMetadata;
|
||||
import io.github.linpeilie.utils.ClassUtil;
|
||||
import io.github.linpeilie.utils.MapperUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -29,6 +29,7 @@ import io.github.linpeilie.processor.metadata.AutoMapMapperMetadata;
|
||||
import io.github.linpeilie.processor.metadata.AutoMapperMetadata;
|
||||
import io.github.linpeilie.processor.metadata.AutoMappingMetadata;
|
||||
import io.github.linpeilie.processor.utils.ExceptionUtils;
|
||||
import io.github.linpeilie.processor.utils.MapperUtils;
|
||||
import io.github.linpeilie.processor.utils.ObjectUtils;
|
||||
import io.github.linpeilie.utils.CollectionUtils;
|
||||
import io.github.linpeilie.utils.StrUtil;
|
||||
@ -756,7 +757,15 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
|
||||
AutoMapperMetadata metadata = initAutoMapperMetadata(source, target, autoMapperGem.cycleAvoiding().get());
|
||||
|
||||
metadata.setUsesClassNameList(transToClassNameList(autoMapperGem.uses().get()));
|
||||
List<ClassName> usesClassNameList = transToClassNameList(autoMapperGem.uses().get());
|
||||
List<ClassName> useEnumClassNameList = transToClassNameList(autoMapperGem.useEnums().get()).stream()
|
||||
.map(enumClass -> ClassName.get(MapperUtils.getMapperPackage(enumClass.packageName()),
|
||||
MapperUtils.getEnumMapperClassName(enumClass.simpleName())))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
usesClassNameList.addAll(useEnumClassNameList);
|
||||
|
||||
metadata.setUsesClassNameList(usesClassNameList);
|
||||
metadata.setImportsClassNameList(transToClassNameList(autoMapperGem.imports().get()));
|
||||
metadata.setFieldMappingList(autoMappingMetadataList);
|
||||
metadata.setFieldReverseMappingList(reverseMappingMetadataList);
|
||||
@ -966,7 +975,7 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
||||
|
||||
private List<ClassName> transToClassNameList(List<TypeMirror> typeMirrors) {
|
||||
if (typeMirrors == null) {
|
||||
return Collections.emptyList();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return typeMirrors.stream()
|
||||
.map(this::transToClassName)
|
||||
|
||||
@ -2,6 +2,7 @@ package io.github.linpeilie.processor.metadata;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import io.github.linpeilie.processor.AutoMapperProperties;
|
||||
import io.github.linpeilie.processor.utils.MapperUtils;
|
||||
import io.github.linpeilie.utils.StrUtil;
|
||||
|
||||
public abstract class AbstractMapperMetadata {
|
||||
@ -9,8 +10,7 @@ public abstract class AbstractMapperMetadata {
|
||||
protected ClassName sourceClassName;
|
||||
|
||||
public String mapperPackage() {
|
||||
return StrUtil.isNotEmpty(AutoMapperProperties.getMapperPackage())
|
||||
? AutoMapperProperties.getMapperPackage() : sourceClassName.packageName();
|
||||
return MapperUtils.getMapperPackage(sourceClassName.packageName());
|
||||
}
|
||||
|
||||
public abstract String mapperName();
|
||||
|
||||
@ -2,7 +2,6 @@ package io.github.linpeilie.processor.metadata;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import io.github.linpeilie.utils.ClassUtil;
|
||||
import io.github.linpeilie.utils.MapperUtils;
|
||||
|
||||
public class AdapterMethodMetadata extends AbstractAdapterMethodMetadata {
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package io.github.linpeilie.processor.metadata;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import com.squareup.javapoet.TypeName;
|
||||
import io.github.linpeilie.processor.utils.MapperUtils;
|
||||
|
||||
public class AutoEnumMapperMetadata extends AbstractMapperMetadata {
|
||||
|
||||
@ -27,7 +27,7 @@ public class AutoEnumMapperMetadata extends AbstractMapperMetadata {
|
||||
|
||||
@Override
|
||||
public String mapperName() {
|
||||
return sourceClassName.simpleName() + "Mapper";
|
||||
return MapperUtils.getEnumMapperClassName(sourceClassName.simpleName());
|
||||
}
|
||||
|
||||
public String toEnumMethodName() {
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package io.github.linpeilie.processor.metadata;
|
||||
|
||||
import com.squareup.javapoet.ClassName;
|
||||
import io.github.linpeilie.utils.MapperUtils;
|
||||
import io.github.linpeilie.processor.utils.MapperUtils;
|
||||
import io.github.linpeilie.utils.StrUtil;
|
||||
import java.util.List;
|
||||
import org.mapstruct.NullValueMappingStrategy;
|
||||
@ -23,6 +23,8 @@ public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
|
||||
private List<ClassName> usesClassNameList;
|
||||
|
||||
private List<ClassName> useEnumClassNameList;
|
||||
|
||||
private List<ClassName> importsClassNameList;
|
||||
|
||||
private List<AutoMappingMetadata> fieldMappingList;
|
||||
@ -107,6 +109,14 @@ public class AutoMapperMetadata extends AbstractMapperMetadata {
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<ClassName> getUseEnumClassNameList() {
|
||||
return useEnumClassNameList;
|
||||
}
|
||||
|
||||
public void setUseEnumClassNameList(List<ClassName> useEnumClassNameList) {
|
||||
this.useEnumClassNameList = useEnumClassNameList;
|
||||
}
|
||||
|
||||
public List<ClassName> getImportsClassNameList() {
|
||||
return importsClassNameList;
|
||||
}
|
||||
|
||||
@ -1,4 +1,7 @@
|
||||
package io.github.linpeilie.utils;
|
||||
package io.github.linpeilie.processor.utils;
|
||||
|
||||
import io.github.linpeilie.processor.AutoMapperProperties;
|
||||
import io.github.linpeilie.utils.StrUtil;
|
||||
|
||||
public class MapperUtils {
|
||||
|
||||
@ -11,4 +14,14 @@ public class MapperUtils {
|
||||
return mapperClassName.substring(0, 1).toUpperCase() + mapperClassName.substring(1) + "Mapper";
|
||||
}
|
||||
|
||||
public static String getEnumMapperClassName(String sourceSimpleName) {
|
||||
return sourceSimpleName + "Mapper";
|
||||
}
|
||||
|
||||
public static String getMapperPackage(String sourcePackage) {
|
||||
return StrUtil.isNotEmpty(AutoMapperProperties.getMapperPackage())
|
||||
? AutoMapperProperties.getMapperPackage() : sourcePackage;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -29,6 +29,11 @@ public @interface AutoMapper {
|
||||
|
||||
Class<?>[] uses() default {};
|
||||
|
||||
/**
|
||||
* 转换需要依赖的枚举,依赖的枚举,需要被 AutoEnumMapper 所注解
|
||||
*/
|
||||
Class<?>[] useEnums() default {};
|
||||
|
||||
Class<?>[] imports() default {};
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user