This commit is contained in:
Gunnar Morling 2014-11-24 22:33:17 +01:00
parent 4ca2b6ecf1
commit 2fde97c039
13 changed files with 164 additions and 171 deletions

View File

@ -31,8 +31,8 @@ import java.lang.annotation.Target;
* *
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Target( { ElementType.METHOD } ) @Target({ ElementType.METHOD })
@Retention( RetentionPolicy.SOURCE ) @Retention(RetentionPolicy.SOURCE)
public @interface NullValueMapping { public @interface NullValueMapping {
/** /**

View File

@ -27,7 +27,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
@ -40,8 +39,8 @@ import org.mapstruct.ap.model.source.Mapping;
import org.mapstruct.ap.model.source.SourceMethod; import org.mapstruct.ap.model.source.SourceMethod;
import org.mapstruct.ap.model.source.SourceReference; import org.mapstruct.ap.model.source.SourceReference;
import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.option.ReportingPolicy;
import org.mapstruct.ap.prism.NullValueMappingPrism;
import org.mapstruct.ap.prism.CollectionMappingStrategyPrism; import org.mapstruct.ap.prism.CollectionMappingStrategyPrism;
import org.mapstruct.ap.prism.NullValueMappingPrism;
import org.mapstruct.ap.util.Executables; import org.mapstruct.ap.util.Executables;
import org.mapstruct.ap.util.MapperConfig; import org.mapstruct.ap.util.MapperConfig;
import org.mapstruct.ap.util.Strings; import org.mapstruct.ap.util.Strings;
@ -102,7 +101,7 @@ public class BeanMappingMethod extends MappingMethod {
// mapNullToDefault // mapNullToDefault
NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() ); NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() );
boolean mapNullToDefault = boolean mapNullToDefault =
MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism ); MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism );
MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx ); MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
return new BeanMappingMethod( method, propertyMappings, factoryMethod, mapNullToDefault ); return new BeanMappingMethod( method, propertyMappings, factoryMethod, mapNullToDefault );
@ -182,13 +181,14 @@ public class BeanMappingMethod extends MappingMethod {
ExecutableElement targetProperty = unprocessedTargetProperties.get( mapping.getTargetName() ); ExecutableElement targetProperty = unprocessedTargetProperties.get( mapping.getTargetName() );
if ( targetProperty == null ) { if ( targetProperty == null ) {
ctx.getMessager().printMessage( ctx.getMessager().printMessage(
Diagnostic.Kind.ERROR, Diagnostic.Kind.ERROR,
String.format( "Unknown property \"%s\" in return type.", String.format(
mapping.getTargetName() "Unknown property \"%s\" in return type.",
), mapping.getTargetName()
method.getExecutable(), ),
mapping.getMirror(), method.getExecutable(),
mapping.getSourceAnnotationValue() mapping.getMirror(),
mapping.getSourceAnnotationValue()
); );
errorOccurred = true; errorOccurred = true;
} }
@ -212,14 +212,14 @@ public class BeanMappingMethod extends MappingMethod {
// targetProperty == null can occur: we arrived here because we want as many errors // targetProperty == null can occur: we arrived here because we want as many errors
// as possible before we stop analysing // as possible before we stop analysing
propertyMapping = new PropertyMappingBuilder() propertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .souceMethod( method )
.targetAccessor( targetProperty ) .targetAccessor( targetProperty )
.targetPropertyName( mapping.getTargetName() ) .targetPropertyName( mapping.getTargetName() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.qualifiers( mapping.getQualifiers() ) .qualifiers( mapping.getQualifiers() )
.dateFormat( mapping.getDateFormat() ) .dateFormat( mapping.getDateFormat() )
.build(); .build();
handledTargets.add( mapping.getTargetName() ); handledTargets.add( mapping.getTargetName() );
unprocessedSourceParameters.remove( sourceRef.getParameter() ); unprocessedSourceParameters.remove( sourceRef.getParameter() );
} }
@ -233,13 +233,13 @@ public class BeanMappingMethod extends MappingMethod {
else if ( mapping.getConstant() != null && targetProperty != null ) { else if ( mapping.getConstant() != null && targetProperty != null ) {
propertyMapping = new ConstantMappingBuilder() propertyMapping = new ConstantMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.sourceMethod( method ) .sourceMethod( method )
.constantExpression( "\"" + mapping.getConstant() + "\"" ) .constantExpression( "\"" + mapping.getConstant() + "\"" )
.targetAccessor( targetProperty ) .targetAccessor( targetProperty )
.dateFormat( mapping.getDateFormat() ) .dateFormat( mapping.getDateFormat() )
.qualifiers( mapping.getQualifiers() ) .qualifiers( mapping.getQualifiers() )
.build(); .build();
handledTargets.add( mapping.getTargetName() ); handledTargets.add( mapping.getTargetName() );
} }
@ -247,11 +247,11 @@ public class BeanMappingMethod extends MappingMethod {
else if ( mapping.getJavaExpression() != null && targetProperty != null ) { else if ( mapping.getJavaExpression() != null && targetProperty != null ) {
propertyMapping = new JavaExpressionMappingBuilder() propertyMapping = new JavaExpressionMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .souceMethod( method )
.javaExpression( mapping.getJavaExpression() ) .javaExpression( mapping.getJavaExpression() )
.targetAccessor( targetProperty ) .targetAccessor( targetProperty )
.build(); .build();
handledTargets.add( mapping.getTargetName() ); handledTargets.add( mapping.getTargetName() );
} }
@ -280,7 +280,7 @@ public class BeanMappingMethod extends MappingMethod {
*/ */
private void applyPropertyNameBasedMapping() { private void applyPropertyNameBasedMapping() {
Iterator<Entry<String, ExecutableElement>> targetProperties = Iterator<Entry<String, ExecutableElement>> targetProperties =
unprocessedTargetProperties.entrySet().iterator(); unprocessedTargetProperties.entrySet().iterator();
// usually there should be only one getter; only for Boolean there may be two: isFoo() and getFoo() // usually there should be only one getter; only for Boolean there may be two: isFoo() and getFoo()
List<ExecutableElement> candidates = new ArrayList<ExecutableElement>( 2 ); List<ExecutableElement> candidates = new ArrayList<ExecutableElement>( 2 );
@ -312,21 +312,21 @@ public class BeanMappingMethod extends MappingMethod {
Mapping mapping = method.getSingleMappingByTargetPropertyName( targetProperty.getKey() ); Mapping mapping = method.getSingleMappingByTargetPropertyName( targetProperty.getKey() );
SourceReference sourceRef = new SourceReference.BuilderFromProperty() SourceReference sourceRef = new SourceReference.BuilderFromProperty()
.sourceParameter( sourceParameter ) .sourceParameter( sourceParameter )
.type( ctx.getTypeFactory().getReturnType( sourceAccessor ) ) .type( ctx.getTypeFactory().getReturnType( sourceAccessor ) )
.accessor( sourceAccessor ) .accessor( sourceAccessor )
.name( targetProperty.getKey() ) .name( targetProperty.getKey() )
.build(); .build();
newPropertyMapping = new PropertyMappingBuilder() newPropertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .souceMethod( method )
.targetAccessor( targetProperty.getValue() ) .targetAccessor( targetProperty.getValue() )
.targetPropertyName( targetProperty.getKey() ) .targetPropertyName( targetProperty.getKey() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.qualifiers( mapping != null ? mapping.getQualifiers() : null ) .qualifiers( mapping != null ? mapping.getQualifiers() : null )
.dateFormat( mapping != null ? mapping.getDateFormat() : null ) .dateFormat( mapping != null ? mapping.getDateFormat() : null )
.build(); .build();
unprocessedSourceParameters.remove( sourceParameter ); unprocessedSourceParameters.remove( sourceParameter );
@ -337,11 +337,11 @@ public class BeanMappingMethod extends MappingMethod {
if ( propertyMapping != null && newPropertyMapping != null ) { if ( propertyMapping != null && newPropertyMapping != null ) {
// TODO improve error message // TODO improve error message
ctx.getMessager().printMessage( ctx.getMessager().printMessage(
Diagnostic.Kind.ERROR, Diagnostic.Kind.ERROR,
"Several possible source properties for target property \"" "Several possible source properties for target property \""
+ targetProperty.getKey() + targetProperty.getKey()
+ "\".", + "\".",
method.getExecutable() method.getExecutable()
); );
break; break;
} }
@ -361,7 +361,7 @@ public class BeanMappingMethod extends MappingMethod {
private void applyParameterNameBasedMapping() { private void applyParameterNameBasedMapping() {
Iterator<Entry<String, ExecutableElement>> targetProperties = Iterator<Entry<String, ExecutableElement>> targetProperties =
unprocessedTargetProperties.entrySet().iterator(); unprocessedTargetProperties.entrySet().iterator();
while ( targetProperties.hasNext() ) { while ( targetProperties.hasNext() ) {
@ -376,19 +376,19 @@ public class BeanMappingMethod extends MappingMethod {
Mapping mapping = method.getSingleMappingByTargetPropertyName( targetProperty.getKey() ); Mapping mapping = method.getSingleMappingByTargetPropertyName( targetProperty.getKey() );
SourceReference sourceRef = new SourceReference.BuilderFromProperty() SourceReference sourceRef = new SourceReference.BuilderFromProperty()
.sourceParameter( sourceParameter ) .sourceParameter( sourceParameter )
.name( targetProperty.getKey() ) .name( targetProperty.getKey() )
.build(); .build();
PropertyMapping propertyMapping = new PropertyMappingBuilder() PropertyMapping propertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.souceMethod( method ) .souceMethod( method )
.targetAccessor( targetProperty.getValue() ) .targetAccessor( targetProperty.getValue() )
.targetPropertyName( targetProperty.getKey() ) .targetPropertyName( targetProperty.getKey() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.qualifiers( mapping != null ? mapping.getQualifiers() : null ) .qualifiers( mapping != null ? mapping.getQualifiers() : null )
.dateFormat( mapping != null ? mapping.getDateFormat() : null ) .dateFormat( mapping != null ? mapping.getDateFormat() : null )
.build(); .build();
propertyMappings.add( propertyMapping ); propertyMappings.add( propertyMapping );
targetProperties.remove(); targetProperties.remove();
@ -426,7 +426,7 @@ public class BeanMappingMethod extends MappingMethod {
} }
} }
/** /**
* Returns the effective policy for reporting unmapped getReturnType properties. If explicitly set via * Returns the effective policy for reporting unmapped getReturnType properties. If explicitly set via
* {@code Mapper}, this value will be returned. Otherwise the value from the corresponding processor option will * {@code Mapper}, this value will be returned. Otherwise the value from the corresponding processor option will
* be returned. If that is not set either, the default value from {@code Mapper#unmappedTargetPolicy()} will be * be returned. If that is not set either, the default value from {@code Mapper#unmappedTargetPolicy()} will be
@ -456,8 +456,7 @@ public class BeanMappingMethod extends MappingMethod {
} }
private void reportErrorForUnmappedTargetPropertiesIfRequired() {
private void reportErrorForUnmappedTargetPropertiesIfRequired( ) {
// fetch settings from element to implement // fetch settings from element to implement
ReportingPolicy unmappedTargetPolicy = getEffectiveUnmappedTargetPolicy(); ReportingPolicy unmappedTargetPolicy = getEffectiveUnmappedTargetPolicy();
@ -465,13 +464,13 @@ public class BeanMappingMethod extends MappingMethod {
if ( !unprocessedTargetProperties.isEmpty() && unmappedTargetPolicy.requiresReport() ) { if ( !unprocessedTargetProperties.isEmpty() && unmappedTargetPolicy.requiresReport() ) {
ctx.getMessager().printMessage( ctx.getMessager().printMessage(
unmappedTargetPolicy.getDiagnosticKind(), unmappedTargetPolicy.getDiagnosticKind(),
MessageFormat.format( MessageFormat.format(
"Unmapped target {0,choice,1#property|1<properties}: \"{1}\"", "Unmapped target {0,choice,1#property|1<properties}: \"{1}\"",
unprocessedTargetProperties.size(), unprocessedTargetProperties.size(),
Strings.join( unprocessedTargetProperties.keySet(), ", " ) Strings.join( unprocessedTargetProperties.keySet(), ", " )
), ),
method.getExecutable() method.getExecutable()
); );
} }
} }
@ -480,7 +479,7 @@ public class BeanMappingMethod extends MappingMethod {
private BeanMappingMethod(SourceMethod method, private BeanMappingMethod(SourceMethod method,
List<PropertyMapping> propertyMappings, List<PropertyMapping> propertyMappings,
MethodReference factoryMethod, MethodReference factoryMethod,
boolean mapNullToDefault ) { boolean mapNullToDefault) {
super( method ); super( method );
this.propertyMappings = propertyMappings; this.propertyMappings = propertyMappings;
@ -532,7 +531,7 @@ public class BeanMappingMethod extends MappingMethod {
public List<Parameter> getSourceParametersExcludingPrimitives() { public List<Parameter> getSourceParametersExcludingPrimitives() {
List<Parameter> sourceParameters = new ArrayList<Parameter>(); List<Parameter> sourceParameters = new ArrayList<Parameter>();
for ( Parameter sourceParam : getSourceParameters() ) { for ( Parameter sourceParam : getSourceParameters() ) {
if (!sourceParam.getType().isPrimitive()) { if ( !sourceParam.getType().isPrimitive() ) {
sourceParameters.add( sourceParam ); sourceParameters.add( sourceParam );
} }
} }
@ -543,7 +542,7 @@ public class BeanMappingMethod extends MappingMethod {
public List<Parameter> getSourcePrimitiveParameters() { public List<Parameter> getSourcePrimitiveParameters() {
List<Parameter> sourceParameters = new ArrayList<Parameter>(); List<Parameter> sourceParameters = new ArrayList<Parameter>();
for ( Parameter sourceParam : getSourceParameters() ) { for ( Parameter sourceParam : getSourceParameters() ) {
if (sourceParam.getType().isPrimitive()) { if ( sourceParam.getType().isPrimitive() ) {
sourceParameters.add( sourceParam ); sourceParameters.add( sourceParam );
} }
} }

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.model;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
@ -111,22 +110,22 @@ public class IterableMappingMethod extends MappingMethod {
// mapNullToDefault // mapNullToDefault
NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() ); NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() );
boolean mapNullToDefault boolean mapNullToDefault
= MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism ); = MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism );
MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx ); MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
return new IterableMappingMethod( return new IterableMappingMethod(
method, method,
assignment, assignment,
factoryMethod, factoryMethod,
mapNullToDefault, mapNullToDefault,
ctx.getTypeFactory() ctx.getTypeFactory()
); );
} }
} }
private IterableMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod, private IterableMappingMethod(Method method, Assignment parameterAssignment, MethodReference factoryMethod,
boolean mapNullToDefault, TypeFactory typeFactory ) { boolean mapNullToDefault, TypeFactory typeFactory) {
super( method ); super( method );
this.elementAssignment = parameterAssignment; this.elementAssignment = parameterAssignment;
this.factoryMethod = factoryMethod; this.factoryMethod = factoryMethod;
@ -157,7 +156,7 @@ public class IterableMappingMethod extends MappingMethod {
types.addAll( elementAssignment.getImportTypes() ); types.addAll( elementAssignment.getImportTypes() );
} }
if (mapNullToDefault) { if ( mapNullToDefault ) {
types.add( typeFactory.getType( Collections.class ) ); types.add( typeFactory.getType( Collections.class ) );
} }

View File

@ -21,7 +21,6 @@ package org.mapstruct.ap.model;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
@ -140,10 +139,10 @@ public class MapMappingMethod extends MappingMethod {
ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, message, method.getExecutable() ); ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, message, method.getExecutable() );
} }
// mapNullToDefault // mapNullToDefault
NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() ); NullValueMappingPrism prism = NullValueMappingPrism.getInstanceOn( method.getExecutable() );
boolean mapNullToDefault = boolean mapNullToDefault =
MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism ); MapperConfig.getInstanceOn( ctx.getMapperTypeElement() ).isMapToDefault( prism );
MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx ); MethodReference factoryMethod = AssignmentFactory.createFactoryMethod( method.getReturnType(), ctx );
@ -151,18 +150,18 @@ public class MapMappingMethod extends MappingMethod {
valueAssignment = new LocalVarWrapper( valueAssignment, method.getThrownTypes() ); valueAssignment = new LocalVarWrapper( valueAssignment, method.getThrownTypes() );
return new MapMappingMethod( return new MapMappingMethod(
method, method,
keyAssignment, keyAssignment,
valueAssignment, valueAssignment,
factoryMethod, factoryMethod,
mapNullToDefault, mapNullToDefault,
ctx.getTypeFactory() ctx.getTypeFactory()
); );
} }
} }
private MapMappingMethod(Method method, Assignment keyAssignment, Assignment valueAssignment, private MapMappingMethod(Method method, Assignment keyAssignment, Assignment valueAssignment,
MethodReference factoryMethod, boolean mapNullToDefault, TypeFactory typeFactory) { MethodReference factoryMethod, boolean mapNullToDefault, TypeFactory typeFactory) {
super( method ); super( method );
this.keyAssignment = keyAssignment; this.keyAssignment = keyAssignment;
@ -202,7 +201,7 @@ public class MapMappingMethod extends MappingMethod {
types.addAll( valueAssignment.getImportTypes() ); types.addAll( valueAssignment.getImportTypes() );
} }
if (mapNullToDefault) { if ( mapNullToDefault ) {
types.add( typeFactory.getType( Collections.class ) ); types.add( typeFactory.getType( Collections.class ) );
} }

View File

@ -162,7 +162,7 @@ public class PropertyMapping extends ModelElement {
Diagnostic.Kind.ERROR, Diagnostic.Kind.ERROR,
String.format( String.format(
"Can't map %s to \"%s %s\". " "Can't map %s to \"%s %s\". "
+ "Consider to declare/implement a mapping method: \"%s map(%s value)\".", + "Consider to declare/implement a mapping method: \"%s map(%s value)\".",
sourceElement, sourceElement,
targetType, targetType,
targetPropertyName, targetPropertyName,
@ -182,17 +182,17 @@ public class PropertyMapping extends ModelElement {
} }
private Assignment assignObject(Type sourceType, Type targetType, TargetAccessorType targetAccessorType, private Assignment assignObject(Type sourceType, Type targetType, TargetAccessorType targetAccessorType,
Assignment rhs ) { Assignment rhs) {
Assignment result = rhs; Assignment result = rhs;
if ( targetAccessorType == TargetAccessorType.SETTER ) { if ( targetAccessorType == TargetAccessorType.SETTER ) {
result = new SetterWrapper( result, method.getThrownTypes() ); result = new SetterWrapper( result, method.getThrownTypes() );
if ( !sourceType.isPrimitive() if ( !sourceType.isPrimitive()
&& ( result.getType() == TYPE_CONVERTED && ( result.getType() == TYPE_CONVERTED
|| result.getType() == TYPE_CONVERTED_MAPPED || result.getType() == TYPE_CONVERTED_MAPPED
|| result.getType() == DIRECT && targetType.isPrimitive() ) ) { || result.getType() == DIRECT && targetType.isPrimitive() ) ) {
// for primitive types null check is not possible at all, but a conversion needs // for primitive types null check is not possible at all, but a conversion needs
// a null check. // a null check.
result = new NullCheckWrapper( result ); result = new NullCheckWrapper( result );
} }
@ -201,10 +201,10 @@ public class PropertyMapping extends ModelElement {
// TargetAccessorType must be ADDER // TargetAccessorType must be ADDER
if ( getSourceType().isCollectionType() ) { if ( getSourceType().isCollectionType() ) {
result = new AdderWrapper( result = new AdderWrapper(
result, result,
method.getThrownTypes(), method.getThrownTypes(),
getSourceRef(), getSourceRef(),
sourceType sourceType
); );
} }
else { else {
@ -217,7 +217,7 @@ public class PropertyMapping extends ModelElement {
} }
private Assignment assignCollection( Type targetType, TargetAccessorType targetAccessorType, Assignment rhs ) { private Assignment assignCollection(Type targetType, TargetAccessorType targetAccessorType, Assignment rhs) {
Assignment result = rhs; Assignment result = rhs;
@ -237,9 +237,9 @@ public class PropertyMapping extends ModelElement {
// target accessor is setter, so wrap the setter in setter map/ collection handling // target accessor is setter, so wrap the setter in setter map/ collection handling
result = new SetterCollectionOrMapWrapper( result = new SetterCollectionOrMapWrapper(
result, result,
targetAccessor.getSimpleName().toString(), targetAccessor.getSimpleName().toString(),
newCollectionOrMap newCollectionOrMap
); );
} }
else { else {

View File

@ -22,17 +22,16 @@ import javax.xml.bind.annotation.XmlElementDecl;
import net.java.dev.hickory.prism.GeneratePrism; import net.java.dev.hickory.prism.GeneratePrism;
import net.java.dev.hickory.prism.GeneratePrisms; import net.java.dev.hickory.prism.GeneratePrisms;
import org.mapstruct.DecoratedWith; import org.mapstruct.DecoratedWith;
import org.mapstruct.InheritInverseConfiguration; import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.IterableMapping; import org.mapstruct.IterableMapping;
import org.mapstruct.MapMapping; import org.mapstruct.MapMapping;
import org.mapstruct.NullValueMapping;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.MapperConfig; import org.mapstruct.MapperConfig;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget; import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.NullValueMapping;
import org.mapstruct.Qualifier; import org.mapstruct.Qualifier;
import org.mapstruct.TargetType; import org.mapstruct.TargetType;
@ -52,8 +51,8 @@ import org.mapstruct.TargetType;
@GeneratePrism(value = DecoratedWith.class, publicAccess = true), @GeneratePrism(value = DecoratedWith.class, publicAccess = true),
@GeneratePrism(value = MapperConfig.class, publicAccess = true), @GeneratePrism(value = MapperConfig.class, publicAccess = true),
@GeneratePrism(value = InheritInverseConfiguration.class, publicAccess = true), @GeneratePrism(value = InheritInverseConfiguration.class, publicAccess = true),
@GeneratePrism( value = Qualifier.class, publicAccess = true ), @GeneratePrism(value = Qualifier.class, publicAccess = true),
@GeneratePrism( value = NullValueMapping.class, publicAccess = true ), @GeneratePrism(value = NullValueMapping.class, publicAccess = true),
// external types // external types
@GeneratePrism(value = XmlElementDecl.class, publicAccess = true) @GeneratePrism(value = XmlElementDecl.class, publicAccess = true)

View File

@ -22,20 +22,18 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import org.mapstruct.ap.prism.NullValueMappingStrategyPrism;
import org.mapstruct.ap.prism.NullValueMappingPrism;
import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.option.ReportingPolicy;
import org.mapstruct.ap.prism.CollectionMappingStrategyPrism; import org.mapstruct.ap.prism.CollectionMappingStrategyPrism;
import org.mapstruct.ap.prism.MapperConfigPrism; import org.mapstruct.ap.prism.MapperConfigPrism;
import org.mapstruct.ap.prism.MapperPrism; import org.mapstruct.ap.prism.MapperPrism;
import org.mapstruct.ap.prism.NullValueMappingPrism;
import org.mapstruct.ap.prism.NullValueMappingStrategyPrism;
import static org.mapstruct.ap.prism.CollectionMappingStrategyPrism.valueOf; import static org.mapstruct.ap.prism.CollectionMappingStrategyPrism.valueOf;
@ -124,7 +122,7 @@ public class MapperConfig {
// check on method level // check on method level
if ( mapNullToDefault != null ) { if ( mapNullToDefault != null ) {
NullValueMappingStrategyPrism methodPolicy NullValueMappingStrategyPrism methodPolicy
= NullValueMappingStrategyPrism.valueOf( mapNullToDefault.value() ); = NullValueMappingStrategyPrism.valueOf( mapNullToDefault.value() );
if ( methodPolicy != NullValueMappingStrategyPrism.DEFAULT ) { if ( methodPolicy != NullValueMappingStrategyPrism.DEFAULT ) {
return methodPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT; return methodPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT;
} }
@ -132,7 +130,7 @@ public class MapperConfig {
// check on mapper level // check on mapper level
NullValueMappingStrategyPrism mapperPolicy = NullValueMappingStrategyPrism mapperPolicy =
NullValueMappingStrategyPrism.valueOf( mapperPrism.nullValueMappingStrategy() ); NullValueMappingStrategyPrism.valueOf( mapperPrism.nullValueMappingStrategy() );
if ( mapperPolicy != NullValueMappingStrategyPrism.DEFAULT ) { if ( mapperPolicy != NullValueMappingStrategyPrism.DEFAULT ) {
// it is not the default mapper configuration, so return the mapper configured value // it is not the default mapper configuration, so return the mapper configured value
@ -143,8 +141,8 @@ public class MapperConfig {
else if ( mapperConfigPrism != null ) { else if ( mapperConfigPrism != null ) {
// try the config mapper configuration // try the config mapper configuration
NullValueMappingStrategyPrism configPolicy = NullValueMappingStrategyPrism configPolicy =
NullValueMappingStrategyPrism.valueOf( mapperConfigPrism.nullValueMappingStrategy() ); NullValueMappingStrategyPrism.valueOf( mapperConfigPrism.nullValueMappingStrategy() );
if ( configPolicy != NullValueMappingStrategyPrism.DEFAULT ) { if ( configPolicy != NullValueMappingStrategyPrism.DEFAULT ) {
// its not the default configuration, so return the mapper config configured value // its not the default configuration, so return the mapper config configured value
return configPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT; return configPolicy == NullValueMappingStrategyPrism.RETURN_DEFAULT;
} }

View File

@ -18,8 +18,6 @@
*/ */
package org.mapstruct.ap.test.mapnulltodefault; package org.mapstruct.ap.test.mapnulltodefault;
import static org.mapstruct.NullValueMappingStrategy.RETURN_DEFAULT;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -32,6 +30,8 @@ import org.mapstruct.ap.test.mapnulltodefault.source.Car;
import org.mapstruct.ap.test.mapnulltodefault.target.CarDto; import org.mapstruct.ap.test.mapnulltodefault.target.CarDto;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
import static org.mapstruct.NullValueMappingStrategy.RETURN_DEFAULT;
@Mapper(imports = UUID.class) @Mapper(imports = UUID.class)
public interface CarMapper { public interface CarMapper {

View File

@ -23,15 +23,15 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.NullValueMapping;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.NullValueMapping;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.ap.test.mapnulltodefault.source.Car; import org.mapstruct.ap.test.mapnulltodefault.source.Car;
import org.mapstruct.ap.test.mapnulltodefault.target.CarDto; import org.mapstruct.ap.test.mapnulltodefault.target.CarDto;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@Mapper(imports = UUID.class, config = CentralConfig.class ) @Mapper(imports = UUID.class, config = CentralConfig.class)
public interface CarMapperSettingOnConfig { public interface CarMapperSettingOnConfig {
CarMapperSettingOnConfig INSTANCE = Mappers.getMapper( CarMapperSettingOnConfig.class ); CarMapperSettingOnConfig INSTANCE = Mappers.getMapper( CarMapperSettingOnConfig.class );

View File

@ -23,15 +23,15 @@ import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.NullValueMapping;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
import org.mapstruct.NullValueMapping;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.ap.test.mapnulltodefault.source.Car; import org.mapstruct.ap.test.mapnulltodefault.source.Car;
import org.mapstruct.ap.test.mapnulltodefault.target.CarDto; import org.mapstruct.ap.test.mapnulltodefault.target.CarDto;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
@Mapper(imports = UUID.class, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT ) @Mapper(imports = UUID.class, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface CarMapperSettingOnMapper { public interface CarMapperSettingOnMapper {
CarMapperSettingOnMapper INSTANCE = Mappers.getMapper( CarMapperSettingOnMapper.class ); CarMapperSettingOnMapper INSTANCE = Mappers.getMapper( CarMapperSettingOnMapper.class );

View File

@ -22,8 +22,6 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -32,6 +30,8 @@ import org.mapstruct.ap.test.mapnulltodefault.target.CarDto;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
@WithClasses({ @WithClasses({
Car.class, Car.class,
CarDto.class, CarDto.class,
@ -108,41 +108,41 @@ public class CarMapperTest {
//when //when
List<CarDto> carDtos1 = CarMapper.INSTANCE.carsToCarDtos( Arrays.asList( car ) ); List<CarDto> carDtos1 = CarMapper.INSTANCE.carsToCarDtos( Arrays.asList( car ) );
//then //then
assertThat( carDtos1 ).isNotNull(); assertThat( carDtos1 ).isNotNull();
assertThat( carDtos1.size() ).isEqualTo( 1 ); assertThat( carDtos1.size() ).isEqualTo( 1 );
//when //when
List<CarDto> carDtos2 = CarMapper.INSTANCE.carsToCarDtos( null ); List<CarDto> carDtos2 = CarMapper.INSTANCE.carsToCarDtos( null );
//then //then
assertThat( carDtos2 ).isNotNull(); assertThat( carDtos2 ).isNotNull();
assertThat( carDtos2.isEmpty() ).isTrue(); assertThat( carDtos2.isEmpty() ).isTrue();
} }
@Test @Test
@SuppressWarnings( { "rawtypes", "unchecked" } ) @SuppressWarnings({ "rawtypes", "unchecked" })
public void shouldMapMapWithNullArg() { public void shouldMapMapWithNullArg() {
//given //given
Car car = new Car( "Morris", 2 ); Car car = new Car( "Morris", 2 );
Map carMap = new HashMap(); Map carMap = new HashMap();
carMap.put( 1, car); carMap.put( 1, car );
//when //when
Map<Integer, CarDto> carDtoMap1 = CarMapper.INSTANCE.carsToCarDtoMap( carMap ); Map<Integer, CarDto> carDtoMap1 = CarMapper.INSTANCE.carsToCarDtoMap( carMap );
//then //then
assertThat( carDtoMap1 ).isNotNull(); assertThat( carDtoMap1 ).isNotNull();
assertThat( carDtoMap1.size() ).isEqualTo( 1 ); assertThat( carDtoMap1.size() ).isEqualTo( 1 );
//when //when
Map<Integer, CarDto> carDtoMap2 = CarMapper.INSTANCE.carsToCarDtoMap( null ); Map<Integer, CarDto> carDtoMap2 = CarMapper.INSTANCE.carsToCarDtoMap( null );
//then //then
assertThat( carDtoMap2 ).isNotNull(); assertThat( carDtoMap2 ).isNotNull();
assertThat( carDtoMap2.isEmpty() ).isTrue(); assertThat( carDtoMap2.isEmpty() ).isTrue();
} }
@ -163,23 +163,23 @@ public class CarMapperTest {
@Test @Test
public void shouldMapIterableWithNullArgOnMapper() { public void shouldMapIterableWithNullArgOnMapper() {
//when //when
List<CarDto> carDtos = CarMapperSettingOnMapper.INSTANCE.carsToCarDtos( null ); List<CarDto> carDtos = CarMapperSettingOnMapper.INSTANCE.carsToCarDtos( null );
//then //then
assertThat( carDtos ).isNotNull(); assertThat( carDtos ).isNotNull();
assertThat( carDtos.isEmpty() ).isTrue(); assertThat( carDtos.isEmpty() ).isTrue();
} }
@Test @Test
public void shouldMapMapWithNullArgOnMapper() { public void shouldMapMapWithNullArgOnMapper() {
//when //when
Map<Integer, CarDto> carDtoMap = CarMapperSettingOnMapper.INSTANCE.carsToCarDtoMap( null ); Map<Integer, CarDto> carDtoMap = CarMapperSettingOnMapper.INSTANCE.carsToCarDtoMap( null );
//then //then
assertThat( carDtoMap ).isNull(); assertThat( carDtoMap ).isNull();
} }
@ -201,23 +201,23 @@ public class CarMapperTest {
@Test @Test
public void shouldMapIterableWithNullArgOnConfig() { public void shouldMapIterableWithNullArgOnConfig() {
//when //when
List<CarDto> carDtos = CarMapperSettingOnConfig.INSTANCE.carsToCarDtos( null ); List<CarDto> carDtos = CarMapperSettingOnConfig.INSTANCE.carsToCarDtos( null );
//then //then
assertThat( carDtos ).isNotNull(); assertThat( carDtos ).isNotNull();
assertThat( carDtos.isEmpty() ).isTrue(); assertThat( carDtos.isEmpty() ).isTrue();
} }
@Test @Test
public void shouldMapMapWithNullArgOnConfig() { public void shouldMapMapWithNullArgOnConfig() {
//when //when
Map<Integer, CarDto> carDtoMap = CarMapperSettingOnConfig.INSTANCE.carsToCarDtoMap( null ); Map<Integer, CarDto> carDtoMap = CarMapperSettingOnConfig.INSTANCE.carsToCarDtoMap( null );
//then //then
assertThat( carDtoMap ).isNull(); assertThat( carDtoMap ).isNull();
} }
} }

View File

@ -18,14 +18,13 @@
*/ */
package org.mapstruct.ap.test.mapnulltodefault; package org.mapstruct.ap.test.mapnulltodefault;
import org.mapstruct.NullValueMappingStrategy;
import org.mapstruct.MapperConfig; import org.mapstruct.MapperConfig;
import org.mapstruct.NullValueMappingStrategy;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@MapperConfig( nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT ) @MapperConfig(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public class CentralConfig { public class CentralConfig {
} }

View File

@ -54,7 +54,7 @@ public class CarDto {
return model; return model;
} }
public void setModel( String model ) { public void setModel(String model) {
this.model = model; this.model = model;
} }
@ -62,7 +62,7 @@ public class CarDto {
return catalogId; return catalogId;
} }
public void setCatalogId( String catalogId ) { public void setCatalogId(String catalogId) {
this.catalogId = catalogId; this.catalogId = catalogId;
} }