Skip to content

Commit 85890dd

Browse files
committed
mapstruct#2245 Local variable should be created when using default value
1 parent e67daa3 commit 85890dd

4 files changed

Lines changed: 155 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,11 @@ private Assignment getDefaultValueAssignment( Assignment rhs ) {
374374
return null;
375375
}
376376

377+
private boolean hasDefaultValueAssignment(Assignment rhs) {
378+
return ( defaultValue != null || defaultJavaExpression != null ) &&
379+
( !rhs.getSourceType().isPrimitive() || rhs.getSourcePresenceCheckerReference() != null);
380+
}
381+
377382
private Assignment assignToPlain(Type targetType, AccessorType targetAccessorType,
378383
Assignment rightHandSide) {
379384

@@ -415,7 +420,9 @@ private Assignment assignToPlainViaSetter(Type targetType, Assignment rhs) {
415420
);
416421
}
417422
else {
418-
boolean includeSourceNullCheck = SetterWrapper.doSourceNullCheck( rhs, nvcs, nvpms, targetType );
423+
// If the property mapping has a default value assignment then we have to do a null value check
424+
boolean includeSourceNullCheck = SetterWrapper.doSourceNullCheck( rhs, nvcs, nvpms, targetType )
425+
|| hasDefaultValueAssignment( rhs );
419426
if ( !includeSourceNullCheck ) {
420427
// solution for #834 introduced a local var and null check for nested properties always.
421428
// however, a local var is not needed if there's no need to check for null.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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._2245;
7+
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.mapstruct.ap.testutil.IssueKey;
12+
import org.mapstruct.ap.testutil.WithClasses;
13+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
14+
import org.mapstruct.ap.testutil.runner.GeneratedSource;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
/**
19+
* @author Filip Hrisafov
20+
*/
21+
@IssueKey("2245")
22+
@RunWith(AnnotationProcessorTestRunner.class)
23+
@WithClasses({
24+
TestMapper.class
25+
})
26+
public class Issue2245Test {
27+
28+
@Rule
29+
public final GeneratedSource generatedSource = new GeneratedSource()
30+
.addComparisonToFixtureFor( TestMapper.class );
31+
32+
@Test
33+
public void shouldGenerateSourceGetMethodOnce() {
34+
35+
TestMapper.Tenant tenant =
36+
TestMapper.INSTANCE.map( new TestMapper.TenantDTO( new TestMapper.Inner( "acme" ) ) );
37+
38+
assertThat( tenant ).isNotNull();
39+
assertThat( tenant.getId() ).isEqualTo( "acme" );
40+
41+
}
42+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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._2245;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.factory.Mappers;
11+
12+
@Mapper
13+
public interface TestMapper {
14+
15+
TestMapper INSTANCE = Mappers.getMapper( TestMapper.class );
16+
17+
class Tenant {
18+
private String id;
19+
20+
public String getId() {
21+
return id;
22+
}
23+
24+
public void setId(String id) {
25+
this.id = id;
26+
}
27+
}
28+
29+
class Inner {
30+
private final String id;
31+
32+
public Inner(String id) {
33+
this.id = id;
34+
}
35+
36+
public String getId() {
37+
return id;
38+
}
39+
}
40+
41+
class TenantDTO {
42+
private final Inner inner;
43+
44+
public TenantDTO(Inner inner) {
45+
this.inner = inner;
46+
}
47+
48+
public Inner getInner() {
49+
return inner;
50+
}
51+
}
52+
53+
@Mapping(target = "id", source = "inner.id", defaultValue = "test")
54+
Tenant map(TenantDTO tenant);
55+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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._2245;
7+
8+
import javax.annotation.Generated;
9+
10+
@Generated(
11+
value = "org.mapstruct.ap.MappingProcessor",
12+
date = "2020-10-24T17:57:23+0200",
13+
comments = "version: , compiler: javac, environment: Java 1.8.0_202 (AdoptOpenJdk)"
14+
)
15+
public class TestMapperImpl implements TestMapper {
16+
17+
@Override
18+
public Tenant map(TenantDTO tenant) {
19+
if ( tenant == null ) {
20+
return null;
21+
}
22+
23+
Tenant tenant1 = new Tenant();
24+
25+
String id = tenantInnerId( tenant );
26+
if ( id != null ) {
27+
tenant1.setId( id );
28+
}
29+
else {
30+
tenant1.setId( "test" );
31+
}
32+
33+
return tenant1;
34+
}
35+
36+
private String tenantInnerId(TenantDTO tenantDTO) {
37+
if ( tenantDTO == null ) {
38+
return null;
39+
}
40+
Inner inner = tenantDTO.getInner();
41+
if ( inner == null ) {
42+
return null;
43+
}
44+
String id = inner.getId();
45+
if ( id == null ) {
46+
return null;
47+
}
48+
return id;
49+
}
50+
}

0 commit comments

Comments
 (0)