mirror of
https://gitee.com/easii/mapstruct-plus.git
synced 2025-12-07 01:28:31 +08:00
feat(processor): 支持在注解上使用 @AutoMapping- 新增处理注解上存在 @AutoMapping 的逻辑
- 修改 @AutoMapping 注解,增加对 ElementType.ANNOTATION_TYPE 的支持
可以实现如下效果:
//新增dto使用注解,自动将当前用户映射到创建人字段
@AutoMapping(source = "operatorUserId", target = "createBy")
public @interface CreateMapper {
}
@AutoMapping(source = "operatorUserId", target = "createBy")
// 更新dto使用注解,自动将当前用户映射到updateBy字段
@AutoMapping(source = "operatorUserId", target = "updateBy")
public @interface UpdateMapper{
}
这样就可以将基类中的operatorUserId根据dto的不同,映射到对应的字段
This commit is contained in:
parent
66f129ea66
commit
fff9461d35
@ -57,10 +57,8 @@ import javax.annotation.processing.RoundEnvironment;
|
|||||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||||
import javax.annotation.processing.SupportedOptions;
|
import javax.annotation.processing.SupportedOptions;
|
||||||
import javax.lang.model.SourceVersion;
|
import javax.lang.model.SourceVersion;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.*;
|
||||||
import javax.lang.model.element.ElementKind;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
|
||||||
import javax.lang.model.element.TypeElement;
|
|
||||||
import javax.lang.model.type.MirroredTypeException;
|
import javax.lang.model.type.MirroredTypeException;
|
||||||
import javax.lang.model.type.MirroredTypesException;
|
import javax.lang.model.type.MirroredTypesException;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
@ -990,6 +988,8 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
list.addAll(buildMirrorAnno(autoMapperEle));
|
||||||
|
|
||||||
for (Element ele : autoMapperEle.getEnclosedElements()) {
|
for (Element ele : autoMapperEle.getEnclosedElements()) {
|
||||||
if (ele.getKind() != ElementKind.FIELD && ele.getKind() != ElementKind.METHOD) {
|
if (ele.getKind() != ElementKind.FIELD && ele.getKind() != ElementKind.METHOD) {
|
||||||
continue;
|
continue;
|
||||||
@ -1010,6 +1010,32 @@ public class AutoMapperProcessor extends AbstractProcessor {
|
|||||||
list.removeIf(Objects::isNull);
|
list.removeIf(Objects::isNull);
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 处理注解上存在@AutoMapping的情况
|
||||||
|
*
|
||||||
|
* @param element element
|
||||||
|
*/
|
||||||
|
private List<AutoMappingMetadata> buildMirrorAnno(final TypeElement element) {
|
||||||
|
List<AutoMappingMetadata> list = new ArrayList<>();
|
||||||
|
List<? extends AnnotationMirror> allAnnotationMirrors = processingEnv.getElementUtils().getAllAnnotationMirrors(element);
|
||||||
|
for (AnnotationMirror annotationMirror : allAnnotationMirrors) {
|
||||||
|
DeclaredType annotationType = annotationMirror.getAnnotationType();
|
||||||
|
TypeElement annotationElement = (TypeElement) annotationType.asElement();
|
||||||
|
if (annotationElement == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
AutoMappingGem autoMappingGem = AutoMappingGem.instanceOn(annotationElement);
|
||||||
|
if (autoMappingGem != null && autoMappingGem.isValid()) {
|
||||||
|
list.add(buildAutoMappingMetadata(autoMappingGem, element));
|
||||||
|
}
|
||||||
|
AutoMappingsGem autoMappingsGem = AutoMappingsGem.instanceOn(element);
|
||||||
|
if (autoMappingsGem != null && autoMappingsGem.isValid()) {
|
||||||
|
autoMappingsGem.value().get().forEach(a -> list.add(buildAutoMappingMetadata(a, element)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private AutoMappingMetadata buildAutoMappingMetadata(AutoMappingGem autoMappingGem, Element ele) {
|
private AutoMappingMetadata buildAutoMappingMetadata(AutoMappingGem autoMappingGem, Element ele) {
|
||||||
ClassName targetClass = transToClassName(autoMappingGem.targetClass().get());
|
ClassName targetClass = transToClassName(autoMappingGem.targetClass().get());
|
||||||
|
|||||||
@ -18,7 +18,7 @@ import org.mapstruct.control.MappingControl;
|
|||||||
|
|
||||||
import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
|
import static org.mapstruct.NullValueCheckStrategy.ON_IMPLICIT_CONVERSION;
|
||||||
|
|
||||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
@Target({ElementType.FIELD, ElementType.METHOD,ElementType.ANNOTATION_TYPE})
|
||||||
@Retention(RetentionPolicy.CLASS)
|
@Retention(RetentionPolicy.CLASS)
|
||||||
public @interface AutoMapping {
|
public @interface AutoMapping {
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user