Skip to content

Commit 6c1108d

Browse files
authored
mapstruct#1698 Skip "java.lang.Object" as intermediate result additional unit test (mapstruct#1737)
1 parent 51bd43f commit 6c1108d

File tree

6 files changed

+126
-22
lines changed

6 files changed

+126
-22
lines changed

processor/src/main/java/org/mapstruct/ap/internal/processor/creation/MappingResolverImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ private Assignment resolveViaMethodAndMethod(Type sourceType, Type targetType) {
378378
* <li>there is a method from B to C, methodY</li>
379379
* </ul>
380380
* then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) )
381+
*
382+
* In stead of directly using a built in method candidate all the return types as 'B' of all available built-in
383+
* methods are used to resolve a mapping (assignment) from result type to 'B'. If a match is found, an attempt
384+
* is done to find a matching type conversion.
381385
*/
382386
private Assignment resolveViaConversionAndMethod(Type sourceType, Type targetType) {
383387

@@ -421,7 +425,11 @@ private Assignment resolveViaConversionAndMethod(Type sourceType, Type targetTyp
421425
* <li>there is a conversion from A to B, conversionX</li>
422426
* <li>there is a method from B to C, methodY</li>
423427
* </ul>
424-
* then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) )
428+
* then this method tries to resolve this combination and make a mapping conversionY( methodX ( parameter ) )
429+
*
430+
* In stead of directly using a built in method candidate all the return types as 'B' of all available built-in
431+
* methods are used to resolve a mapping (assignment) from source type to 'B'. If a match is found, an attempt
432+
* is done to find a matching type conversion.
425433
*/
426434
private ConversionAssignment resolveViaMethodAndConversion(Type sourceType, Type targetType) {
427435

processor/src/test/java/org/mapstruct/ap/test/builtin/BuiltInTest.java

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
*/
66
package org.mapstruct.ap.test.builtin;
77

8-
import static org.assertj.core.api.Assertions.assertThat;
9-
8+
import java.math.BigDecimal;
109
import java.text.ParseException;
1110
import java.text.SimpleDateFormat;
1211
import java.time.ZoneId;
@@ -19,7 +18,6 @@
1918
import java.util.HashMap;
2019
import java.util.List;
2120
import java.util.TimeZone;
22-
2321
import javax.xml.bind.JAXBElement;
2422
import javax.xml.datatype.DatatypeConfigurationException;
2523
import javax.xml.datatype.DatatypeFactory;
@@ -34,10 +32,13 @@
3432
import org.junit.runners.MethodSorters;
3533
import org.mapstruct.ap.test.builtin._target.IterableTarget;
3634
import org.mapstruct.ap.test.builtin._target.MapTarget;
35+
import org.mapstruct.ap.test.builtin.bean.BigDecimalProperty;
3736
import org.mapstruct.ap.test.builtin.bean.CalendarProperty;
3837
import org.mapstruct.ap.test.builtin.bean.DateProperty;
3938
import org.mapstruct.ap.test.builtin.bean.JaxbElementListProperty;
4039
import org.mapstruct.ap.test.builtin.bean.JaxbElementProperty;
40+
import org.mapstruct.ap.test.builtin.bean.SomeType;
41+
import org.mapstruct.ap.test.builtin.bean.SomeTypeProperty;
4142
import org.mapstruct.ap.test.builtin.bean.StringListProperty;
4243
import org.mapstruct.ap.test.builtin.bean.StringProperty;
4344
import org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarProperty;
@@ -64,6 +65,8 @@
6465
import org.mapstruct.ap.testutil.WithClasses;
6566
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
6667

68+
import static org.assertj.core.api.Assertions.assertThat;
69+
6770
/**
6871
* Test for the generation of built-in mapping methods.
6972
*
@@ -78,24 +81,11 @@
7881
JaxbElementProperty.class,
7982
StringListProperty.class,
8083
StringProperty.class,
84+
BigDecimalProperty.class,
85+
SomeTypeProperty.class,
86+
SomeType.class,
8187
XmlGregorianCalendarProperty.class,
8288
ZonedDateTimeProperty.class,
83-
CalendarToZonedDateTimeMapper.class,
84-
ZonedDateTimeToCalendarMapper.class,
85-
CalendarToDateMapper.class,
86-
CalendarToStringMapper.class,
87-
CalendarToXmlGregCalMapper.class,
88-
DateToCalendarMapper.class,
89-
DateToXmlGregCalMapper.class,
90-
IterableSourceTargetMapper.class,
91-
JaxbListMapper.class,
92-
JaxbMapper.class,
93-
MapSourceTargetMapper.class,
94-
StringToCalendarMapper.class,
95-
StringToXmlGregCalMapper.class,
96-
XmlGregCalToCalendarMapper.class,
97-
XmlGregCalToDateMapper.class,
98-
XmlGregCalToStringMapper.class,
9989
IterableSource.class,
10090
MapSource.class
10191
})
@@ -117,8 +107,8 @@ public static void restoreOriginalTimeZone() {
117107
}
118108

119109
@Test
120-
public void shouldApplyBuiltInOnJAXBElement() throws ParseException, DatatypeConfigurationException {
121-
110+
@WithClasses( JaxbMapper.class )
111+
public void shouldApplyBuiltInOnJAXBElement() {
122112
JaxbElementProperty source = new JaxbElementProperty();
123113
source.setProp( createJaxb( "TEST" ) );
124114
source.publicProp = createJaxb( "PUBLIC TEST" );
@@ -130,6 +120,30 @@ public void shouldApplyBuiltInOnJAXBElement() throws ParseException, DatatypeCon
130120
}
131121

132122
@Test
123+
@WithClasses( JaxbMapper.class )
124+
@IssueKey( "1698" )
125+
public void shouldApplyBuiltInOnJAXBElementExtra() {
126+
JaxbElementProperty source = new JaxbElementProperty();
127+
source.setProp( createJaxb( "5" ) );
128+
source.publicProp = createJaxb( "5" );
129+
130+
BigDecimalProperty target = JaxbMapper.INSTANCE.mapBD( source );
131+
assertThat( target ).isNotNull();
132+
assertThat( target.getProp() ).isEqualTo( new BigDecimal( "5" ) );
133+
assertThat( target.publicProp ).isEqualTo( new BigDecimal( "5" ) );
134+
135+
JaxbElementProperty source2 = new JaxbElementProperty();
136+
source2.setProp( createJaxb( "5" ) );
137+
source2.publicProp = createJaxb( "5" );
138+
139+
SomeTypeProperty target2 = JaxbMapper.INSTANCE.mapSomeType( source2 );
140+
assertThat( target2 ).isNotNull();
141+
assertThat( target2.publicProp ).isNotNull();
142+
assertThat( target2.getProp() ).isNotNull();
143+
}
144+
145+
@Test
146+
@WithClasses( JaxbListMapper.class )
133147
@IssueKey( "141" )
134148
public void shouldApplyBuiltInOnJAXBElementList() throws ParseException, DatatypeConfigurationException {
135149

@@ -144,6 +158,7 @@ public void shouldApplyBuiltInOnJAXBElementList() throws ParseException, Datatyp
144158
}
145159

146160
@Test
161+
@WithClasses( DateToXmlGregCalMapper.class )
147162
public void shouldApplyBuiltInOnDateToXmlGregCal() throws ParseException, DatatypeConfigurationException {
148163

149164
DateProperty source = new DateProperty();
@@ -159,6 +174,7 @@ public void shouldApplyBuiltInOnDateToXmlGregCal() throws ParseException, Dataty
159174
}
160175

161176
@Test
177+
@WithClasses( XmlGregCalToDateMapper.class )
162178
public void shouldApplyBuiltInOnXmlGregCalToDate() throws ParseException, DatatypeConfigurationException {
163179

164180
XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty();
@@ -175,6 +191,7 @@ public void shouldApplyBuiltInOnXmlGregCalToDate() throws ParseException, Dataty
175191
}
176192

177193
@Test
194+
@WithClasses( StringToXmlGregCalMapper.class )
178195
public void shouldApplyBuiltInStringToXmlGregCal() throws ParseException, DatatypeConfigurationException {
179196

180197
StringProperty source = new StringProperty();
@@ -209,6 +226,7 @@ public void shouldApplyBuiltInStringToXmlGregCal() throws ParseException, Dataty
209226
}
210227

211228
@Test
229+
@WithClasses( XmlGregCalToStringMapper.class )
212230
public void shouldApplyBuiltInXmlGregCalToString() throws ParseException, DatatypeConfigurationException {
213231

214232
XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty();
@@ -235,6 +253,7 @@ public void shouldApplyBuiltInXmlGregCalToString() throws ParseException, Dataty
235253
}
236254

237255
@Test
256+
@WithClasses( CalendarToXmlGregCalMapper.class )
238257
public void shouldApplyBuiltInOnCalendarToXmlGregCal() throws ParseException, DatatypeConfigurationException {
239258

240259
CalendarProperty source = new CalendarProperty();
@@ -250,6 +269,7 @@ public void shouldApplyBuiltInOnCalendarToXmlGregCal() throws ParseException, Da
250269
}
251270

252271
@Test
272+
@WithClasses( XmlGregCalToCalendarMapper.class )
253273
public void shouldApplyBuiltInOnXmlGregCalToCalendar() throws ParseException, DatatypeConfigurationException {
254274

255275
XmlGregorianCalendarProperty source = new XmlGregorianCalendarProperty();
@@ -267,6 +287,7 @@ public void shouldApplyBuiltInOnXmlGregCalToCalendar() throws ParseException, Da
267287
}
268288

269289
@Test
290+
@WithClasses( CalendarToDateMapper.class )
270291
public void shouldApplyBuiltInOnCalendarToDate() throws ParseException, DatatypeConfigurationException {
271292

272293
CalendarProperty source = new CalendarProperty();
@@ -282,6 +303,7 @@ public void shouldApplyBuiltInOnCalendarToDate() throws ParseException, Datatype
282303
}
283304

284305
@Test
306+
@WithClasses( DateToCalendarMapper.class )
285307
public void shouldApplyBuiltInOnDateToCalendar() throws ParseException, DatatypeConfigurationException {
286308

287309
DateProperty source = new DateProperty();
@@ -298,6 +320,7 @@ public void shouldApplyBuiltInOnDateToCalendar() throws ParseException, Datatype
298320
}
299321

300322
@Test
323+
@WithClasses( CalendarToStringMapper.class )
301324
public void shouldApplyBuiltInOnCalendarToString() throws ParseException, DatatypeConfigurationException {
302325

303326
CalendarProperty source = new CalendarProperty();
@@ -313,6 +336,7 @@ public void shouldApplyBuiltInOnCalendarToString() throws ParseException, Dataty
313336
}
314337

315338
@Test
339+
@WithClasses( StringToCalendarMapper.class )
316340
public void shouldApplyBuiltInOnStringToCalendar() throws ParseException, DatatypeConfigurationException {
317341

318342
StringProperty source = new StringProperty();
@@ -329,6 +353,7 @@ public void shouldApplyBuiltInOnStringToCalendar() throws ParseException, Dataty
329353
}
330354

331355
@Test
356+
@WithClasses( IterableSourceTargetMapper.class )
332357
public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfigurationException {
333358

334359
IterableSource source = new IterableSource();
@@ -342,6 +367,7 @@ public void shouldApplyBuiltInOnIterable() throws ParseException, DatatypeConfig
342367
}
343368

344369
@Test
370+
@WithClasses( MapSourceTargetMapper.class )
345371
public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurationException {
346372

347373
MapSource source = new MapSource();
@@ -357,6 +383,7 @@ public void shouldApplyBuiltInOnMap() throws ParseException, DatatypeConfigurati
357383
}
358384

359385
@Test
386+
@WithClasses( CalendarToZonedDateTimeMapper.class )
360387
public void shouldApplyBuiltInOnCalendarToZonedDateTime() throws ParseException {
361388
assertThat( CalendarToZonedDateTimeMapper.INSTANCE.map( null ) ).isNull();
362389

@@ -373,6 +400,7 @@ public void shouldApplyBuiltInOnCalendarToZonedDateTime() throws ParseException
373400
}
374401

375402
@Test
403+
@WithClasses( ZonedDateTimeToCalendarMapper.class )
376404
public void shouldApplyBuiltInOnZonedDateTimeToCalendar() throws ParseException {
377405
assertThat( ZonedDateTimeToCalendarMapper.INSTANCE.map( null ) ).isNull();
378406

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.builtin.bean;
7+
8+
import java.math.BigDecimal;
9+
10+
public class BigDecimalProperty {
11+
12+
// CHECKSTYLE:OFF
13+
public BigDecimal publicProp;
14+
// CHECKSTYLE:ON
15+
16+
private BigDecimal prop;
17+
18+
public BigDecimal getProp() {
19+
return prop;
20+
}
21+
22+
public void setProp( BigDecimal prop ) {
23+
this.prop = prop;
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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.builtin.bean;
7+
8+
public class SomeType {
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.builtin.bean;
7+
8+
public class SomeTypeProperty {
9+
10+
// CHECKSTYLE:OFF
11+
public SomeType publicProp;
12+
// CHECKSTYLE:ON
13+
14+
private SomeType prop;
15+
16+
public SomeType getProp() {
17+
return prop;
18+
}
19+
20+
public void setProp( SomeType prop ) {
21+
this.prop = prop;
22+
}
23+
}

processor/src/test/java/org/mapstruct/ap/test/builtin/mapper/JaxbMapper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
package org.mapstruct.ap.test.builtin.mapper;
77

88
import org.mapstruct.Mapper;
9+
import org.mapstruct.ap.test.builtin.bean.BigDecimalProperty;
10+
import org.mapstruct.ap.test.builtin.bean.SomeType;
11+
import org.mapstruct.ap.test.builtin.bean.SomeTypeProperty;
912
import org.mapstruct.ap.test.builtin.bean.JaxbElementProperty;
1013
import org.mapstruct.ap.test.builtin.bean.StringProperty;
1114
import org.mapstruct.factory.Mappers;
@@ -16,4 +19,12 @@ public interface JaxbMapper {
1619
JaxbMapper INSTANCE = Mappers.getMapper( JaxbMapper.class );
1720

1821
StringProperty map(JaxbElementProperty source);
22+
23+
BigDecimalProperty mapBD(JaxbElementProperty source);
24+
25+
SomeTypeProperty mapSomeType(JaxbElementProperty source);
26+
27+
default SomeType map( String in ) {
28+
return new SomeType();
29+
}
1930
}

0 commit comments

Comments
 (0)