mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#919 Fix compilation error in map-mapping methods with non-generic maps as source
This commit is contained in:
parent
4ab3c2d537
commit
d94d5857ce
@ -52,7 +52,7 @@
|
|||||||
</#if>
|
</#if>
|
||||||
</#list>
|
</#list>
|
||||||
<#-- Once #148 has been addressed, the simple name of Map.Entry can be used -->
|
<#-- 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 -->
|
<#-- key -->
|
||||||
<@includeModel object=keyAssignment
|
<@includeModel object=keyAssignment
|
||||||
targetWriteAccessorName=keyVariableName
|
targetWriteAccessorName=keyVariableName
|
||||||
|
@ -37,7 +37,9 @@ import org.mapstruct.ap.testutil.WithClasses;
|
|||||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
|
|
||||||
@WithClasses({ Source.class, Target.class, Colour.class, SourceTargetMapper.class, TestList.class, TestMap.class,
|
@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)
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
public class CollectionMappingTest {
|
public class CollectionMappingTest {
|
||||||
|
|
||||||
@ -401,12 +403,14 @@ public class CollectionMappingTest {
|
|||||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||||
|
|
||||||
assertThat( target ).isNotNull();
|
assertThat( target ).isNotNull();
|
||||||
assertThat( target.getNonGenericStringList() ).containsExactly( "Bob", "Alice" );
|
assertThat( target.getNonGenericStringList() ).containsExactly(
|
||||||
|
new StringHolder( "Bob" ),
|
||||||
|
new StringHolder( "Alice" ) );
|
||||||
|
|
||||||
// Inverse direction
|
// Inverse direction
|
||||||
Target newTarget = new Target();
|
Target newTarget = new Target();
|
||||||
StringArrayList nonGenericStringList = new StringArrayList();
|
StringHolderArrayList nonGenericStringList = new StringHolderArrayList();
|
||||||
nonGenericStringList.addAll( Arrays.asList( "Bill", "Bob" ) );
|
nonGenericStringList.addAll( Arrays.asList( new StringHolder( "Bill" ), new StringHolder( "Bob" ) ) );
|
||||||
newTarget.setNonGenericStringList( nonGenericStringList );
|
newTarget.setNonGenericStringList( nonGenericStringList );
|
||||||
|
|
||||||
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( newTarget );
|
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( newTarget );
|
||||||
@ -415,6 +419,7 @@ public class CollectionMappingTest {
|
|||||||
assertThat( mappedSource.getStringList3() ).containsExactly( "Bill", "Bob" );
|
assertThat( mappedSource.getStringList3() ).containsExactly( "Bill", "Bob" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
@IssueKey("853")
|
@IssueKey("853")
|
||||||
public void shouldMapNonGenericMap() {
|
public void shouldMapNonGenericMap() {
|
||||||
@ -427,13 +432,15 @@ public class CollectionMappingTest {
|
|||||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||||
|
|
||||||
assertThat( target ).isNotNull();
|
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
|
// Inverse direction
|
||||||
Target newTarget = new Target();
|
Target newTarget = new Target();
|
||||||
StringToLongMap stringToLongMap = new StringToLongMap();
|
StringHolderToLongMap stringToLongMap = new StringHolderToLongMap();
|
||||||
stringToLongMap.put( "Blue", 321L );
|
stringToLongMap.put( new StringHolder( "Blue" ), 321L );
|
||||||
stringToLongMap.put( "Green", 654L );
|
stringToLongMap.put( new StringHolder( "Green" ), 654L );
|
||||||
newTarget.setNonGenericMapStringtoLong( stringToLongMap );
|
newTarget.setNonGenericMapStringtoLong( stringToLongMap );
|
||||||
|
|
||||||
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( newTarget );
|
Source mappedSource = SourceTargetMapper.INSTANCE.targetToSource( newTarget );
|
||||||
|
@ -29,9 +29,9 @@ import org.mapstruct.Mappings;
|
|||||||
import org.mapstruct.factory.Mappers;
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface SourceTargetMapper {
|
public abstract class SourceTargetMapper {
|
||||||
|
|
||||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
static final SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||||
|
|
||||||
@Mappings({
|
@Mappings({
|
||||||
@Mapping(source = "integerList", target = "integerCollection"),
|
@Mapping(source = "integerList", target = "integerCollection"),
|
||||||
@ -42,23 +42,31 @@ public interface SourceTargetMapper {
|
|||||||
@Mapping(source = "stringList3", target = "nonGenericStringList"),
|
@Mapping(source = "stringList3", target = "nonGenericStringList"),
|
||||||
@Mapping(source = "stringLongMapForNonGeneric", target = "nonGenericMapStringtoLong")
|
@Mapping(source = "stringLongMapForNonGeneric", target = "nonGenericMapStringtoLong")
|
||||||
})
|
})
|
||||||
Target sourceToTarget(Source source);
|
public abstract Target sourceToTarget(Source source);
|
||||||
|
|
||||||
@InheritInverseConfiguration( name = "sourceToTarget" )
|
@InheritInverseConfiguration( name = "sourceToTarget" )
|
||||||
Source targetToSource(Target target);
|
public abstract Source targetToSource(Target target);
|
||||||
|
|
||||||
@InheritConfiguration
|
@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
|
@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
|
@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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -23,7 +23,7 @@ import java.util.ArrayList;
|
|||||||
/**
|
/**
|
||||||
* @author Stefan May
|
* @author Stefan May
|
||||||
*/
|
*/
|
||||||
public class StringArrayList extends ArrayList<String> {
|
public class StringHolderArrayList extends ArrayList<StringHolder> {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
}
|
}
|
@ -23,7 +23,7 @@ import java.util.HashMap;
|
|||||||
/**
|
/**
|
||||||
* @author Stefan May
|
* @author Stefan May
|
||||||
*/
|
*/
|
||||||
public class StringToLongMap extends HashMap<String, Long> {
|
public class StringHolderToLongMap extends HashMap<StringHolder, Long> {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
@ -58,9 +58,9 @@ public class Target {
|
|||||||
|
|
||||||
private EnumSet<Colour> enumSet;
|
private EnumSet<Colour> enumSet;
|
||||||
|
|
||||||
private StringArrayList nonGenericStringList;
|
private StringHolderArrayList nonGenericStringList;
|
||||||
|
|
||||||
private StringToLongMap nonGenericMapStringtoLong;
|
private StringHolderToLongMap nonGenericMapStringtoLong;
|
||||||
|
|
||||||
public Target() {
|
public Target() {
|
||||||
otherStringLongMap = Maps.newHashMap();
|
otherStringLongMap = Maps.newHashMap();
|
||||||
@ -189,19 +189,19 @@ public class Target {
|
|||||||
this.enumSet = enumSet;
|
this.enumSet = enumSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringArrayList getNonGenericStringList() {
|
public StringHolderArrayList getNonGenericStringList() {
|
||||||
return nonGenericStringList;
|
return nonGenericStringList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNonGenericStringList(StringArrayList nonGenericStringList) {
|
public void setNonGenericStringList(StringHolderArrayList nonGenericStringList) {
|
||||||
this.nonGenericStringList = nonGenericStringList;
|
this.nonGenericStringList = nonGenericStringList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringToLongMap getNonGenericMapStringtoLong() {
|
public StringHolderToLongMap getNonGenericMapStringtoLong() {
|
||||||
return nonGenericMapStringtoLong;
|
return nonGenericMapStringtoLong;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNonGenericMapStringtoLong(StringToLongMap nonGenericMapStringtoLong) {
|
public void setNonGenericMapStringtoLong(StringHolderToLongMap nonGenericMapStringtoLong) {
|
||||||
this.nonGenericMapStringtoLong = nonGenericMapStringtoLong;
|
this.nonGenericMapStringtoLong = nonGenericMapStringtoLong;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user