mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#3310 Make sure that adders work properly when they are in a generic class
This commit is contained in:
parent
04434af17a
commit
53c73324ff
@ -958,9 +958,8 @@ public class Type extends ModelElement implements Comparable<Type> {
|
|||||||
List<Accessor> adderList = getAdders();
|
List<Accessor> adderList = getAdders();
|
||||||
List<Accessor> candidateList = new ArrayList<>();
|
List<Accessor> candidateList = new ArrayList<>();
|
||||||
for ( Accessor adder : adderList ) {
|
for ( Accessor adder : adderList ) {
|
||||||
ExecutableElement executable = (ExecutableElement) adder.getElement();
|
TypeMirror adderParameterType = determineTargetType( adder ).getTypeMirror();
|
||||||
VariableElement arg = executable.getParameters().get( 0 );
|
if ( typeUtils.isSameType( boxed( adderParameterType ), boxed( typeArg ) ) ) {
|
||||||
if ( typeUtils.isSameType( boxed( arg.asType() ), boxed( typeArg ) ) ) {
|
|
||||||
candidateList.add( adder );
|
candidateList.add( adder );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._3310;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.mapstruct.CollectionMappingStrategy;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
|
||||||
|
public interface Issue3310Mapper {
|
||||||
|
|
||||||
|
Issue3310Mapper INSTANCE = Mappers.getMapper( Issue3310Mapper.class );
|
||||||
|
|
||||||
|
Target map(Source source);
|
||||||
|
|
||||||
|
abstract class BaseClass<T> {
|
||||||
|
|
||||||
|
private List<T> items;
|
||||||
|
|
||||||
|
public List<T> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setItems(List<T> items) {
|
||||||
|
throw new UnsupportedOperationException( "adder should be used instead" );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addItem(T item) {
|
||||||
|
if ( items == null ) {
|
||||||
|
items = new ArrayList<>();
|
||||||
|
}
|
||||||
|
items.add( item );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Target extends BaseClass<String> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class Source {
|
||||||
|
|
||||||
|
private final List<String> items;
|
||||||
|
|
||||||
|
public Source(List<String> items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getItems() {
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright MapStruct Authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.test.bugs._3310;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("3310")
|
||||||
|
@WithClasses(Issue3310Mapper.class)
|
||||||
|
class Issue3310Test {
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
void shouldUseAdderWithGenericBaseClass() {
|
||||||
|
Issue3310Mapper.Source source = new Issue3310Mapper.Source( Collections.singletonList( "test" ) );
|
||||||
|
Issue3310Mapper.Target target = Issue3310Mapper.INSTANCE.map( source );
|
||||||
|
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getItems() ).containsExactly( "test" );
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user