#919 Fix compilation error in map-mapping methods with non-generic maps as source

This commit is contained in:
Andreas Gudian 2016-10-09 17:39:13 +02:00
parent 4ab3c2d537
commit d94d5857ce
7 changed files with 108 additions and 27 deletions

View File

@ -52,7 +52,7 @@
</#if>
</#list>
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
for ( java.util.Map.Entry<<#list sourceParameter.type.typeParameters as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>> ${entryVariableName} : ${sourceParameter.name}.entrySet() ) {
for ( java.util.Map.Entry<<#list sourceElementTypes as typeParameter><@includeModel object=typeParameter /><#if typeParameter_has_next>, </#if></#list>> ${entryVariableName} : ${sourceParameter.name}.entrySet() ) {
<#-- key -->
<@includeModel object=keyAssignment
targetWriteAccessorName=keyVariableName

View File

@ -37,7 +37,9 @@ import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
@WithClasses({ Source.class, Target.class, Colour.class, SourceTargetMapper.class, TestList.class, TestMap.class,
StringArrayList.class, StringToLongMap.class })
StringHolderArrayList.class,
StringHolderToLongMap.class,
StringHolder.class })
@RunWith(AnnotationProcessorTestRunner.class)
public class CollectionMappingTest {
@ -401,12 +403,14 @@ public class CollectionMappingTest {
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getNonGenericStringList() ).containsExactly( "Bob", "Alice" );
assertThat( target.getNonGenericStringList() ).containsExactly(
new StringHolder( "Bob" ),
new StringHolder( "Alice" ) );
// Inverse direction
Target newTarget = new Target();
StringArrayList nonGenericStringList = new StringArrayList();
nonGenericStringList.addAll( Arrays.asList( "Bill", "Bob" ) );
StringHolderArrayList nonGenericStringList = new StringHolderArrayList();
nonGenericStringList.addAll( Arrays.asList( new StringHolder( "Bill" ), new StringHolder( "Bob" ) ) );
newTarget.setNonGenericStringList( nonGenericStringList );
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( newTarget );
@ -415,6 +419,7 @@ public class CollectionMappingTest {
assertThat( mappedSource.getStringList3() ).containsExactly( "Bill", "Bob" );
}
@SuppressWarnings("unchecked")
@Test
@IssueKey("853")
public void shouldMapNonGenericMap() {
@ -427,13 +432,15 @@ public class CollectionMappingTest {
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getNonGenericMapStringtoLong() ).contains( entry( "Bob", 123L ), entry( "Alice", 456L ) );
assertThat( target.getNonGenericMapStringtoLong() ).contains(
entry( new StringHolder( "Bob" ), 123L ),
entry( new StringHolder( "Alice" ), 456L ) );
// Inverse direction
Target newTarget = new Target();
StringToLongMap stringToLongMap = new StringToLongMap();
stringToLongMap.put( "Blue", 321L );
stringToLongMap.put( "Green", 654L );
StringHolderToLongMap stringToLongMap = new StringHolderToLongMap();
stringToLongMap.put( new StringHolder( "Blue" ), 321L );
stringToLongMap.put( new StringHolder( "Green" ), 654L );
newTarget.setNonGenericMapStringtoLong( stringToLongMap );
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( newTarget );

View File

@ -29,9 +29,9 @@ import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
@Mapper
public interface SourceTargetMapper {
public abstract class SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
static final SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mappings({
@Mapping(source = "integerList", target = "integerCollection"),
@ -42,23 +42,31 @@ public interface SourceTargetMapper {
@Mapping(source = "stringList3", target = "nonGenericStringList"),
@Mapping(source = "stringLongMapForNonGeneric", target = "nonGenericMapStringtoLong")
})
Target sourceToTarget(Source source);
public abstract Target sourceToTarget(Source source);
@InheritInverseConfiguration( name = "sourceToTarget" )
Source targetToSource(Target target);
public abstract Source targetToSource(Target target);
@InheritConfiguration
Target sourceToTargetTwoArg(Source source, @MappingTarget Target target);
public abstract Target sourceToTargetTwoArg(Source source, @MappingTarget Target target);
Set<String> integerSetToStringSet(Set<Integer> integers);
public abstract Set<String> integerSetToStringSet(Set<Integer> integers);
@InheritInverseConfiguration
Set<Integer> stringSetToIntegerSet(Set<String> strings);
public abstract Set<Integer> stringSetToIntegerSet(Set<String> strings);
Set<String> colourSetToStringSet(Set<Colour> colours);
public abstract Set<String> colourSetToStringSet(Set<Colour> colours);
@InheritInverseConfiguration
Set<Colour> stringSetToColourSet(Set<String> colours);
public abstract Set<Colour> stringSetToColourSet(Set<String> colours);
Set<Number> integerSetToNumberSet(Set<Integer> integers);
public abstract Set<Number> integerSetToNumberSet(Set<Integer> integers);
protected StringHolder toStringHolder(String string) {
return new StringHolder( string );
}
protected String toString(StringHolder string) {
return string.getString();
}
}

View File

@ -0,0 +1,66 @@
/**
* Copyright 2012-2016 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.collection;
/**
* @author Andreas Gudian
*
*/
public class StringHolder {
private final String string;
public StringHolder(String string) {
this.string = string;
}
public String getString() {
return string;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( string == null ) ? 0 : string.hashCode() );
return result;
}
@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
StringHolder other = (StringHolder) obj;
if ( string == null ) {
if ( other.string != null ) {
return false;
}
}
else if ( !string.equals( other.string ) ) {
return false;
}
return true;
}
}

View File

@ -23,7 +23,7 @@ import java.util.ArrayList;
/**
* @author Stefan May
*/
public class StringArrayList extends ArrayList<String> {
public class StringHolderArrayList extends ArrayList<StringHolder> {
private static final long serialVersionUID = 1L;
}

View File

@ -23,7 +23,7 @@ import java.util.HashMap;
/**
* @author Stefan May
*/
public class StringToLongMap extends HashMap<String, Long> {
public class StringHolderToLongMap extends HashMap<StringHolder, Long> {
private static final long serialVersionUID = 1L;

View File

@ -58,9 +58,9 @@ public class Target {
private EnumSet<Colour> enumSet;
private StringArrayList nonGenericStringList;
private StringHolderArrayList nonGenericStringList;
private StringToLongMap nonGenericMapStringtoLong;
private StringHolderToLongMap nonGenericMapStringtoLong;
public Target() {
otherStringLongMap = Maps.newHashMap();
@ -189,19 +189,19 @@ public class Target {
this.enumSet = enumSet;
}
public StringArrayList getNonGenericStringList() {
public StringHolderArrayList getNonGenericStringList() {
return nonGenericStringList;
}
public void setNonGenericStringList(StringArrayList nonGenericStringList) {
public void setNonGenericStringList(StringHolderArrayList nonGenericStringList) {
this.nonGenericStringList = nonGenericStringList;
}
public StringToLongMap getNonGenericMapStringtoLong() {
public StringHolderToLongMap getNonGenericMapStringtoLong() {
return nonGenericMapStringtoLong;
}
public void setNonGenericMapStringtoLong(StringToLongMap nonGenericMapStringtoLong) {
public void setNonGenericMapStringtoLong(StringHolderToLongMap nonGenericMapStringtoLong) {
this.nonGenericMapStringtoLong = nonGenericMapStringtoLong;
}
}