Skip to content

Commit 2a385c0

Browse files
committed
mapstruct#600 Improve generated assignment code when using default-values, enhance javadoc a little and remove unused attribute in PropertyMapping
1 parent 964f676 commit 2a385c0

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

core-jdk8/src/main/java/org/mapstruct/Mapping.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
* <li>When no matching property is found, MapStruct looks for a matching parameter name instead.</li>
6767
* <li>When used to map an enum constant, the name of the constant member is to be given.</li>
6868
* </ol>
69-
* Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping.
69+
* This attribute can not be used together with {@link #constant()} or {@link #expression()}.
7070
*
7171
* @return The source name of the configured property or enum constant.
7272
*/
@@ -85,7 +85,7 @@
8585
* property is not of type {@code String}, the value will be converted by applying a matching conversion method or
8686
* built-in conversion.
8787
* <p>
88-
* Either this attribute or {@link #source()} or {@link #expression()} may be specified for a given mapping.
88+
* This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #expression()}.
8989
*
9090
* @return A constant {@code String} constant specifying the value for the designated target property
9191
*/
@@ -112,7 +112,7 @@
112112
* Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be
113113
* imported via {@link Mapper#imports()}.
114114
* <p>
115-
* Either this attribute, {@link #source()} or {@link #constant()} may be specified for a given mapping.
115+
* This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #constant()}.
116116
*
117117
* @return An expression specifying the value for the designated target property
118118
*/
@@ -156,14 +156,13 @@
156156
String[] dependsOn() default { };
157157

158158
/**
159+
* In case the source property is {@code null}, the provided default {@link String} value is set. If the designated
160+
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
161+
* method or built-in conversion.
159162
* <p>
160-
* In case the source property is null the provided default {@link String} value is set.
161-
* If the designated target
162-
* property is not of type {@code String}, the value will be converted by applying a matching conversion method or
163-
* built-in conversion.
164-
* </p>
165-
* Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping.
166-
* @return Default value to set in case the source property is null.
163+
* This attribute can not be used together with {@link #constant()} or {@link #expression()}.
164+
*
165+
* @return Default value to set in case the source property is {@code null}.
167166
*/
168167
String defaultValue() default "";
169168
}

core/src/main/java/org/mapstruct/Mapping.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* <li>When no matching property is found, MapStruct looks for a matching parameter name instead.</li>
6565
* <li>When used to map an enum constant, the name of the constant member is to be given.</li>
6666
* </ol>
67-
* Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping.
67+
* This attribute can not be used together with {@link #constant()} or {@link #expression()}.
6868
*
6969
* @return The source name of the configured property or enum constant.
7070
*/
@@ -83,7 +83,7 @@
8383
* property is not of type {@code String}, the value will be converted by applying a matching conversion method or
8484
* built-in conversion.
8585
* <p>
86-
* Either this attribute or {@link #source()} or {@link #expression()} may be specified for a given mapping.
86+
* This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #expression()}.
8787
*
8888
* @return A constant {@code String} constant specifying the value for the designated target property
8989
*/
@@ -94,6 +94,7 @@
9494
* <p>
9595
* Currently, Java is the only supported "expression language" and expressions must be given in form of Java
9696
* expressions using the following format: {@code java(<EXPRESSION>)}. For instance the mapping
97+
* <p>
9798
*
9899
* <pre>
99100
* {@code @Mapping(
@@ -109,7 +110,7 @@
109110
* Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be
110111
* imported via {@link Mapper#imports()}.
111112
* <p>
112-
* Either this attribute, {@link #source()} or {@link #constant()} may be specified for a given mapping.
113+
* This attribute can not be used together with {@link #source()}, {@link #defaultValue()} or {@link #constant()}.
113114
*
114115
* @return An expression specifying the value for the designated target property
115116
*/
@@ -153,14 +154,13 @@
153154
String[] dependsOn() default { };
154155

155156
/**
157+
* In case the source property is {@code null}, the provided default {@link String} value is set. If the designated
158+
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
159+
* method or built-in conversion.
156160
* <p>
157-
* In case the source property is null the provided default {@link String} value is set.
158-
* If the designated target
159-
* property is not of type {@code String}, the value will be converted by applying a matching conversion method or
160-
* built-in conversion.
161-
* </p>
162-
* Either this attribute or {@link #constant()} or {@link #expression()} may be specified for a given mapping.
163-
* @return Default value to set in case the source property is null.
161+
* This attribute can not be used together with {@link #constant()} or {@link #expression()}.
162+
*
163+
* @return Default value to set in case the source property is {@code null}.
164164
*/
165165
String defaultValue() default "";
166166
}

processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public class PropertyMapping extends ModelElement {
6868
private final Type targetType;
6969
private final Assignment assignment;
7070
private final List<String> dependsOn;
71-
private String defaultValue;
72-
private Assignment defaultValueAssignment;
71+
private final Assignment defaultValueAssignment;
7372

7473
@SuppressWarnings("unchecked")
7574
private static class MappingBuilderBase<T extends MappingBuilderBase<T>> {
@@ -798,10 +797,6 @@ public List<String> getDependsOn() {
798797
return dependsOn;
799798
}
800799

801-
public String getDefaultValue() {
802-
return defaultValue;
803-
}
804-
805800
@Override
806801
public String toString() {
807802
return "PropertyMapping {"

processor/src/main/resources/org/mapstruct/ap/internal/model/assignment/SetterWrapper.ftl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
}
3131
</#list>
3232
</#if>
33-
3433
<#macro _assignment>
3534
<@includeModel object=assignment
3635
targetBeanName=ext.targetBeanName
@@ -40,21 +39,23 @@
4039
targetType=ext.targetType
4140
defaultValueAssignment=ext.defaultValueAssignment/>
4241
</#macro>
43-
4442
<#macro _defaultValueAssignment>
4543
<@includeModel object=ext.defaultValueAssignment.assignment
4644
targetBeanName=ext.targetBeanName
4745
existingInstanceMapping=ext.existingInstanceMapping
4846
targetWriteAccessorName=ext.targetWriteAccessorName
4947
targetType=ext.targetType/>
5048
</#macro>
51-
5249
<#macro assignment_w_defaultValue>
53-
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> );
54-
<#-- if the assignee property is a primitive, defaulValueAssignment will not be set -->
5550
<#if ext.defaultValueAssignment?? >
56-
if ( ${sourceReference} == null ) {
57-
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_defaultValueAssignment/> );
58-
}
51+
<#-- if the assignee property is a primitive, defaulValueAssignment will not be set -->
52+
if ( ${sourceReference} != null ) {
53+
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> );
54+
}
55+
else {
56+
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_defaultValueAssignment/> );
57+
}
58+
<#else>
59+
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> );
5960
</#if>
6061
</#macro>

0 commit comments

Comments
 (0)