mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2825 Fix SubclassMapping stackoverflow exception
This commit is contained in:
parent
ac356cab25
commit
3cc2aa7675
@ -196,7 +196,7 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
|
|
||||||
this.mappingMethod = mappingMethod;
|
this.mappingMethod = mappingMethod;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.methods = filterPossibleCandidateMethods( sourceModel );
|
this.methods = filterPossibleCandidateMethods( sourceModel, mappingMethod );
|
||||||
this.formattingParameters =
|
this.formattingParameters =
|
||||||
formattingParameters == null ? FormattingParameters.EMPTY : formattingParameters;
|
formattingParameters == null ? FormattingParameters.EMPTY : formattingParameters;
|
||||||
this.sourceRHS = sourceRHS;
|
this.sourceRHS = sourceRHS;
|
||||||
@ -210,10 +210,10 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
}
|
}
|
||||||
// CHECKSTYLE:ON
|
// CHECKSTYLE:ON
|
||||||
|
|
||||||
private <T extends Method> List<T> filterPossibleCandidateMethods(List<T> candidateMethods) {
|
private <T extends Method> List<T> filterPossibleCandidateMethods(List<T> candidateMethods, T mappingMethod) {
|
||||||
List<T> result = new ArrayList<>( candidateMethods.size() );
|
List<T> result = new ArrayList<>( candidateMethods.size() );
|
||||||
for ( T candidate : candidateMethods ) {
|
for ( T candidate : candidateMethods ) {
|
||||||
if ( isCandidateForMapping( candidate ) ) {
|
if ( isCandidateForMapping( candidate ) && !candidate.equals( mappingMethod )) {
|
||||||
result.add( candidate );
|
result.add( candidate );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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._2825;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author orange add
|
||||||
|
*/
|
||||||
|
public class Animal {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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._2825;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author orange add
|
||||||
|
*/
|
||||||
|
public class Cat extends Animal {
|
||||||
|
private String race;
|
||||||
|
|
||||||
|
public String getRace() {
|
||||||
|
return race;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRace(String race) {
|
||||||
|
this.race = race;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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._2825;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author orange add
|
||||||
|
*/
|
||||||
|
public class Dog extends Animal {
|
||||||
|
private String race;
|
||||||
|
|
||||||
|
public String getRace() {
|
||||||
|
return race;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRace(String race) {
|
||||||
|
this.race = race;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* 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._2825;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.ReportingPolicy;
|
||||||
|
import org.mapstruct.SubclassMapping;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author orange add
|
||||||
|
*/
|
||||||
|
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
|
||||||
|
public interface Issue2825Mapper {
|
||||||
|
|
||||||
|
Issue2825Mapper INSTANCE = Mappers.getMapper( Issue2825Mapper.class );
|
||||||
|
|
||||||
|
@SubclassMapping(target = TargetAnimal.class, source = Dog.class)
|
||||||
|
@SubclassMapping(target = TargetAnimal.class, source = Cat.class)
|
||||||
|
TargetAnimal map(Animal source);
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* 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._2825;
|
||||||
|
|
||||||
|
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 orange add
|
||||||
|
*/
|
||||||
|
@IssueKey("2825")
|
||||||
|
@WithClasses({
|
||||||
|
Animal.class,
|
||||||
|
Cat.class,
|
||||||
|
Dog.class,
|
||||||
|
Issue2825Mapper.class,
|
||||||
|
TargetAnimal.class,
|
||||||
|
})
|
||||||
|
public class Issue2825Test {
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void mappingMethodShouldNotBeReusedForSubclassMappings() {
|
||||||
|
Dog dog = new Dog();
|
||||||
|
dog.setName( "Lucky" );
|
||||||
|
dog.setRace( "Shepherd" );
|
||||||
|
TargetAnimal target = Issue2825Mapper.INSTANCE.map( dog );
|
||||||
|
assertThat( target.getName() ).isEqualTo( "Lucky" );
|
||||||
|
assertThat( target.getRace() ).isEqualTo( "Shepherd" );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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._2825;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author orange add
|
||||||
|
*/
|
||||||
|
public class TargetAnimal {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private String race;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRace() {
|
||||||
|
return race;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRace(String race) {
|
||||||
|
this.race = race;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user