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;
|
package org.mapstruct.ap.internal.model.assignment;
|
||||||
|
|
||||||
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;
|
||||||
@ -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.model.common.Type;
|
||||||
import org.mapstruct.ap.internal.util.Nouns;
|
import org.mapstruct.ap.internal.util.Nouns;
|
||||||
|
|
||||||
|
import static org.mapstruct.ap.internal.util.Collections.first;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps the assignment in a target setter.
|
* Wraps the assignment in a target setter.
|
||||||
*
|
*
|
||||||
@ -35,6 +38,7 @@ import org.mapstruct.ap.internal.util.Nouns;
|
|||||||
public class AdderWrapper extends AssignmentWrapper {
|
public class AdderWrapper extends AssignmentWrapper {
|
||||||
|
|
||||||
private final List<Type> thrownTypesToExclude;
|
private final List<Type> thrownTypesToExclude;
|
||||||
|
private final Type adderType;
|
||||||
|
|
||||||
public AdderWrapper( Assignment rhs,
|
public AdderWrapper( Assignment rhs,
|
||||||
List<Type> thrownTypesToExclude,
|
List<Type> thrownTypesToExclude,
|
||||||
@ -44,6 +48,7 @@ public class AdderWrapper extends AssignmentWrapper {
|
|||||||
this.thrownTypesToExclude = thrownTypesToExclude;
|
this.thrownTypesToExclude = thrownTypesToExclude;
|
||||||
String desiredName = Nouns.singularize( targetPropertyName );
|
String desiredName = Nouns.singularize( targetPropertyName );
|
||||||
rhs.setSourceLocalVarName( rhs.createLocalVarName( desiredName ) );
|
rhs.setSourceLocalVarName( rhs.createLocalVarName( desiredName ) );
|
||||||
|
adderType = first( getSourceType().determineTypeArguments( Collection.class ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,11 +65,15 @@ public class AdderWrapper extends AssignmentWrapper {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Type getAdderType() {
|
||||||
|
return adderType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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( getSourceType().getTypeParameters().get( 0 ).getTypeBound() );
|
imported.add( adderType.getTypeBound() );
|
||||||
return imported;
|
return imported;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,12 +18,15 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.internal.model.common;
|
package org.mapstruct.ap.internal.model.common;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.mapstruct.ap.internal.util.Strings;
|
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.
|
* 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() {
|
public Type getSourceTypeForMatching() {
|
||||||
return useElementAsSourceTypeForMatching && sourceType.isCollectionType() ?
|
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>
|
<#import "../macro/CommonMacros.ftl" as lib>
|
||||||
<@lib.handleExceptions>
|
<@lib.handleExceptions>
|
||||||
if ( ${sourceReference} != null ) {
|
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>;
|
${ext.targetBeanName}.${ext.targetWriteAccessorName}<@lib.handleWrite><@lib.handleAssignment/></@lib.handleWrite>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,4 +31,6 @@ public interface Issue1338Mapper {
|
|||||||
Issue1338Mapper INSTANCE = Mappers.getMapper( Issue1338Mapper.class );
|
Issue1338Mapper INSTANCE = Mappers.getMapper( Issue1338Mapper.class );
|
||||||
|
|
||||||
Target map(Source source);
|
Target map(Source source);
|
||||||
|
|
||||||
|
Source map(Target target);
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,17 @@ public class Issue1338Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCorrectlyUseAdder() {
|
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 )
|
assertThat( target )
|
||||||
.extracting( "properties" )
|
.extracting( "properties" )
|
||||||
.contains( Arrays.asList( "first", "second" ), atIndex( 0 ) );
|
.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;
|
package org.mapstruct.ap.test.bugs._1338;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -25,13 +26,20 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class Source {
|
public class Source {
|
||||||
|
|
||||||
private final List<String> properties;
|
private List<String> properties;
|
||||||
|
|
||||||
public Source(List<String> properties) {
|
public void addProperty(String property) {
|
||||||
this.properties = properties;
|
if ( properties == null ) {
|
||||||
|
properties = new ArrayList<String>();
|
||||||
|
}
|
||||||
|
properties.add( property );
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getProperties() {
|
public List<String> getProperties() {
|
||||||
return properties;
|
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" );
|
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> {
|
public static class StringList extends ArrayList<String> {
|
||||||
|
|
||||||
private StringList() {
|
private StringList() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user