#3310 Make sure that adders work properly when they are in a generic class

This commit is contained in:
Filip Hrisafov 2023-07-08 17:45:00 +02:00
parent 04434af17a
commit 53c73324ff
3 changed files with 94 additions and 3 deletions

View File

@ -958,9 +958,8 @@ public class Type extends ModelElement implements Comparable<Type> {
List<Accessor> adderList = getAdders();
List<Accessor> candidateList = new ArrayList<>();
for ( Accessor adder : adderList ) {
ExecutableElement executable = (ExecutableElement) adder.getElement();
VariableElement arg = executable.getParameters().get( 0 );
if ( typeUtils.isSameType( boxed( arg.asType() ), boxed( typeArg ) ) ) {
TypeMirror adderParameterType = determineTargetType( adder ).getTypeMirror();
if ( typeUtils.isSameType( boxed( adderParameterType ), boxed( typeArg ) ) ) {
candidateList.add( adder );
}
}

View File

@ -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;
}
}
}

View File

@ -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" );
}
}