Merge pull request #141 from fangzhengjin/issues-140

feat(MapperConfig): 添加 uses 属性以配置全局共享的自定义转换类
This commit is contained in:
easii 2025-05-26 14:13:07 +08:00 committed by GitHub
commit 95d3160703
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 3 deletions

View File

@ -367,6 +367,9 @@ public class AutoMapperProcessor extends AbstractProcessor {
if (mapperConfigGem == null || !mapperConfigGem.isValid()) { if (mapperConfigGem == null || !mapperConfigGem.isValid()) {
return; return;
} }
if (mapperConfigGem.uses().hasValue()) {
AutoMapperProperties.setUses(mapperConfigGem.uses().get());
}
if (mapperConfigGem.mapperPackage().hasValue()) { if (mapperConfigGem.mapperPackage().hasValue()) {
AutoMapperProperties.setMapperPackage(mapperConfigGem.mapperPackage().get()); AutoMapperProperties.setMapperPackage(mapperConfigGem.mapperPackage().get());
} }

View File

@ -3,12 +3,14 @@ package io.github.linpeilie.processor;
import cn.easii.tutelary.deps.com.squareup.javapoet.ClassName; import cn.easii.tutelary.deps.com.squareup.javapoet.ClassName;
import io.github.linpeilie.processor.utils.FileUtils; import io.github.linpeilie.processor.utils.FileUtils;
import io.github.linpeilie.processor.utils.IncrementMarkUtils; import io.github.linpeilie.processor.utils.IncrementMarkUtils;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.NullValuePropertyMappingStrategy; import javax.lang.model.type.TypeMirror;
import org.mapstruct.ReportingPolicy; import java.util.List;
public class AutoMapperProperties { public class AutoMapperProperties {
private static List<TypeMirror> uses;
private static String mapperPackage; private static String mapperPackage;
private static String componentModel = ContextConstants.ComponentModelConfig.defaultComponentModel; private static String componentModel = ContextConstants.ComponentModelConfig.defaultComponentModel;
@ -84,6 +86,14 @@ public class AutoMapperProperties {
/* ******************** getter/setter ******************** */ /* ******************** getter/setter ******************** */
public static List<TypeMirror> getUses() {
return uses;
}
public static void setUses(List<TypeMirror> uses) {
AutoMapperProperties.uses = uses;
}
public static String getMapperPackage() { public static String getMapperPackage() {
return mapperPackage; return mapperPackage;
} }

View File

@ -48,6 +48,11 @@ public class MapperConfigGenerator {
usesCodeBuilder.add(", $T.class", use); usesCodeBuilder.add(", $T.class", use);
}); });
} }
if (CollectionUtils.isNotEmpty(AutoMapperProperties.getUses())) {
AutoMapperProperties.getUses().forEach(use -> {
usesCodeBuilder.add(", $T.class", use);
});
}
CodeBlock usesCodeBlock = usesCodeBuilder.add("}").build(); CodeBlock usesCodeBlock = usesCodeBuilder.add("}").build();
final AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get("org.mapstruct", "MapperConfig")) final AnnotationSpec.Builder builder = AnnotationSpec.builder(ClassName.get("org.mapstruct", "MapperConfig"))
.addMember("componentModel", .addMember("componentModel",

View File

@ -31,6 +31,11 @@ import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface MapperConfig { public @interface MapperConfig {
/**
* 设置公共的转换器或其它 Mapper 在这里主要用途是设置全局共享的自定义转换类不用再单个注解上重复设置 {@link org.mapstruct.MapperConfig#uses()}
*/
Class<?>[] uses() default {};
/** /**
* 所生成的 Mapper 接口的包 * 所生成的 Mapper 接口的包
* @return Mapper 接口自动生成后的包名如果为空则默认生成在要转换的类同包下 * @return Mapper 接口自动生成后的包名如果为空则默认生成在要转换的类同包下