From 21069e5a2ece79d3f4a7f7d247b2f96060ccedbb Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Mon, 29 Aug 2022 21:15:01 +0200 Subject: [PATCH] #2895 Generate more readable annotations --- .../ap/internal/model/Annotation.ftl | 15 +++++- .../test/annotatewith/AnnotateWithTest.java | 9 +++- .../test/annotatewith/CustomNamedMapper.java | 38 +++++++++++++++ .../annotatewith/CustomNamedMapperImpl.java | 46 ++++++++++++++++++- .../MultipleArrayValuesMapperImpl.java | 14 +++++- 5 files changed, 117 insertions(+), 5 deletions(-) diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl index eb67d732f..81f3d2ba8 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/Annotation.ftl @@ -6,4 +6,17 @@ --> <#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.Annotation" --> -@<@includeModel object=type/><#if (properties?size > 0) >(<#list properties as property><@includeModel object=property/><#if property_has_next>, ) \ No newline at end of file +<#switch properties?size> + <#case 0> + @<@includeModel object=type/><#rt> + <#break> + <#case 1> + @<@includeModel object=type/>(<@includeModel object=properties[0]/>)<#rt> + <#break> + <#default> + @<@includeModel object=type/>( + <#list properties as property> + <#nt><@includeModel object=property/><#if property_has_next>, + + )<#rt> + \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java b/processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java index 31b8d8fad..e3b20f547 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/annotatewith/AnnotateWithTest.java @@ -42,8 +42,13 @@ public class AnnotateWithTest { } @ProcessorTest - @WithClasses( { CustomNamedMapper.class, CustomAnnotationWithParamsContainer.class, - CustomAnnotationWithParams.class } ) + @WithClasses( { + CustomNamedMapper.class, + CustomAnnotationWithParamsContainer.class, + CustomAnnotationWithParams.class, + CustomClassOnlyAnnotation.class, + CustomMethodOnlyAnnotation.class, + } ) public void annotationWithValue() { generatedSource.addComparisonToFixtureFor( CustomNamedMapper.class ); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/annotatewith/CustomNamedMapper.java b/processor/src/test/java/org/mapstruct/ap/test/annotatewith/CustomNamedMapper.java index 32241ab4a..9f48d8c76 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/annotatewith/CustomNamedMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/annotatewith/CustomNamedMapper.java @@ -13,6 +13,7 @@ import org.mapstruct.Mapper; * @author Ben Zegveld */ @Mapper +@AnnotateWith( CustomClassOnlyAnnotation.class ) @AnnotateWith( value = CustomAnnotationWithParams.class, elements = { @Element( name = "stringArray", strings = "test" ), @Element( name = "stringParam", strings = "test" ), @@ -35,6 +36,43 @@ import org.mapstruct.Mapper; @Element( name = "shortArray", shorts = 3 ), @Element( name = "shortParam", shorts = 1 ) } ) +@AnnotateWith( value = CustomAnnotationWithParams.class, elements = { + @Element(name = "stringParam", strings = "single value") +}) public interface CustomNamedMapper { + @AnnotateWith(value = CustomAnnotationWithParams.class, elements = { + @Element(name = "stringParam", strings = "double method value"), + @Element(name = "stringArray", strings = { "first", "second" }), + }) + @AnnotateWith(value = CustomAnnotationWithParams.class, elements = { + @Element(name = "stringParam", strings = "single method value") + }) + @AnnotateWith( CustomMethodOnlyAnnotation.class ) + Target map(Source source); + + class Target { + private final String value; + + public Target(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + + class Source { + private final String value; + + public Source(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + } + } diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/CustomNamedMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/CustomNamedMapperImpl.java index cb01cfec8..a485d6676 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/CustomNamedMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/CustomNamedMapperImpl.java @@ -12,6 +12,50 @@ import javax.annotation.processing.Generated; date = "2022-06-06T16:48:18+0200", comments = "version: , compiler: javac, environment: Java 11.0.12 (Azul Systems, Inc.)" ) -@CustomAnnotationWithParams(stringArray = "test", stringParam = "test", booleanArray = true, booleanParam = true, byteArray = 16, byteParam = 19, charArray = 'd', charParam = 'a', enumArray = AnnotateWithEnum.EXISTING, enumParam = AnnotateWithEnum.EXISTING, doubleArray = 0.3, doubleParam = 1.2, floatArray = 0.300000011920929f, floatParam = 1.2000000476837158f, intArray = 3, intParam = 1, longArray = 3L, longParam = 1L, shortArray = 3, shortParam = 1) +@CustomClassOnlyAnnotation +@CustomAnnotationWithParams( + stringArray = "test", + stringParam = "test", + booleanArray = true, + booleanParam = true, + byteArray = 16, + byteParam = 19, + charArray = 'd', + charParam = 'a', + enumArray = AnnotateWithEnum.EXISTING, + enumParam = AnnotateWithEnum.EXISTING, + doubleArray = 0.3, + doubleParam = 1.2, + floatArray = 0.300000011920929f, + floatParam = 1.2000000476837158f, + intArray = 3, + intParam = 1, + longArray = 3L, + longParam = 1L, + shortArray = 3, + shortParam = 1 +) +@CustomAnnotationWithParams(stringParam = "single value") public class CustomNamedMapperImpl implements CustomNamedMapper { + + @CustomAnnotationWithParams( + stringParam = "double method value", + stringArray = { "first", "second" } + ) + @CustomAnnotationWithParams(stringParam = "single method value") + @CustomMethodOnlyAnnotation + @Override + public Target map(Source source) { + if ( source == null ) { + return null; + } + + String value = null; + + value = source.getValue(); + + Target target = new Target( value ); + + return target; + } } diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/MultipleArrayValuesMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/MultipleArrayValuesMapperImpl.java index fe9bb93f1..ea15207c6 100644 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/MultipleArrayValuesMapperImpl.java +++ b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/annotatewith/MultipleArrayValuesMapperImpl.java @@ -13,6 +13,18 @@ import org.mapstruct.Mapper; date = "2022-06-06T16:48:23+0200", comments = "version: , compiler: javac, environment: Java 11.0.12 (Azul Systems, Inc.)" ) -@CustomAnnotationWithParams(stringArray = { "test1", "test2" }, booleanArray = { false, true }, byteArray = { 8, 31 }, charArray = { 'b', 'c' }, doubleArray = { 1.2, 3.4 }, floatArray = { 1.2000000476837158f, 3.4000000953674316f }, intArray = { 12, 34 }, longArray = { 12L, 34L }, shortArray = { 12, 34 }, classArray = { Mapper.class, CustomAnnotationWithParams.class }, stringParam = "required parameter") +@CustomAnnotationWithParams( + stringArray = { "test1", "test2" }, + booleanArray = { false, true }, + byteArray = { 8, 31 }, + charArray = { 'b', 'c' }, + doubleArray = { 1.2, 3.4 }, + floatArray = { 1.2000000476837158f, 3.4000000953674316f }, + intArray = { 12, 34 }, + longArray = { 12L, 34L }, + shortArray = { 12, 34 }, + classArray = { Mapper.class, CustomAnnotationWithParams.class }, + stringParam = "required parameter" +) public class MultipleArrayValuesMapperImpl implements MultipleArrayValuesMapper { }