mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#289 adding extra null check before add/putAll of a target collection when having a getter only targetaccessor
This commit is contained in:
parent
bfaa524cb7
commit
a381396839
@ -20,6 +20,7 @@ package org.mapstruct.ap.model;
|
|||||||
|
|
||||||
import java.text.MessageFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@ -74,6 +75,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
private List<TypeMirror> qualifiers;
|
private List<TypeMirror> qualifiers;
|
||||||
private NullValueMappingStrategyPrism nullValueMappingStrategy;
|
private NullValueMappingStrategyPrism nullValueMappingStrategy;
|
||||||
private TypeMirror resultTypeMirror;
|
private TypeMirror resultTypeMirror;
|
||||||
|
private final Collection<String> existingVariableNames = new HashSet<String>();
|
||||||
|
|
||||||
public Builder mappingContext(MappingBuilderContext mappingContext) {
|
public Builder mappingContext(MappingBuilderContext mappingContext) {
|
||||||
this.ctx = mappingContext;
|
this.ctx = mappingContext;
|
||||||
@ -88,6 +90,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
||||||
unprocessedSourceParameters.add( sourceParameter );
|
unprocessedSourceParameters.add( sourceParameter );
|
||||||
}
|
}
|
||||||
|
existingVariableNames.addAll( method.getParameterNames() );
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +151,14 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new BeanMappingMethod( method, propertyMappings, factoryMethod, mapNullToDefault, resultType );
|
return new BeanMappingMethod(
|
||||||
|
method,
|
||||||
|
propertyMappings,
|
||||||
|
factoryMethod,
|
||||||
|
mapNullToDefault,
|
||||||
|
resultType,
|
||||||
|
existingVariableNames
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -210,6 +220,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
.qualifiers( mapping.getQualifiers() )
|
.qualifiers( mapping.getQualifiers() )
|
||||||
.resultType( mapping.getResultType() )
|
.resultType( mapping.getResultType() )
|
||||||
.dateFormat( mapping.getDateFormat() )
|
.dateFormat( mapping.getDateFormat() )
|
||||||
|
.existingVariableNames( existingVariableNames )
|
||||||
.build();
|
.build();
|
||||||
handledTargets.add( mapping.getTargetName() );
|
handledTargets.add( mapping.getTargetName() );
|
||||||
unprocessedSourceParameters.remove( sourceRef.getParameter() );
|
unprocessedSourceParameters.remove( sourceRef.getParameter() );
|
||||||
@ -231,6 +242,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
.dateFormat( mapping.getDateFormat() )
|
.dateFormat( mapping.getDateFormat() )
|
||||||
.qualifiers( mapping.getQualifiers() )
|
.qualifiers( mapping.getQualifiers() )
|
||||||
.resultType( mapping.getResultType() )
|
.resultType( mapping.getResultType() )
|
||||||
|
.existingVariableNames( existingVariableNames )
|
||||||
.build();
|
.build();
|
||||||
handledTargets.add( mapping.getTargetName() );
|
handledTargets.add( mapping.getTargetName() );
|
||||||
}
|
}
|
||||||
@ -243,6 +255,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
.souceMethod( method )
|
.souceMethod( method )
|
||||||
.javaExpression( mapping.getJavaExpression() )
|
.javaExpression( mapping.getJavaExpression() )
|
||||||
.targetAccessor( targetProperty )
|
.targetAccessor( targetProperty )
|
||||||
|
.existingVariableNames( existingVariableNames )
|
||||||
.build();
|
.build();
|
||||||
handledTargets.add( mapping.getTargetName() );
|
handledTargets.add( mapping.getTargetName() );
|
||||||
}
|
}
|
||||||
@ -319,6 +332,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
.qualifiers( mapping != null ? mapping.getQualifiers() : null )
|
.qualifiers( mapping != null ? mapping.getQualifiers() : null )
|
||||||
.resultType( mapping != null ? mapping.getResultType() : null )
|
.resultType( mapping != null ? mapping.getResultType() : null )
|
||||||
.dateFormat( mapping != null ? mapping.getDateFormat() : null )
|
.dateFormat( mapping != null ? mapping.getDateFormat() : null )
|
||||||
|
.existingVariableNames( existingVariableNames )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
unprocessedSourceParameters.remove( sourceParameter );
|
unprocessedSourceParameters.remove( sourceParameter );
|
||||||
@ -379,6 +393,7 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
.qualifiers( mapping != null ? mapping.getQualifiers() : null )
|
.qualifiers( mapping != null ? mapping.getQualifiers() : null )
|
||||||
.resultType( mapping != null ? mapping.getResultType() : null )
|
.resultType( mapping != null ? mapping.getResultType() : null )
|
||||||
.dateFormat( mapping != null ? mapping.getDateFormat() : null )
|
.dateFormat( mapping != null ? mapping.getDateFormat() : null )
|
||||||
|
.existingVariableNames( existingVariableNames )
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
propertyMappings.add( propertyMapping );
|
propertyMappings.add( propertyMapping );
|
||||||
@ -466,8 +481,9 @@ public class BeanMappingMethod extends MappingMethod {
|
|||||||
List<PropertyMapping> propertyMappings,
|
List<PropertyMapping> propertyMappings,
|
||||||
MethodReference factoryMethod,
|
MethodReference factoryMethod,
|
||||||
boolean mapNullToDefault,
|
boolean mapNullToDefault,
|
||||||
Type resultType ) {
|
Type resultType,
|
||||||
super( method );
|
Collection<String> existingVariableNames ) {
|
||||||
|
super( method, existingVariableNames );
|
||||||
this.propertyMappings = propertyMappings;
|
this.propertyMappings = propertyMappings;
|
||||||
|
|
||||||
// intialize constant mappings as all mappings, but take out the ones that can be contributed to a
|
// intialize constant mappings as all mappings, but take out the ones that can be contributed to a
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -45,8 +46,16 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
private final Accessibility accessibility;
|
private final Accessibility accessibility;
|
||||||
private final List<Type> thrownTypes;
|
private final List<Type> thrownTypes;
|
||||||
private final boolean isStatic;
|
private final boolean isStatic;
|
||||||
|
private final String resultName;
|
||||||
|
|
||||||
protected MappingMethod(Method method) {
|
/**
|
||||||
|
* constructor to be overloaded when local variable names are required prior to calling this constructor. (e.g.
|
||||||
|
* for property mappings). It is supposed to be initialized with at least the parameter names.
|
||||||
|
*
|
||||||
|
* @param method
|
||||||
|
* @param existingVariableNames
|
||||||
|
*/
|
||||||
|
protected MappingMethod(Method method, Collection<String> existingVariableNames ) {
|
||||||
this.name = method.getName();
|
this.name = method.getName();
|
||||||
this.parameters = method.getParameters();
|
this.parameters = method.getParameters();
|
||||||
this.returnType = method.getReturnType();
|
this.returnType = method.getReturnType();
|
||||||
@ -54,6 +63,34 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
this.accessibility = method.getAccessibility();
|
this.accessibility = method.getAccessibility();
|
||||||
this.thrownTypes = method.getThrownTypes();
|
this.thrownTypes = method.getThrownTypes();
|
||||||
this.isStatic = method.isStatic();
|
this.isStatic = method.isStatic();
|
||||||
|
this.resultName = initResultName( existingVariableNames );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected MappingMethod(Method method ) {
|
||||||
|
this.name = method.getName();
|
||||||
|
this.parameters = method.getParameters();
|
||||||
|
this.returnType = method.getReturnType();
|
||||||
|
this.targetParameter = method.getMappingTargetParameter();
|
||||||
|
this.accessibility = method.getAccessibility();
|
||||||
|
this.thrownTypes = method.getThrownTypes();
|
||||||
|
this.isStatic = method.isStatic();
|
||||||
|
this.resultName = initResultName( method.getParameterNames() );
|
||||||
|
}
|
||||||
|
|
||||||
|
private String initResultName(Collection<String> existingVarNames) {
|
||||||
|
if ( targetParameter != null ) {
|
||||||
|
return targetParameter.getName();
|
||||||
|
}
|
||||||
|
else if ( getResultType().isArrayType() ) {
|
||||||
|
String name = getSaveVariableName( getResultType().getComponentType().getName() + "Tmp", existingVarNames );
|
||||||
|
existingVarNames.add( name );
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
String name = getSaveVariableName( getResultType().getName(), existingVarNames );
|
||||||
|
existingVarNames.add( name );
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -81,15 +118,7 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getResultName() {
|
public String getResultName() {
|
||||||
if ( targetParameter != null ) {
|
return resultName;
|
||||||
return targetParameter.getName();
|
|
||||||
}
|
|
||||||
else if ( getResultType().isArrayType() ) {
|
|
||||||
return getSaveVariableName( getResultType().getComponentType().getName() + "Tmp", getParameterNames() );
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return getSaveVariableName( getResultType().getName(), getParameterNames() );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getReturnType() {
|
public Type getReturnType() {
|
||||||
@ -139,4 +168,6 @@ public abstract class MappingMethod extends ModelElement {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return returnType + " " + getName() + "(" + join( parameters, ", " ) + ")";
|
return returnType + " " + getName() + "(" + join( parameters, ", " ) + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
package org.mapstruct.ap.model;
|
package org.mapstruct.ap.model;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.lang.model.element.ExecutableElement;
|
import javax.lang.model.element.ExecutableElement;
|
||||||
@ -27,10 +28,10 @@ import javax.lang.model.type.TypeMirror;
|
|||||||
import org.mapstruct.ap.model.assignment.AdderWrapper;
|
import org.mapstruct.ap.model.assignment.AdderWrapper;
|
||||||
import org.mapstruct.ap.model.assignment.ArrayCopyWrapper;
|
import org.mapstruct.ap.model.assignment.ArrayCopyWrapper;
|
||||||
import org.mapstruct.ap.model.assignment.Assignment;
|
import org.mapstruct.ap.model.assignment.Assignment;
|
||||||
import org.mapstruct.ap.model.assignment.GetterCollectionOrMapWrapper;
|
import org.mapstruct.ap.model.assignment.GetterWrapperForCollectionsAndMaps;
|
||||||
import org.mapstruct.ap.model.assignment.NewCollectionOrMapWrapper;
|
import org.mapstruct.ap.model.assignment.NewCollectionOrMapWrapper;
|
||||||
import org.mapstruct.ap.model.assignment.NullCheckWrapper;
|
import org.mapstruct.ap.model.assignment.NullCheckWrapper;
|
||||||
import org.mapstruct.ap.model.assignment.SetterCollectionOrMapWrapper;
|
import org.mapstruct.ap.model.assignment.SetterWrapperForCollectionsAndMaps;
|
||||||
import org.mapstruct.ap.model.assignment.SetterWrapper;
|
import org.mapstruct.ap.model.assignment.SetterWrapper;
|
||||||
import org.mapstruct.ap.model.common.ModelElement;
|
import org.mapstruct.ap.model.common.ModelElement;
|
||||||
import org.mapstruct.ap.model.common.Parameter;
|
import org.mapstruct.ap.model.common.Parameter;
|
||||||
@ -72,6 +73,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
private List<TypeMirror> qualifiers;
|
private List<TypeMirror> qualifiers;
|
||||||
private TypeMirror resultType;
|
private TypeMirror resultType;
|
||||||
private SourceReference sourceReference;
|
private SourceReference sourceReference;
|
||||||
|
private Collection<String> existingVariableNames;
|
||||||
|
|
||||||
public PropertyMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
|
public PropertyMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
|
||||||
this.ctx = mappingContext;
|
this.ctx = mappingContext;
|
||||||
@ -113,6 +115,11 @@ public class PropertyMapping extends ModelElement {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PropertyMappingBuilder existingVariableNames(Collection<String> existingVariableNames) {
|
||||||
|
this.existingVariableNames = existingVariableNames;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
private enum TargetAccessorType {
|
private enum TargetAccessorType {
|
||||||
|
|
||||||
GETTER,
|
GETTER,
|
||||||
@ -245,18 +252,20 @@ public class PropertyMapping extends ModelElement {
|
|||||||
result = new SetterWrapper( result, method.getThrownTypes() );
|
result = new SetterWrapper( result, method.getThrownTypes() );
|
||||||
|
|
||||||
// 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 SetterWrapperForCollectionsAndMaps(
|
||||||
result,
|
result,
|
||||||
targetAccessor.getSimpleName().toString(),
|
targetAccessor.getSimpleName().toString(),
|
||||||
newCollectionOrMap
|
newCollectionOrMap
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// wrap the assignment in the setter method
|
|
||||||
result = new SetterWrapper( result, method.getThrownTypes() );
|
|
||||||
|
|
||||||
// target accessor is getter, so wrap the setter in getter map/ collection handling
|
// target accessor is getter, so wrap the setter in getter map/ collection handling
|
||||||
result = new GetterCollectionOrMapWrapper( result );
|
result = new GetterWrapperForCollectionsAndMaps(
|
||||||
|
result,
|
||||||
|
method.getThrownTypes(),
|
||||||
|
targetType,
|
||||||
|
existingVariableNames
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// For collections and maps include a null check, when the assignment type is DIRECT.
|
// For collections and maps include a null check, when the assignment type is DIRECT.
|
||||||
@ -453,6 +462,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
private String dateFormat;
|
private String dateFormat;
|
||||||
private List<TypeMirror> qualifiers;
|
private List<TypeMirror> qualifiers;
|
||||||
private TypeMirror resultType;
|
private TypeMirror resultType;
|
||||||
|
private Collection<String> existingVariableNames;
|
||||||
|
|
||||||
public ConstantMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
|
public ConstantMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
|
||||||
this.ctx = mappingContext;
|
this.ctx = mappingContext;
|
||||||
@ -489,6 +499,11 @@ public class PropertyMapping extends ModelElement {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ConstantMappingBuilder existingVariableNames(Collection<String> existingVariableNames) {
|
||||||
|
this.existingVariableNames = existingVariableNames;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public PropertyMapping build() {
|
public PropertyMapping build() {
|
||||||
|
|
||||||
// source
|
// source
|
||||||
@ -520,12 +535,18 @@ public class PropertyMapping extends ModelElement {
|
|||||||
|
|
||||||
if ( assignment != null ) {
|
if ( assignment != null ) {
|
||||||
|
|
||||||
// target accessor is setter, so decorate assignment as setter
|
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
// target accessor is setter, so decorate assignment as setter
|
||||||
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
// wrap when dealing with getter only on target
|
}
|
||||||
if ( Executables.isGetterMethod( targetAccessor ) ) {
|
else {
|
||||||
assignment = new GetterCollectionOrMapWrapper( assignment );
|
// wrap when dealing with getter only on target
|
||||||
|
assignment = new GetterWrapperForCollectionsAndMaps(
|
||||||
|
assignment,
|
||||||
|
method.getThrownTypes(),
|
||||||
|
targetType,
|
||||||
|
existingVariableNames
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -548,6 +569,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
private SourceMethod method;
|
private SourceMethod method;
|
||||||
private String javaExpression;
|
private String javaExpression;
|
||||||
private ExecutableElement targetAccessor;
|
private ExecutableElement targetAccessor;
|
||||||
|
private Collection<String> existingVariableNames;
|
||||||
|
|
||||||
public JavaExpressionMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
|
public JavaExpressionMappingBuilder mappingContext(MappingBuilderContext mappingContext) {
|
||||||
this.ctx = mappingContext;
|
this.ctx = mappingContext;
|
||||||
@ -569,20 +591,30 @@ public class PropertyMapping extends ModelElement {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JavaExpressionMappingBuilder existingVariableNames(Collection<String> existingVariableNames) {
|
||||||
|
this.existingVariableNames = existingVariableNames;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public PropertyMapping build() {
|
public PropertyMapping build() {
|
||||||
|
|
||||||
Assignment assignment = AssignmentFactory.createDirect( javaExpression );
|
Assignment assignment = AssignmentFactory.createDirect( javaExpression );
|
||||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
|
||||||
|
|
||||||
Type targetType;
|
Type targetType;
|
||||||
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||||
|
// setter, so wrap in setter
|
||||||
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
targetType = ctx.getTypeFactory().getSingleParameter( targetAccessor ).getType();
|
targetType = ctx.getTypeFactory().getSingleParameter( targetAccessor ).getType();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
targetType = ctx.getTypeFactory().getReturnType( targetAccessor );
|
targetType = ctx.getTypeFactory().getReturnType( targetAccessor );
|
||||||
|
|
||||||
// target accessor is getter, so wrap the setter in getter map/ collection handling
|
// target accessor is getter, so wrap the setter in getter map/ collection handling
|
||||||
assignment = new GetterCollectionOrMapWrapper( assignment );
|
assignment = new GetterWrapperForCollectionsAndMaps(
|
||||||
|
assignment,
|
||||||
|
method.getThrownTypes(),
|
||||||
|
targetType,
|
||||||
|
existingVariableNames
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PropertyMapping( targetAccessor.getSimpleName().toString(), targetType, assignment );
|
return new PropertyMapping( targetAccessor.getSimpleName().toString(), targetType, assignment );
|
||||||
|
@ -0,0 +1,84 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.model.assignment;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.mapstruct.ap.model.common.Type;
|
||||||
|
import org.mapstruct.ap.util.Strings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This wrapper handles the situation were an assignment must be done via a target getter method because there
|
||||||
|
* is no setter available.
|
||||||
|
*
|
||||||
|
* The wrapper checks if there is an collection or map initialized on the target bean (not null). If so it uses the
|
||||||
|
* addAll (for collections) or putAll (for maps). The collection / map is cleared in case of a pre-existing target
|
||||||
|
* {@link org.mapstruct.MappingTarget }before adding the source entries. The goal is that the same collection / map
|
||||||
|
* is used as target.
|
||||||
|
*
|
||||||
|
* Nothing can be added if the getter on the target returns null.
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class GetterWrapperForCollectionsAndMaps extends AssignmentWrapper {
|
||||||
|
|
||||||
|
private final List<Type> exceptionTypesToExclude;
|
||||||
|
private final Type targetType;
|
||||||
|
private final String localVarName;
|
||||||
|
|
||||||
|
|
||||||
|
public GetterWrapperForCollectionsAndMaps(Assignment decoratedAssignment, List<Type> exceptionTypesToExclude,
|
||||||
|
Type targetType, Collection<String> existingVariableNames ) {
|
||||||
|
super( decoratedAssignment );
|
||||||
|
this.exceptionTypesToExclude = exceptionTypesToExclude;
|
||||||
|
this.targetType = targetType;
|
||||||
|
this.localVarName = Strings.getSaveVariableName( "target" + targetType.getName(), existingVariableNames );
|
||||||
|
existingVariableNames.add( localVarName );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Type> getExceptionTypes() {
|
||||||
|
List<Type> parentExceptionTypes = super.getExceptionTypes();
|
||||||
|
List<Type> result = new ArrayList<Type>( parentExceptionTypes );
|
||||||
|
for ( Type exceptionTypeToExclude : exceptionTypesToExclude ) {
|
||||||
|
for ( Type parentExceptionType : parentExceptionTypes ) {
|
||||||
|
if ( parentExceptionType.isAssignableTo( exceptionTypeToExclude ) ) {
|
||||||
|
result.remove( parentExceptionType );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Type> getImportTypes() {
|
||||||
|
Set<Type> imported = new HashSet<Type>();
|
||||||
|
imported.addAll( super.getImportTypes() );
|
||||||
|
imported.add( targetType ); /* is a local var */
|
||||||
|
return imported;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLocalVarName() {
|
||||||
|
return localVarName;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -35,12 +35,12 @@ import org.mapstruct.ap.model.common.Type;
|
|||||||
*
|
*
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
public class SetterCollectionOrMapWrapper extends AssignmentWrapper {
|
public class SetterWrapperForCollectionsAndMaps extends AssignmentWrapper {
|
||||||
|
|
||||||
private final String targetGetterName;
|
private final String targetGetterName;
|
||||||
private final Assignment newCollectionOrMapAssignment;
|
private final Assignment newCollectionOrMapAssignment;
|
||||||
|
|
||||||
public SetterCollectionOrMapWrapper(Assignment decoratedAssignment,
|
public SetterWrapperForCollectionsAndMaps(Assignment decoratedAssignment,
|
||||||
String targetSetterName,
|
String targetSetterName,
|
||||||
Assignment newCollectionOrMapAssignment) {
|
Assignment newCollectionOrMapAssignment) {
|
||||||
super( decoratedAssignment );
|
super( decoratedAssignment );
|
@ -22,19 +22,31 @@ if ( ${ext.targetBeanName}.${ext.targetAccessorName}() != null ) {
|
|||||||
<#if ext.existingInstanceMapping>
|
<#if ext.existingInstanceMapping>
|
||||||
${ext.targetBeanName}.${ext.targetAccessorName}().clear();
|
${ext.targetBeanName}.${ext.targetAccessorName}().clear();
|
||||||
</#if>
|
</#if>
|
||||||
<#if ext.targetType.collectionType>
|
<#if (exceptionTypes?size == 0) >
|
||||||
<@includeModel object=assignment
|
<@_assignmentLine/>
|
||||||
targetBeanName=ext.targetBeanName
|
<#else>
|
||||||
raw=ext.raw
|
try {
|
||||||
existingInstanceMapping=ext.existingInstanceMapping
|
<@_assignmentLine/>
|
||||||
targetAccessorName="${ext.targetAccessorName}().addAll"
|
}
|
||||||
targetType=ext.targetType/>
|
<#list exceptionTypes as exceptionType>
|
||||||
<#else>
|
catch ( <@includeModel object=exceptionType/> e ) {
|
||||||
<@includeModel object=assignment
|
throw new RuntimeException( e );
|
||||||
targetBeanName=ext.targetBeanName
|
}
|
||||||
raw=ext.raw
|
</#list>
|
||||||
existingInstanceMapping=ext.existingInstanceMapping
|
|
||||||
targetAccessorName="${ext.targetAccessorName}().putAll"
|
|
||||||
targetType=ext.targetType/>
|
|
||||||
</#if>
|
</#if>
|
||||||
}
|
}
|
||||||
|
<#macro _assignmentLine>
|
||||||
|
<@includeModel object=ext.targetType/> ${localVarName} = <@_assignment/>;
|
||||||
|
if ( ${localVarName} != null ) {
|
||||||
|
${ext.targetBeanName}.${ext.targetAccessorName}().<#if ext.targetType.collectionType>addAll<#else>putAll</#if>( ${localVarName} );
|
||||||
|
}
|
||||||
|
</#macro>
|
||||||
|
<#macro _assignment>
|
||||||
|
<@includeModel object=assignment
|
||||||
|
targetBeanName=ext.targetBeanName
|
||||||
|
raw=ext.raw
|
||||||
|
existingInstanceMapping=ext.existingInstanceMapping
|
||||||
|
targetReadAccessorName=ext.targetReadAccessorName
|
||||||
|
targetWriteAccessorName=ext.targetWriteAccessorName
|
||||||
|
targetType=ext.targetType/>
|
||||||
|
</#macro>
|
@ -0,0 +1,41 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingTarget;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface Issue289Mapper {
|
||||||
|
|
||||||
|
Issue289Mapper INSTANCE = Mappers.getMapper( Issue289Mapper.class );
|
||||||
|
|
||||||
|
TargetWithoutSetter sourceToTargetWithoutSetter(Source source);
|
||||||
|
|
||||||
|
void sourceToTargetWithoutSetter(Source source, @MappingTarget TargetWithoutSetter target);
|
||||||
|
|
||||||
|
TargetWithSetter sourceToTargetWithSetter(Source source);
|
||||||
|
|
||||||
|
TargetElement sourceElementToTargetElement(SourceElement source);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reproducer for https://github.com/mapstruct/mapstruct/issues/289.
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@IssueKey( "289" )
|
||||||
|
@WithClasses( {
|
||||||
|
Issue289Mapper.class,
|
||||||
|
Source.class,
|
||||||
|
TargetWithoutSetter.class,
|
||||||
|
TargetWithSetter.class,
|
||||||
|
SourceElement.class,
|
||||||
|
TargetElement.class
|
||||||
|
} )
|
||||||
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
|
public class Issue289Test {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldLeaveEmptyTargetSetWhenSourceIsNullAndGetterOnlyForCreateMethod() {
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setCollection( null );
|
||||||
|
|
||||||
|
TargetWithoutSetter target = Issue289Mapper.INSTANCE.sourceToTargetWithoutSetter( source );
|
||||||
|
|
||||||
|
assertThat( target.getCollection() ).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldLeaveEmptyTargetSetWhenSourceIsNullAndGetterOnlyForUpdateMethod() {
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setCollection( null );
|
||||||
|
TargetWithoutSetter target = new TargetWithoutSetter();
|
||||||
|
target.getCollection().add( new TargetElement() );
|
||||||
|
|
||||||
|
Issue289Mapper.INSTANCE.sourceToTargetWithoutSetter( source, target );
|
||||||
|
|
||||||
|
assertThat( target.getCollection() ).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldLeaveNullTargetSetWhenSourceIsNullForCreateMethod() {
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
source.setCollection( null );
|
||||||
|
|
||||||
|
TargetWithSetter target = Issue289Mapper.INSTANCE.sourceToTargetWithSetter( source );
|
||||||
|
|
||||||
|
assertThat( target.getCollection() ).isNull();
|
||||||
|
}
|
||||||
|
}
|
@ -16,26 +16,24 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.model.assignment;
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This wrapper handles the situation were an assignment must be done via a target getter method because there
|
|
||||||
* is no setter available.
|
|
||||||
*
|
|
||||||
* The wrapper checks if there is an collection or map initialized on the target bean (not null). If so it uses the
|
|
||||||
* addAll (for collections) or putAll (for maps). The collection / map is cleared in case of a pre-existing target
|
|
||||||
* {@link org.mapstruct.MappingTarget }before adding the source entries. The goal is that the same collection / map
|
|
||||||
* is used as target.
|
|
||||||
*
|
|
||||||
* Nothing can be added if the getter on the target returns null.
|
|
||||||
*
|
*
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
public class GetterCollectionOrMapWrapper extends AssignmentWrapper {
|
public class Source {
|
||||||
|
|
||||||
|
private Collection<SourceElement> collection;
|
||||||
|
|
||||||
public GetterCollectionOrMapWrapper(Assignment decoratedAssignment) {
|
public Collection<SourceElement> getCollection() {
|
||||||
super( decoratedAssignment );
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCollection(Collection<SourceElement> collection) {
|
||||||
|
this.collection = collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class SourceElement {
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class TargetElement {
|
||||||
|
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class TargetWithSetter {
|
||||||
|
|
||||||
|
private Collection<TargetElement> collection;
|
||||||
|
|
||||||
|
public Collection<TargetElement> getCollection() {
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCollection( Collection<TargetElement> collection ) {
|
||||||
|
this.collection = collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._289;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class TargetWithoutSetter {
|
||||||
|
|
||||||
|
private final Collection<TargetElement> collection = new ArrayList<TargetElement>();
|
||||||
|
|
||||||
|
public Collection<TargetElement> getCollection() {
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user