Skip to content

Commit 406ae3f

Browse files
prtnvfiliphr
andauthored
#2891 Fix subclass mapping while superclass has non-empty constructor
Co-authored-by: Filip Hrisafov <filip.hrisafov@gmail.com>
1 parent 98eb46a commit 406ae3f

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,14 @@ else if ( matchingFactoryMethods.size() == 1 ) {
704704
}
705705

706706
private ConstructorAccessor getConstructorAccessor(Type type) {
707+
if ( type.isAbstract() ) {
708+
// We cannot construct abstract classes.
709+
// Usually we won't reach here,
710+
// but if SubclassMapping is used with SubclassExhaustiveStrategy#RUNTIME_EXCEPTION
711+
// then we will still generate the code.
712+
// We shouldn't generate anything for those abstract types
713+
return null;
714+
}
707715

708716
if ( type.isRecord() ) {
709717
// If the type is a record then just get the record components and use then
@@ -1783,7 +1791,7 @@ public boolean hasSubclassMappings() {
17831791
}
17841792

17851793
public boolean isAbstractReturnType() {
1786-
return getFactoryMethod() == null && !hasConstructorMappings() && returnTypeToConstruct != null
1794+
return getFactoryMethod() == null && returnTypeToConstruct != null
17871795
&& returnTypeToConstruct.isAbstract();
17881796
}
17891797

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2891;
7+
8+
import org.mapstruct.BeanMapping;
9+
import org.mapstruct.Mapper;
10+
import org.mapstruct.SubclassExhaustiveStrategy;
11+
import org.mapstruct.SubclassMapping;
12+
13+
/**
14+
* @author Sergei Portnov
15+
*/
16+
@Mapper
17+
public interface Issue2891Mapper {
18+
19+
@BeanMapping(subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION)
20+
@SubclassMapping(source = Source1.class, target = Target1.class)
21+
@SubclassMapping(source = Source2.class, target = Target2.class)
22+
AbstractTarget map(AbstractSource source);
23+
24+
abstract class AbstractTarget {
25+
26+
private final String name;
27+
28+
protected AbstractTarget(String name) {
29+
this.name = name;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
}
36+
37+
class Target1 extends AbstractTarget {
38+
39+
protected Target1(String name) {
40+
super( name );
41+
}
42+
}
43+
44+
class Target2 extends AbstractTarget {
45+
46+
protected Target2(String name) {
47+
super( name );
48+
}
49+
}
50+
51+
abstract class AbstractSource {
52+
53+
private final String name;
54+
55+
protected AbstractSource(String name) {
56+
this.name = name;
57+
}
58+
59+
public String getName() {
60+
return name;
61+
}
62+
}
63+
64+
class Source1 extends AbstractSource {
65+
protected Source1(String name) {
66+
super( name );
67+
}
68+
}
69+
70+
class Source2 extends AbstractSource {
71+
72+
protected Source2(String name) {
73+
super( name );
74+
}
75+
}
76+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2891;
7+
8+
import org.junit.jupiter.api.extension.RegisterExtension;
9+
import org.mapstruct.ap.testutil.IssueKey;
10+
import org.mapstruct.ap.testutil.ProcessorTest;
11+
import org.mapstruct.ap.testutil.WithClasses;
12+
import org.mapstruct.ap.testutil.runner.GeneratedSource;
13+
14+
/**
15+
* @author Sergei Portnov
16+
*/
17+
@WithClasses({
18+
Issue2891Mapper.class
19+
})
20+
@IssueKey("2891")
21+
class Issue2891Test {
22+
23+
@RegisterExtension
24+
final GeneratedSource generatedSource = new GeneratedSource()
25+
.addComparisonToFixtureFor( Issue2891Mapper.class );
26+
27+
@ProcessorTest
28+
void shouldCompile() {
29+
}
30+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2891;
7+
8+
import javax.annotation.processing.Generated;
9+
10+
@Generated(
11+
value = "org.mapstruct.ap.MappingProcessor",
12+
date = "2022-06-18T14:48:32+0300",
13+
comments = "version: , compiler: Eclipse JDT (Batch) 3.20.0.v20191203-2131, environment: Java 11.0.15.1 (BellSoft)"
14+
)
15+
public class Issue2891MapperImpl implements Issue2891Mapper {
16+
17+
@Override
18+
public AbstractTarget map(AbstractSource source) {
19+
if ( source == null ) {
20+
return null;
21+
}
22+
23+
if (source instanceof Source1) {
24+
return source1ToTarget1( (Source1) source );
25+
}
26+
else if (source instanceof Source2) {
27+
return source2ToTarget2( (Source2) source );
28+
}
29+
else {
30+
throw new IllegalArgumentException("Not all subclasses are supported for this mapping. Missing for " + source.getClass());
31+
}
32+
}
33+
34+
protected Target1 source1ToTarget1(Source1 source1) {
35+
if ( source1 == null ) {
36+
return null;
37+
}
38+
39+
String name = null;
40+
41+
name = source1.getName();
42+
43+
Target1 target1 = new Target1( name );
44+
45+
return target1;
46+
}
47+
48+
protected Target2 source2ToTarget2(Source2 source2) {
49+
if ( source2 == null ) {
50+
return null;
51+
}
52+
53+
String name = null;
54+
55+
name = source2.getName();
56+
57+
Target2 target2 = new Target2( name );
58+
59+
return target2;
60+
}
61+
}

0 commit comments

Comments
 (0)