mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#523 fix type of assignment in GetterWrapperForCollectionsAndMaps
This commit is contained in:
parent
f7433466cd
commit
a431581da8
@ -320,7 +320,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
result = new GetterWrapperForCollectionsAndMaps(
|
result = new GetterWrapperForCollectionsAndMaps(
|
||||||
result,
|
result,
|
||||||
method.getThrownTypes(),
|
method.getThrownTypes(),
|
||||||
targetType,
|
ctx.getTypeFactory().asCollectionOrMap( targetType ),
|
||||||
existingVariableNames
|
existingVariableNames
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -591,7 +591,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
assignment = new GetterWrapperForCollectionsAndMaps(
|
assignment = new GetterWrapperForCollectionsAndMaps(
|
||||||
assignment,
|
assignment,
|
||||||
method.getThrownTypes(),
|
method.getThrownTypes(),
|
||||||
targetType,
|
ctx.getTypeFactory().asCollectionOrMap( targetType ),
|
||||||
existingVariableNames
|
existingVariableNames
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -643,7 +643,7 @@ public class PropertyMapping extends ModelElement {
|
|||||||
assignment = new GetterWrapperForCollectionsAndMaps(
|
assignment = new GetterWrapperForCollectionsAndMaps(
|
||||||
assignment,
|
assignment,
|
||||||
method.getThrownTypes(),
|
method.getThrownTypes(),
|
||||||
targetType,
|
ctx.getTypeFactory().asCollectionOrMap( targetType ),
|
||||||
existingVariableNames
|
existingVariableNames
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ 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;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.util.Strings;
|
import org.mapstruct.ap.util.Strings;
|
||||||
|
|
||||||
@ -42,16 +43,16 @@ import org.mapstruct.ap.util.Strings;
|
|||||||
public class GetterWrapperForCollectionsAndMaps extends AssignmentWrapper {
|
public class GetterWrapperForCollectionsAndMaps extends AssignmentWrapper {
|
||||||
|
|
||||||
private final List<Type> exceptionTypesToExclude;
|
private final List<Type> exceptionTypesToExclude;
|
||||||
private final Type targetType;
|
private final Type localVarType;
|
||||||
private final String localVarName;
|
private final String localVarName;
|
||||||
|
|
||||||
|
|
||||||
public GetterWrapperForCollectionsAndMaps(Assignment decoratedAssignment, List<Type> exceptionTypesToExclude,
|
public GetterWrapperForCollectionsAndMaps(Assignment decoratedAssignment, List<Type> exceptionTypesToExclude,
|
||||||
Type targetType, Collection<String> existingVariableNames ) {
|
Type localVarType, Collection<String> existingVariableNames) {
|
||||||
super( decoratedAssignment );
|
super( decoratedAssignment );
|
||||||
this.exceptionTypesToExclude = exceptionTypesToExclude;
|
this.exceptionTypesToExclude = exceptionTypesToExclude;
|
||||||
this.targetType = targetType;
|
this.localVarType = localVarType;
|
||||||
this.localVarName = Strings.getSaveVariableName( "target" + targetType.getName(), existingVariableNames );
|
this.localVarName = Strings.getSaveVariableName( "target" + localVarType.getName(), existingVariableNames );
|
||||||
existingVariableNames.add( localVarName );
|
existingVariableNames.add( localVarName );
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,10 +74,17 @@ public class GetterWrapperForCollectionsAndMaps extends AssignmentWrapper {
|
|||||||
public Set<Type> getImportTypes() {
|
public Set<Type> getImportTypes() {
|
||||||
Set<Type> imported = new HashSet<Type>();
|
Set<Type> imported = new HashSet<Type>();
|
||||||
imported.addAll( super.getImportTypes() );
|
imported.addAll( super.getImportTypes() );
|
||||||
imported.add( targetType ); /* is a local var */
|
imported.add( localVarType ); /* is a local var */
|
||||||
return imported;
|
return imported;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the targetType
|
||||||
|
*/
|
||||||
|
public Type getLocalVarType() {
|
||||||
|
return localVarType;
|
||||||
|
}
|
||||||
|
|
||||||
public String getLocalVarName() {
|
public String getLocalVarName() {
|
||||||
return localVarName;
|
return localVarName;
|
||||||
}
|
}
|
||||||
|
@ -451,4 +451,35 @@ public class TypeFactory {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts any collection type, e.g. List<T> to Collection<T> and any map type, e.g. HashMap<K,V> to Map<K,V>.
|
||||||
|
*
|
||||||
|
* @param collectionOrMap any collection or map type
|
||||||
|
* @return the type representing Collection<T> or Map<K,V>, if the argument type is a subtype of Collection<T> or of
|
||||||
|
* Map<K,V> respectively.
|
||||||
|
*/
|
||||||
|
public Type asCollectionOrMap(Type collectionOrMap) {
|
||||||
|
List<Type> originalParameters = collectionOrMap.getTypeParameters();
|
||||||
|
TypeMirror[] originalParameterMirrors = new TypeMirror[originalParameters.size()];
|
||||||
|
int i = 0;
|
||||||
|
for ( Type param : originalParameters ) {
|
||||||
|
originalParameterMirrors[i++] = param.getTypeMirror();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( collectionOrMap.isCollectionType()
|
||||||
|
&& !"java.util.Collection".equals( collectionOrMap.getFullyQualifiedName() ) ) {
|
||||||
|
return getType( typeUtils.getDeclaredType(
|
||||||
|
elementUtils.getTypeElement( "java.util.Collection" ),
|
||||||
|
originalParameterMirrors ) );
|
||||||
|
}
|
||||||
|
else if ( collectionOrMap.isMapType()
|
||||||
|
&& !"java.util.Map".equals( collectionOrMap.getFullyQualifiedName() ) ) {
|
||||||
|
return getType( typeUtils.getDeclaredType(
|
||||||
|
elementUtils.getTypeElement( "java.util.Map" ),
|
||||||
|
originalParameterMirrors ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return collectionOrMap;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ if ( ${ext.targetBeanName}.${ext.targetWriteAccessorName}() != null ) {
|
|||||||
</#if>
|
</#if>
|
||||||
}
|
}
|
||||||
<#macro _assignmentLine>
|
<#macro _assignmentLine>
|
||||||
<@includeModel object=ext.targetType/> ${localVarName} = <@_assignment/>;
|
<@includeModel object=localVarType/> ${localVarName} = <@_assignment/>;
|
||||||
if ( ${localVarName} != null ) {
|
if ( ${localVarName} != null ) {
|
||||||
${ext.targetBeanName}.${ext.targetWriteAccessorName}().<#if ext.targetType.collectionType>addAll<#else>putAll</#if>( ${localVarName} );
|
${ext.targetBeanName}.${ext.targetWriteAccessorName}().<#if ext.targetType.collectionType>addAll<#else>putAll</#if>( ${localVarName} );
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,8 @@ public class Source {
|
|||||||
|
|
||||||
private List<String> stringList2;
|
private List<String> stringList2;
|
||||||
|
|
||||||
|
private Set<String> stringSet2;
|
||||||
|
|
||||||
public List<String> getStringList() {
|
public List<String> getStringList() {
|
||||||
return stringList;
|
return stringList;
|
||||||
}
|
}
|
||||||
@ -154,4 +156,12 @@ public class Source {
|
|||||||
this.otherStringLongMap = otherStringLongMap;
|
this.otherStringLongMap = otherStringLongMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<String> getStringSet2() {
|
||||||
|
return stringSet2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStringSet2(Set<String> stringSet2) {
|
||||||
|
this.stringSet2 = stringSet2;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,12 @@ package org.mapstruct.ap.test.collection;
|
|||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.mapstruct.InheritConfiguration;
|
||||||
|
import org.mapstruct.InheritInverseConfiguration;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
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.InheritInverseConfiguration;
|
|
||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
@ -36,19 +37,15 @@ public interface SourceTargetMapper {
|
|||||||
@Mapping(source = "integerList", target = "integerCollection"),
|
@Mapping(source = "integerList", target = "integerCollection"),
|
||||||
@Mapping(source = "integerSet", target = "set"),
|
@Mapping(source = "integerSet", target = "set"),
|
||||||
@Mapping(source = "anotherIntegerSet", target = "anotherStringSet"),
|
@Mapping(source = "anotherIntegerSet", target = "anotherStringSet"),
|
||||||
@Mapping(source = "stringList2", target = "stringListNoSetter")
|
@Mapping(source = "stringList2", target = "stringListNoSetter"),
|
||||||
|
@Mapping(source = "stringSet2", target = "stringListNoSetter2")
|
||||||
})
|
})
|
||||||
Target sourceToTarget(Source source);
|
Target sourceToTarget(Source source);
|
||||||
|
|
||||||
@InheritInverseConfiguration( name = "sourceToTarget" )
|
@InheritInverseConfiguration( name = "sourceToTarget" )
|
||||||
Source targetToSource(Target target);
|
Source targetToSource(Target target);
|
||||||
|
|
||||||
@Mappings({
|
@InheritConfiguration
|
||||||
@Mapping(source = "integerList", target = "integerCollection"),
|
|
||||||
@Mapping(source = "integerSet", target = "set"),
|
|
||||||
@Mapping(source = "anotherIntegerSet", target = "anotherStringSet"),
|
|
||||||
@Mapping(source = "stringList2", target = "stringListNoSetter")
|
|
||||||
})
|
|
||||||
Target sourceToTargetTwoArg(Source source, @MappingTarget Target target);
|
Target sourceToTargetTwoArg(Source source, @MappingTarget Target target);
|
||||||
|
|
||||||
Set<String> integerSetToStringSet(Set<Integer> integers);
|
Set<String> integerSetToStringSet(Set<Integer> integers);
|
||||||
|
@ -50,6 +50,8 @@ public class Target {
|
|||||||
|
|
||||||
private List<String> stringListNoSetter;
|
private List<String> stringListNoSetter;
|
||||||
|
|
||||||
|
private List<String> stringListNoSetter2;
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
private Set set;
|
private Set set;
|
||||||
|
|
||||||
@ -149,6 +151,13 @@ public class Target {
|
|||||||
return stringListNoSetter;
|
return stringListNoSetter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getStringListNoSetter2() {
|
||||||
|
if ( stringListNoSetter2 == null ) {
|
||||||
|
stringListNoSetter2 = new ArrayList<String>();
|
||||||
|
}
|
||||||
|
return stringListNoSetter2;
|
||||||
|
}
|
||||||
|
|
||||||
public Map<String, Long> getOtherStringLongMap() {
|
public Map<String, Long> getOtherStringLongMap() {
|
||||||
return otherStringLongMap;
|
return otherStringLongMap;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user