#1453 Make sure that we always forge iterable / map mapping methods without bounds

When generating collection / map mapping methods make sure that the method result type is without bounds
This commit is contained in:
Filip Hrisafov 2018-05-06 22:03:16 +02:00 committed by GitHub
parent 45cc87849b
commit 771debee88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 519 additions and 0 deletions

View File

@ -598,6 +598,7 @@ public class PropertyMapping extends ModelElement {
private Assignment forgeWithElementMapping(Type sourceType, Type targetType, SourceRHS source, private Assignment forgeWithElementMapping(Type sourceType, Type targetType, SourceRHS source,
ExecutableElement element, ContainerMappingMethodBuilder<?, ? extends ContainerMappingMethod> builder) { ExecutableElement element, ContainerMappingMethodBuilder<?, ? extends ContainerMappingMethod> builder) {
targetType = targetType.withoutBounds();
ForgedMethod methodRef = prepareForgedMethod( sourceType, targetType, source, element, "[]" ); ForgedMethod methodRef = prepareForgedMethod( sourceType, targetType, source, element, "[]" );
ContainerMappingMethod iterableMappingMethod = builder ContainerMappingMethod iterableMappingMethod = builder
@ -634,6 +635,7 @@ public class PropertyMapping extends ModelElement {
private Assignment forgeMapMapping(Type sourceType, Type targetType, SourceRHS source, private Assignment forgeMapMapping(Type sourceType, Type targetType, SourceRHS source,
ExecutableElement element) { ExecutableElement element) {
targetType = targetType.withoutBounds();
ForgedMethod methodRef = prepareForgedMethod( sourceType, targetType, source, element, "{}" ); ForgedMethod methodRef = prepareForgedMethod( sourceType, targetType, source, element, "{}" );
MapMappingMethod.Builder builder = new MapMappingMethod.Builder(); MapMappingMethod.Builder builder = new MapMappingMethod.Builder();

View File

@ -406,6 +406,47 @@ public class Type extends ModelElement implements Comparable<Type> {
); );
} }
public Type withoutBounds() {
if ( typeParameters.isEmpty() ) {
return this;
}
List<Type> bounds = new ArrayList<Type>( typeParameters.size() );
List<TypeMirror> mirrors = new ArrayList<TypeMirror>( typeParameters.size() );
for ( Type typeParameter : typeParameters ) {
bounds.add( typeParameter.getTypeBound() );
mirrors.add( typeParameter.getTypeBound().getTypeMirror() );
}
DeclaredType declaredType = typeUtils.getDeclaredType(
typeElement,
mirrors.toArray( new TypeMirror[] {} )
);
return new Type(
typeUtils,
elementUtils,
typeFactory,
accessorNaming,
declaredType,
(TypeElement) declaredType.asElement(),
bounds,
implementationType,
componentType,
builderType == null ? null : builderType.asBuilderInfo(),
packageName,
name,
qualifiedName,
isInterface,
isEnumType,
isIterableType,
isCollectionType,
isMapType,
isStream,
isImported,
isLiteral
);
}
/** /**
* Whether this type is assignable to the given other type. * Whether this type is assignable to the given other type.
* *

View File

@ -0,0 +1,62 @@
/**
* Copyright 2012-2017 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.bugs._1453;
import java.util.List;
import java.util.Map;
/**
* @author Filip Hrisafov
*/
public class Auction {
private final List<Payment> payments;
private final List<Payment> otherPayments;
private Map<Payment, Payment> mapPayments;
private Map<Payment, Payment> mapSuperPayments;
public Auction(List<Payment> payments, List<Payment> otherPayments) {
this.payments = payments;
this.otherPayments = otherPayments;
}
public List<Payment> getPayments() {
return payments;
}
public List<Payment> getOtherPayments() {
return otherPayments;
}
public Map<Payment, Payment> getMapPayments() {
return mapPayments;
}
public void setMapPayments(Map<Payment, Payment> mapPayments) {
this.mapPayments = mapPayments;
}
public Map<Payment, Payment> getMapSuperPayments() {
return mapSuperPayments;
}
public void setMapSuperPayments(Map<Payment, Payment> mapSuperPayments) {
this.mapSuperPayments = mapSuperPayments;
}
}

View File

@ -0,0 +1,66 @@
/**
* Copyright 2012-2017 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.bugs._1453;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Filip Hrisafov
*/
public class AuctionDto {
private List<PaymentDto> payments;
private List<? super PaymentDto> otherPayments;
private Map<PaymentDto, PaymentDto> mapPayments;
private Map<? super PaymentDto, ? super PaymentDto> mapSuperPayments;
List<PaymentDto> takePayments() {
return payments;
}
public void setPayments(List<? extends PaymentDto> payments) {
this.payments = payments == null ? null : new ArrayList<PaymentDto>( payments );
}
List<? super PaymentDto> takeOtherPayments() {
return otherPayments;
}
public void setOtherPayments(List<? super PaymentDto> otherPayments) {
this.otherPayments = otherPayments;
}
public Map<? extends PaymentDto, ? extends PaymentDto> getMapPayments() {
return mapPayments;
}
public void setMapPayments(Map<? extends PaymentDto, ? extends PaymentDto> mapPayments) {
this.mapPayments = mapPayments == null ? null : new HashMap<PaymentDto, PaymentDto>( mapPayments );
}
public Map<? super PaymentDto, ? super PaymentDto> getMapSuperPayments() {
return mapSuperPayments;
}
public void setMapSuperPayments(Map<? super PaymentDto, ? super PaymentDto> mapSuperPayments) {
this.mapSuperPayments = mapSuperPayments;
}
}

View File

@ -0,0 +1,44 @@
/**
* Copyright 2012-2017 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.bugs._1453;
import java.util.List;
import java.util.Map;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue1453Mapper {
Issue1453Mapper INSTANCE = Mappers.getMapper( Issue1453Mapper.class );
AuctionDto map(Auction auction);
List<AuctionDto> mapExtend(List<? extends Auction> auctions);
List<? super AuctionDto> mapSuper(List<Auction> auctions);
Map<AuctionDto, AuctionDto> mapExtend(Map<? extends Auction, ? extends Auction> auctions);
Map<? super AuctionDto, ? super AuctionDto> mapSuper(Map<Auction, Auction> auctions);
}

View File

@ -0,0 +1,82 @@
/**
* Copyright 2012-2017 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.bugs._1453;
import java.util.Arrays;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import org.mapstruct.ap.testutil.runner.GeneratedSource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@IssueKey("1453")
@RunWith(AnnotationProcessorTestRunner.class)
@WithClasses({
Auction.class,
AuctionDto.class,
Issue1453Mapper.class,
Payment.class,
PaymentDto.class
})
public class Issue1453Test {
@Rule
public GeneratedSource source = new GeneratedSource().addComparisonToFixtureFor( Issue1453Mapper.class );
@Test
public void shouldGenerateCorrectCode() {
AuctionDto target = Issue1453Mapper.INSTANCE.map( new Auction(
Arrays.asList( new Payment( 100L ), new Payment( 500L ) ),
Arrays.asList( new Payment( 200L ), new Payment( 600L ) )
) );
PaymentDto first = new PaymentDto();
first.setPrice( 100L );
PaymentDto second = new PaymentDto();
second.setPrice( 500L );
assertThat( target.takePayments() )
.usingFieldByFieldElementComparator()
.containsExactly(
first,
second
);
PaymentDto firstOther = new PaymentDto();
firstOther.setPrice( 200L );
PaymentDto secondOther = new PaymentDto();
secondOther.setPrice( 600L );
assertThat( target.takeOtherPayments() )
.usingFieldByFieldElementComparator()
.containsExactly(
firstOther,
secondOther
);
}
}

View File

@ -0,0 +1,35 @@
/**
* Copyright 2012-2017 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.bugs._1453;
/**
* @author Filip Hrisafov
*/
public class Payment {
private final Long price;
public Payment(Long price) {
this.price = price;
}
public Long getPrice() {
return price;
}
}

View File

@ -0,0 +1,35 @@
/**
* Copyright 2012-2017 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.bugs._1453;
/**
* @author Filip Hrisafov
*/
public class PaymentDto {
private Long price;
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
}

View File

@ -0,0 +1,152 @@
/**
* Copyright 2012-2017 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.bugs._1453;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2018-05-04T23:25:01+0200",
comments = "version: , compiler: javac, environment: Java 1.8.0_161 (Oracle Corporation)"
)
public class Issue1453MapperImpl implements Issue1453Mapper {
@Override
public AuctionDto map(Auction auction) {
if ( auction == null ) {
return null;
}
AuctionDto auctionDto = new AuctionDto();
auctionDto.setPayments( paymentListToPaymentDtoList( auction.getPayments() ) );
auctionDto.setOtherPayments( paymentListToPaymentDtoList( auction.getOtherPayments() ) );
auctionDto.setMapPayments( paymentPaymentMapToPaymentDtoPaymentDtoMap( auction.getMapPayments() ) );
auctionDto.setMapSuperPayments( paymentPaymentMapToPaymentDtoPaymentDtoMap( auction.getMapSuperPayments() ) );
return auctionDto;
}
@Override
public List<AuctionDto> mapExtend(List<? extends Auction> auctions) {
if ( auctions == null ) {
return null;
}
List<AuctionDto> list = new ArrayList<AuctionDto>( auctions.size() );
for ( Auction auction : auctions ) {
list.add( map( auction ) );
}
return list;
}
@Override
public List<? super AuctionDto> mapSuper(List<Auction> auctions) {
if ( auctions == null ) {
return null;
}
List<? super AuctionDto> list = new ArrayList<AuctionDto>( auctions.size() );
for ( Auction auction : auctions ) {
list.add( map( auction ) );
}
return list;
}
@Override
public Map<AuctionDto, AuctionDto> mapExtend(Map<? extends Auction, ? extends Auction> auctions) {
if ( auctions == null ) {
return null;
}
Map<AuctionDto, AuctionDto> map = new HashMap<AuctionDto, AuctionDto>( Math.max( (int) ( auctions.size() / .75f ) + 1, 16 ) );
for ( java.util.Map.Entry<? extends Auction, ? extends Auction> entry : auctions.entrySet() ) {
AuctionDto key = map( entry.getKey() );
AuctionDto value = map( entry.getValue() );
map.put( key, value );
}
return map;
}
@Override
public Map<? super AuctionDto, ? super AuctionDto> mapSuper(Map<Auction, Auction> auctions) {
if ( auctions == null ) {
return null;
}
Map<? super AuctionDto, ? super AuctionDto> map = new HashMap<AuctionDto, AuctionDto>( Math.max( (int) ( auctions.size() / .75f ) + 1, 16 ) );
for ( java.util.Map.Entry<Auction, Auction> entry : auctions.entrySet() ) {
AuctionDto key = map( entry.getKey() );
AuctionDto value = map( entry.getValue() );
map.put( key, value );
}
return map;
}
protected PaymentDto paymentToPaymentDto(Payment payment) {
if ( payment == null ) {
return null;
}
PaymentDto paymentDto = new PaymentDto();
paymentDto.setPrice( payment.getPrice() );
return paymentDto;
}
protected List<PaymentDto> paymentListToPaymentDtoList(List<Payment> list) {
if ( list == null ) {
return null;
}
List<PaymentDto> list1 = new ArrayList<PaymentDto>( list.size() );
for ( Payment payment : list ) {
list1.add( paymentToPaymentDto( payment ) );
}
return list1;
}
protected Map<PaymentDto, PaymentDto> paymentPaymentMapToPaymentDtoPaymentDtoMap(Map<Payment, Payment> map) {
if ( map == null ) {
return null;
}
Map<PaymentDto, PaymentDto> map1 = new HashMap<PaymentDto, PaymentDto>( Math.max( (int) ( map.size() / .75f ) + 1, 16 ) );
for ( java.util.Map.Entry<Payment, Payment> entry : map.entrySet() ) {
PaymentDto key = paymentToPaymentDto( entry.getKey() );
PaymentDto value = paymentToPaymentDto( entry.getValue() );
map1.put( key, value );
}
return map1;
}
}