#2839 Keep thrown types when creating a new ForgedMethod with the same arguments

This fixes a compilation error when mapping fields with the same type due to not wrapping in a `try-catch` block
This commit is contained in:
Hakan 2022-08-20 15:23:32 +02:00 committed by Filip Hrisafov
parent e3f9a1ccd5
commit e5c7fdb2f6
7 changed files with 198 additions and 1 deletions

View File

@ -196,7 +196,7 @@ public class ForgedMethod implements Method {
public ForgedMethod(String name, ForgedMethod forgedMethod) {
this.parameters = forgedMethod.parameters;
this.returnType = forgedMethod.returnType;
this.thrownTypes = new ArrayList<>();
this.thrownTypes = forgedMethod.thrownTypes;
this.history = forgedMethod.history;
this.sourceParameters = Parameter.getSourceParameters( parameters );

View File

@ -0,0 +1,36 @@
/*
* 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._2839;
import java.util.List;
/**
* @author Hakan Özkan
*/
public final class Car {
private final Id id;
private final List<? extends Id> seatIds;
private final List<? extends Id> tireIds;
public Car(Id id, List<? extends Id> seatIds, List<? extends Id> tireIds) {
this.id = id;
this.seatIds = seatIds;
this.tireIds = tireIds;
}
public Id getId() {
return id;
}
public List<? extends Id> getSeatIds() {
return seatIds;
}
public List<? extends Id> getTireIds() {
return tireIds;
}
}

View File

@ -0,0 +1,36 @@
/*
* 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._2839;
import java.util.List;
/**
* @author Hakan Özkan
*/
public final class CarDto {
private final String id;
private final List<String> seatIds;
private final List<String> tireIds;
public CarDto(String id, List<String> seatIds, List<String> tireIds) {
this.id = id;
this.seatIds = seatIds;
this.tireIds = tireIds;
}
public String getId() {
return id;
}
public List<String> getSeatIds() {
return seatIds;
}
public List<String> getTireIds() {
return tireIds;
}
}

View File

@ -0,0 +1,25 @@
/*
* 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._2839;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Hakan Özkan
*/
@Mapper
public abstract class CarMapper {
public static final CarMapper MAPPER = Mappers.getMapper( CarMapper.class );
public abstract Car toEntity(CarDto dto);
protected Id mapId(String id) throws Issue2839Exception {
throw new Issue2839Exception("For id " + id);
}
}

View File

@ -0,0 +1,28 @@
/*
* 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._2839;
import java.util.UUID;
/**
* @author Hakan Özkan
*/
public class Id {
private final UUID id;
public Id() {
this.id = UUID.randomUUID();
}
public Id(UUID id) {
this.id = id;
}
public UUID getId() {
return id;
}
}

View File

@ -0,0 +1,16 @@
/*
* 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._2839;
/**
* @author Hakan Özkan
*/
public class Issue2839Exception extends Exception {
public Issue2839Exception(String message) {
super( message );
}
}

View File

@ -0,0 +1,56 @@
/*
* 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._2839;
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.assertThatThrownBy;
/**
* @author Hakan Özkan
*/
@IssueKey("2839")
@WithClasses({
Car.class,
CarDto.class,
CarMapper.class,
Id.class,
Issue2839Exception.class,
})
public class Issue2839Test {
@ProcessorTest
void shouldCompile() {
CarDto car1 = new CarDto(
"carId",
Collections.singletonList( "seatId" ),
Collections.singletonList( "tireId" )
);
assertThatThrownBy( () -> CarMapper.MAPPER.toEntity( car1 ) )
.isExactlyInstanceOf( RuntimeException.class )
.getCause()
.isInstanceOf( Issue2839Exception.class )
.hasMessage( "For id seatId" );
CarDto car2 = new CarDto( "carId", Collections.emptyList(), Collections.singletonList( "tireId" ) );
assertThatThrownBy( () -> CarMapper.MAPPER.toEntity( car2 ) )
.isExactlyInstanceOf( RuntimeException.class )
.getCause()
.isInstanceOf( Issue2839Exception.class )
.hasMessage( "For id tireId" );
CarDto car3 = new CarDto( "carId", Collections.emptyList(), Collections.emptyList() );
assertThatThrownBy( () -> CarMapper.MAPPER.toEntity( car3 ) )
.isExactlyInstanceOf( RuntimeException.class )
.getCause()
.isInstanceOf( Issue2839Exception.class )
.hasMessage( "For id carId" );
}
}