mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1338 Using an adder with non generic source collection should work
This commit is contained in:
parent
7a3f6d973e
commit
bf31ec72de
@ -19,6 +19,7 @@
|
||||
package org.mapstruct.ap.internal.model.assignment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@ -27,6 +28,8 @@ import org.mapstruct.ap.internal.model.common.Assignment;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.util.Nouns;
|
||||
|
||||
import static org.mapstruct.ap.internal.util.Collections.first;
|
||||
|
||||
/**
|
||||
* Wraps the assignment in a target setter.
|
||||
*
|
||||
@ -35,6 +38,7 @@ import org.mapstruct.ap.internal.util.Nouns;
|
||||
public class AdderWrapper extends AssignmentWrapper {
|
||||
|
||||
private final List<Type> thrownTypesToExclude;
|
||||
private final Type adderType;
|
||||
|
||||
public AdderWrapper( Assignment rhs,
|
||||
List<Type> thrownTypesToExclude,
|
||||
@ -44,6 +48,7 @@ public class AdderWrapper extends AssignmentWrapper {
|
||||
this.thrownTypesToExclude = thrownTypesToExclude;
|
||||
String desiredName = Nouns.singularize( targetPropertyName );
|
||||
rhs.setSourceLocalVarName( rhs.createLocalVarName( desiredName ) );
|
||||
adderType = first( getSourceType().determineTypeArguments( Collection.class ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -60,11 +65,15 @@ public class AdderWrapper extends AssignmentWrapper {
|
||||
return result;
|
||||
}
|
||||
|
||||
public Type getAdderType() {
|
||||
return adderType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Type> getImportTypes() {
|
||||
Set<Type> imported = new HashSet<Type>();
|
||||
imported.addAll( super.getImportTypes() );
|
||||
imported.add( getSourceType().getTypeParameters().get( 0 ).getTypeBound() );
|
||||
imported.add( adderType.getTypeBound() );
|
||||
return imported;
|
||||
}
|
||||
|
||||
|
@ -18,12 +18,15 @@
|
||||
*/
|
||||
package org.mapstruct.ap.internal.model.common;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.util.Strings;
|
||||
|
||||
import static org.mapstruct.ap.internal.util.Collections.first;
|
||||
|
||||
/**
|
||||
* SourceRHS Assignment. Right Hand Side (RHS), source part of the assignment.
|
||||
*
|
||||
@ -135,7 +138,7 @@ public class SourceRHS extends ModelElement implements Assignment {
|
||||
*/
|
||||
public Type getSourceTypeForMatching() {
|
||||
return useElementAsSourceTypeForMatching && sourceType.isCollectionType() ?
|
||||
sourceType.getTypeParameters().get( 0 ) : sourceType;
|
||||
first( sourceType.determineTypeArguments( Collection.class ) ) : sourceType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,7 +22,7 @@
|
||||
<#import "../macro/CommonMacros.ftl" as lib>
|
||||
<@lib.handleExceptions>
|
||||
if ( ${sourceReference} != null ) {
|
||||
for ( <@includeModel object=sourceType.typeParameters[0].typeBound/> ${sourceLocalVarName} : ${sourceReference} ) {
|
||||
for ( <@includeModel object=adderType.typeBound/> ${sourceLocalVarName} : ${sourceReference} ) {
|
||||
${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWrite><@lib.handleAssignment/></@lib.handleWrite>;
|
||||
}
|
||||
}
|
||||
|
@ -31,4 +31,6 @@ public interface Issue1338Mapper {
|
||||
Issue1338Mapper INSTANCE = Mappers.getMapper( Issue1338Mapper.class );
|
||||
|
||||
Target map(Source source);
|
||||
|
||||
Source map(Target target);
|
||||
}
|
||||
|
@ -43,11 +43,17 @@ public class Issue1338Test {
|
||||
|
||||
@Test
|
||||
public void shouldCorrectlyUseAdder() {
|
||||
Target target = Issue1338Mapper.INSTANCE.map( new Source( Arrays.asList( "first", "second" ) ) );
|
||||
Source source = new Source();
|
||||
source.setProperties( Arrays.asList( "first", "second" ) );
|
||||
Target target = Issue1338Mapper.INSTANCE.map( source );
|
||||
|
||||
assertThat( target )
|
||||
.extracting( "properties" )
|
||||
.contains( Arrays.asList( "first", "second" ), atIndex( 0 ) );
|
||||
|
||||
Source mapped = Issue1338Mapper.INSTANCE.map( target );
|
||||
|
||||
assertThat( mapped.getProperties() )
|
||||
.containsExactly( "first", "second" );
|
||||
}
|
||||
}
|
||||
|
@ -18,6 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.bugs._1338;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@ -25,13 +26,20 @@ import java.util.List;
|
||||
*/
|
||||
public class Source {
|
||||
|
||||
private final List<String> properties;
|
||||
private List<String> properties;
|
||||
|
||||
public Source(List<String> properties) {
|
||||
this.properties = properties;
|
||||
public void addProperty(String property) {
|
||||
if ( properties == null ) {
|
||||
properties = new ArrayList<String>();
|
||||
}
|
||||
properties.add( property );
|
||||
}
|
||||
|
||||
public List<String> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setProperties(List<String> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,10 @@ public class Target {
|
||||
throw new IllegalStateException( "Setter is there just as a marker it should not be used" );
|
||||
}
|
||||
|
||||
public StringList getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public static class StringList extends ArrayList<String> {
|
||||
|
||||
private StringList() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user