#600 Improve generated assignment code when using default-values, enhance javadoc a little and remove unused attribute in PropertyMapping

This commit is contained in:
Andreas Gudian 2015-08-12 23:58:01 +02:00
parent 964f676af6
commit 2a385c0ec8
4 changed files with 29 additions and 34 deletions

View File

@ -66,7 +66,7 @@ public @interface Mapping {
* <li>When no matching property is found, MapStruct looks for a matching parameter name instead.</li> * <li>When no matching property is found, MapStruct looks for a matching parameter name instead.</li>
* <li>When used to map an enum constant, the name of the constant member is to be given.</li> * <li>When used to map an enum constant, the name of the constant member is to be given.</li>
* </ol> * </ol>
* 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. * @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 * property is not of type {@code String}, the value will be converted by applying a matching conversion method or
* built-in conversion. * built-in conversion.
* <p> * <p>
* 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 * @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 * Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be
* imported via {@link Mapper#imports()}. * imported via {@link Mapper#imports()}.
* <p> * <p>
* 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 * @return An expression specifying the value for the designated target property
*/ */
@ -156,14 +156,13 @@ public @interface Mapping {
String[] dependsOn() default { }; 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.
* <p> * <p>
* In case the source property is null the provided default {@link String} value is set. * This attribute can not be used together with {@link #constant()} or {@link #expression()}.
* If the designated target *
* property is not of type {@code String}, the value will be converted by applying a matching conversion method or * @return Default value to set in case the source property is {@code null}.
* built-in conversion.
* </p>
* 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.
*/ */
String defaultValue() default ""; String defaultValue() default "";
} }

View File

@ -64,7 +64,7 @@ public @interface Mapping {
* <li>When no matching property is found, MapStruct looks for a matching parameter name instead.</li> * <li>When no matching property is found, MapStruct looks for a matching parameter name instead.</li>
* <li>When used to map an enum constant, the name of the constant member is to be given.</li> * <li>When used to map an enum constant, the name of the constant member is to be given.</li>
* </ol> * </ol>
* 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. * @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 * property is not of type {@code String}, the value will be converted by applying a matching conversion method or
* built-in conversion. * built-in conversion.
* <p> * <p>
* 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 * @return A constant {@code String} constant specifying the value for the designated target property
*/ */
@ -94,6 +94,7 @@ public @interface Mapping {
* <p> * <p>
* Currently, Java is the only supported "expression language" and expressions must be given in form of Java * Currently, Java is the only supported "expression language" and expressions must be given in form of Java
* expressions using the following format: {@code java(<EXPRESSION>)}. For instance the mapping * expressions using the following format: {@code java(<EXPRESSION>)}. For instance the mapping
* <p>
* *
* <pre> * <pre>
* {@code @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 * Any types referenced in expressions must be given via their fully-qualified name. Alternatively, types can be
* imported via {@link Mapper#imports()}. * imported via {@link Mapper#imports()}.
* <p> * <p>
* 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 * @return An expression specifying the value for the designated target property
*/ */
@ -153,14 +154,13 @@ public @interface Mapping {
String[] dependsOn() default { }; 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.
* <p> * <p>
* In case the source property is null the provided default {@link String} value is set. * This attribute can not be used together with {@link #constant()} or {@link #expression()}.
* If the designated target *
* property is not of type {@code String}, the value will be converted by applying a matching conversion method or * @return Default value to set in case the source property is {@code null}.
* built-in conversion.
* </p>
* 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.
*/ */
String defaultValue() default ""; String defaultValue() default "";
} }

View File

@ -68,8 +68,7 @@ public class PropertyMapping extends ModelElement {
private final Type targetType; private final Type targetType;
private final Assignment assignment; private final Assignment assignment;
private final List<String> dependsOn; private final List<String> dependsOn;
private String defaultValue; private final Assignment defaultValueAssignment;
private Assignment defaultValueAssignment;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static class MappingBuilderBase<T extends MappingBuilderBase<T>> { private static class MappingBuilderBase<T extends MappingBuilderBase<T>> {
@ -798,10 +797,6 @@ public class PropertyMapping extends ModelElement {
return dependsOn; return dependsOn;
} }
public String getDefaultValue() {
return defaultValue;
}
@Override @Override
public String toString() { public String toString() {
return "PropertyMapping {" return "PropertyMapping {"

View File

@ -30,7 +30,6 @@
} }
</#list> </#list>
</#if> </#if>
<#macro _assignment> <#macro _assignment>
<@includeModel object=assignment <@includeModel object=assignment
targetBeanName=ext.targetBeanName targetBeanName=ext.targetBeanName
@ -40,7 +39,6 @@
targetType=ext.targetType targetType=ext.targetType
defaultValueAssignment=ext.defaultValueAssignment/> defaultValueAssignment=ext.defaultValueAssignment/>
</#macro> </#macro>
<#macro _defaultValueAssignment> <#macro _defaultValueAssignment>
<@includeModel object=ext.defaultValueAssignment.assignment <@includeModel object=ext.defaultValueAssignment.assignment
targetBeanName=ext.targetBeanName targetBeanName=ext.targetBeanName
@ -48,13 +46,16 @@
targetWriteAccessorName=ext.targetWriteAccessorName targetWriteAccessorName=ext.targetWriteAccessorName
targetType=ext.targetType/> targetType=ext.targetType/>
</#macro> </#macro>
<#macro assignment_w_defaultValue> <#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 ext.defaultValueAssignment?? >
if ( ${sourceReference} == null ) { <#-- 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/> ); ${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_defaultValueAssignment/> );
} }
<#else>
${ext.targetBeanName}.${ext.targetWriteAccessorName}( <@_assignment/> );
</#if> </#if>
</#macro> </#macro>