From 2a385c0ec899798719ad97a808db2db0019d7ff1 Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Wed, 12 Aug 2015 23:58:01 +0200 Subject: [PATCH] #600 Improve generated assignment code when using default-values, enhance javadoc a little and remove unused attribute in PropertyMapping --- .../src/main/java/org/mapstruct/Mapping.java | 19 +++++++++--------- core/src/main/java/org/mapstruct/Mapping.java | 20 +++++++++---------- .../ap/internal/model/PropertyMapping.java | 7 +------ .../model/assignment/SetterWrapper.ftl | 17 ++++++++-------- 4 files changed, 29 insertions(+), 34 deletions(-) diff --git a/core-jdk8/src/main/java/org/mapstruct/Mapping.java b/core-jdk8/src/main/java/org/mapstruct/Mapping.java index 15cb8d0a4..7c264f253 100644 --- a/core-jdk8/src/main/java/org/mapstruct/Mapping.java +++ b/core-jdk8/src/main/java/org/mapstruct/Mapping.java @@ -66,7 +66,7 @@ public @interface Mapping { *
  • When no matching property is found, MapStruct looks for a matching parameter name instead.
  • *
  • When used to map an enum constant, the name of the constant member is to be given.
  • * - * Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping. + * This attribute can not be used together with {@link #constant()} or {@link #expression()}. * * @return The source name of the configured property or enum constant. */ @@ -85,7 +85,7 @@ public @interface Mapping { * property is not of type {@code String}, the value will be converted by applying a matching conversion method or * built-in conversion. *

    - * Either this attribute or {@link #source()} or {@link #expression()} may be specified for a given mapping. + * This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #expression()}. * * @return A constant {@code String} constant specifying the value for the designated target property */ @@ -112,7 +112,7 @@ public @interface Mapping { * Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be * imported via {@link Mapper#imports()}. *

    - * Either this attribute, {@link #source()} or {@link #constant()} may be specified for a given mapping. + * This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #constant()}. * * @return An expression specifying the value for the designated target property */ @@ -156,14 +156,13 @@ public @interface Mapping { String[] dependsOn() default { }; /** + * In case the source property is {@code null}, the provided default {@link String} value is set. If the designated + * target property is not of type {@code String}, the value will be converted by applying a matching conversion + * method or built-in conversion. *

    - * In case the source property is null the provided default {@link String} value is set. - * If the designated target - * property is not of type {@code String}, the value will be converted by applying a matching conversion method or - * built-in conversion. - *

    - * Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping. - * @return Default value to set in case the source property is null. + * This attribute can not be used together with {@link #constant()} or {@link #expression()}. + * + * @return Default value to set in case the source property is {@code null}. */ String defaultValue() default ""; } diff --git a/core/src/main/java/org/mapstruct/Mapping.java b/core/src/main/java/org/mapstruct/Mapping.java index 644ba5720..5221a6775 100644 --- a/core/src/main/java/org/mapstruct/Mapping.java +++ b/core/src/main/java/org/mapstruct/Mapping.java @@ -64,7 +64,7 @@ public @interface Mapping { *
  • When no matching property is found, MapStruct looks for a matching parameter name instead.
  • *
  • When used to map an enum constant, the name of the constant member is to be given.
  • * - * Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping. + * This attribute can not be used together with {@link #constant()} or {@link #expression()}. * * @return The source name of the configured property or enum constant. */ @@ -83,7 +83,7 @@ public @interface Mapping { * property is not of type {@code String}, the value will be converted by applying a matching conversion method or * built-in conversion. *

    - * Either this attribute or {@link #source()} or {@link #expression()} may be specified for a given mapping. + * This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #expression()}. * * @return A constant {@code String} constant specifying the value for the designated target property */ @@ -94,6 +94,7 @@ public @interface Mapping { *

    * Currently, Java is the only supported "expression language" and expressions must be given in form of Java * expressions using the following format: {@code java()}. For instance the mapping + *

    * *

          * {@code @Mapping(
    @@ -109,7 +110,7 @@ public @interface Mapping {
          * Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be
          * imported via {@link Mapper#imports()}.
          * 

    - * Either this attribute, {@link #source()} or {@link #constant()} may be specified for a given mapping. + * This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #constant()}. * * @return An expression specifying the value for the designated target property */ @@ -153,14 +154,13 @@ public @interface Mapping { String[] dependsOn() default { }; /** + * In case the source property is {@code null}, the provided default {@link String} value is set. If the designated + * target property is not of type {@code String}, the value will be converted by applying a matching conversion + * method or built-in conversion. *

    - * In case the source property is null the provided default {@link String} value is set. - * If the designated target - * property is not of type {@code String}, the value will be converted by applying a matching conversion method or - * built-in conversion. - *

    - * Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping. - * @return Default value to set in case the source property is null. + * This attribute can not be used together with {@link #constant()} or {@link #expression()}. + * + * @return Default value to set in case the source property is {@code null}. */ String defaultValue() default ""; } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index c15760d73..d5ae07544 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -68,8 +68,7 @@ public class PropertyMapping extends ModelElement { private final Type targetType; private final Assignment assignment; private final List dependsOn; - private String defaultValue; - private Assignment defaultValueAssignment; + private final Assignment defaultValueAssignment; @SuppressWarnings("unchecked") private static class MappingBuilderBase> { @@ -798,10 +797,6 @@ public class PropertyMapping extends ModelElement { return dependsOn; } - public String getDefaultValue() { - return defaultValue; - } - @Override public String toString() { return "PropertyMapping {" diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl index d39e1f573..0b8b1cf6b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl @@ -30,7 +30,6 @@ } - <#macro _assignment> <@includeModel object=assignment targetBeanName=ext.targetBeanName @@ -40,7 +39,6 @@ targetType=ext.targetType defaultValueAssignment=ext.defaultValueAssignment/> - <#macro _defaultValueAssignment> <@includeModel object=ext.defaultValueAssignment.assignment targetBeanName=ext.targetBeanName @@ -48,13 +46,16 @@ targetWriteAccessorName=ext.targetWriteAccessorName targetType=ext.targetType/> - <#macro assignment_w_defaultValue> - ${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> ); - <#-- if the assignee property is a primitive, defaulValueAssignment will not be set --> <#if ext.defaultValueAssignment?? > - if ( ${sourceReference} == null ) { - ${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_defaultValueAssignment/> ); - } + <#-- if the assignee property is a primitive, defaulValueAssignment will not be set --> + if ( ${sourceReference} != null ) { + ${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> ); + } + else { + ${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_defaultValueAssignment/> ); + } + <#else> + ${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> ); \ No newline at end of file