#3142 Nested forged methods should declare throws from lifecycle methods

This commit is contained in:
Filip Hrisafov 2023-04-13 21:52:15 +02:00 committed by GitHub
parent 2be1f306a1
commit 6e9fa87ba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 211 additions and 0 deletions

View File

@ -340,6 +340,14 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
if ( factoryMethod != null ) {
forgedMethod.addThrownTypes( factoryMethod.getThrownTypes() );
}
for ( LifecycleCallbackMethodReference beforeMappingMethod : beforeMappingMethods ) {
forgedMethod.addThrownTypes( beforeMappingMethod.getThrownTypes() );
}
for ( LifecycleCallbackMethodReference afterMappingMethod : afterMappingMethods ) {
forgedMethod.addThrownTypes( afterMappingMethod.getThrownTypes() );
}
for ( PropertyMapping propertyMapping : propertyMappings ) {
if ( propertyMapping.getAssignment() != null ) {

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._3142;
/**
* @author Filip Hrisafov
*/
public class Issue3142Exception extends Exception {
public Issue3142Exception(String message) {
super( message );
}
}

View File

@ -0,0 +1,48 @@
/*
* 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._3142;
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 Filip Hrisafov
*/
@WithClasses({
Issue3142Exception.class,
Source.class,
Target.class,
})
@IssueKey("3142")
class Issue3142Test {
@ProcessorTest
@WithClasses({
Issue3142WithBeforeMappingExceptionMapper.class
})
void exceptionThrownInBeforeMapping() {
assertThatThrownBy( () -> Issue3142WithBeforeMappingExceptionMapper.INSTANCE.map(
new Source( new Source.Nested( "throwException" ) ), "test" )
)
.isInstanceOf( Issue3142Exception.class )
.hasMessage( "Source nested exception" );
}
@ProcessorTest
@WithClasses({
Issue3142WithAfterMappingExceptionMapper.class
})
void exceptionThrownInAfterMapping() {
assertThatThrownBy( () -> Issue3142WithAfterMappingExceptionMapper.INSTANCE.map(
new Source( new Source.Nested( "throwException" ) ), "test" )
)
.isInstanceOf( Issue3142Exception.class )
.hasMessage( "Source nested exception" );
}
}

View File

@ -0,0 +1,32 @@
/*
* 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._3142;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue3142WithAfterMappingExceptionMapper {
Issue3142WithAfterMappingExceptionMapper INSTANCE =
Mappers.getMapper( Issue3142WithAfterMappingExceptionMapper.class );
Target map(Source source, String id) throws Issue3142Exception;
@AfterMapping
default void afterMappingValidation(Object source) throws Issue3142Exception {
if ( source instanceof Source.Nested ) {
Source.Nested nested = (Source.Nested) source;
if ( "throwException".equals( nested.getValue() ) ) {
throw new Issue3142Exception( "Source nested exception" );
}
}
}
}

View File

@ -0,0 +1,32 @@
/*
* 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._3142;
import org.mapstruct.BeforeMapping;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue3142WithBeforeMappingExceptionMapper {
Issue3142WithBeforeMappingExceptionMapper INSTANCE =
Mappers.getMapper( Issue3142WithBeforeMappingExceptionMapper.class );
Target map(Source source, String id) throws Issue3142Exception;
@BeforeMapping
default void preMappingValidation(Object source) throws Issue3142Exception {
if ( source instanceof Source.Nested ) {
Source.Nested nested = (Source.Nested) source;
if ( "throwException".equals( nested.getValue() ) ) {
throw new Issue3142Exception( "Source nested exception" );
}
}
}
}

View File

@ -0,0 +1,33 @@
/*
* 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._3142;
/**
* @author Filip Hrisafov
*/
public class Source {
private final Nested nested;
public Source(Nested nested) {
this.nested = nested;
}
public Nested getNested() {
return nested;
}
public static class Nested {
private final String value;
public Nested(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}

View File

@ -0,0 +1,42 @@
/*
* 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._3142;
/**
* @author Filip Hrisafov
*/
public class Target {
private String id;
private Nested nested;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Nested getNested() {
return nested;
}
public void setNested(Nested nested) {
this.nested = nested;
}
public static class Nested {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
}