Skip to content

Commit 7c62aec

Browse files
authored
mapstruct#2077 nullpointer due to no-getter source (mapstruct#2078)
1 parent 3bffe96 commit 7c62aec

3 files changed

Lines changed: 102 additions & 23 deletions

File tree

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

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -721,30 +721,33 @@ else if ( mapping.getJavaExpression() != null ) {
721721
sourceRef = getSourceRef( method.getSourceParameters().get( 0 ), targetPropertyName );
722722
}
723723

724-
if ( sourceRef.isValid() ) {
724+
if ( sourceRef != null ) {
725+
// sourceRef == null is not considered an error here
726+
if ( sourceRef.isValid() ) {
725727

726-
// targetProperty == null can occur: we arrived here because we want as many errors
727-
// as possible before we stop analysing
728-
propertyMapping = new PropertyMappingBuilder()
729-
.mappingContext( ctx )
730-
.sourceMethod( method )
731-
.target( targetPropertyName, targetReadAccessor, targetWriteAccessor )
732-
.sourcePropertyName( mapping.getSourceName() )
733-
.sourceReference( sourceRef )
734-
.selectionParameters( mapping.getSelectionParameters() )
735-
.formattingParameters( mapping.getFormattingParameters() )
736-
.existingVariableNames( existingVariableNames )
737-
.dependsOn( mapping.getDependsOn() )
738-
.defaultValue( mapping.getDefaultValue() )
739-
.defaultJavaExpression( mapping.getDefaultJavaExpression() )
740-
.mirror( mapping.getMirror() )
741-
.options( mapping )
742-
.build();
743-
handledTargets.add( targetPropertyName );
744-
unprocessedSourceParameters.remove( sourceRef.getParameter() );
745-
}
746-
else {
747-
errorOccured = true;
728+
// targetProperty == null can occur: we arrived here because we want as many errors
729+
// as possible before we stop analysing
730+
propertyMapping = new PropertyMappingBuilder()
731+
.mappingContext( ctx )
732+
.sourceMethod( method )
733+
.target( targetPropertyName, targetReadAccessor, targetWriteAccessor )
734+
.sourcePropertyName( mapping.getSourceName() )
735+
.sourceReference( sourceRef )
736+
.selectionParameters( mapping.getSelectionParameters() )
737+
.formattingParameters( mapping.getFormattingParameters() )
738+
.existingVariableNames( existingVariableNames )
739+
.dependsOn( mapping.getDependsOn() )
740+
.defaultValue( mapping.getDefaultValue() )
741+
.defaultJavaExpression( mapping.getDefaultJavaExpression() )
742+
.mirror( mapping.getMirror() )
743+
.options( mapping )
744+
.build();
745+
handledTargets.add( targetPropertyName );
746+
unprocessedSourceParameters.remove( sourceRef.getParameter() );
747+
}
748+
else {
749+
errorOccured = true;
750+
}
748751
}
749752
}
750753
// remaining are the mappings without a 'source' so, 'only' a date format or qualifiers
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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._2077;
7+
8+
import org.mapstruct.Mapper;
9+
import org.mapstruct.Mapping;
10+
import org.mapstruct.ReportingPolicy;
11+
import org.mapstruct.factory.Mappers;
12+
13+
/**
14+
* @author Sjaak Derksen
15+
*/
16+
@Mapper( unmappedTargetPolicy = ReportingPolicy.ERROR )
17+
public interface Issue2077ErroneousMapper {
18+
19+
Issue2077ErroneousMapper INSTANCE = Mappers.getMapper( Issue2077ErroneousMapper.class );
20+
21+
@Mapping(target = "s1", defaultValue = "xyz" )
22+
Target map(String source);
23+
24+
class Target {
25+
26+
private String s1;
27+
28+
public String getS1() {
29+
return s1;
30+
}
31+
32+
public void setS1(String s1) {
33+
this.s1 = s1;
34+
}
35+
36+
}
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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._2077;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mapstruct.ap.testutil.IssueKey;
11+
import org.mapstruct.ap.testutil.WithClasses;
12+
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
13+
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
14+
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
15+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
16+
17+
import static javax.tools.Diagnostic.Kind.ERROR;
18+
19+
/**
20+
* @author Sjaak Derksen
21+
*/
22+
@IssueKey("2077")
23+
@RunWith(AnnotationProcessorTestRunner.class)
24+
public class Issue2077Test {
25+
26+
@Test
27+
@WithClasses(Issue2077ErroneousMapper.class)
28+
@ExpectedCompilationOutcome(
29+
value = CompilationResult.FAILED,
30+
diagnostics = {
31+
@Diagnostic(type = Issue2077ErroneousMapper.class,
32+
kind = ERROR,
33+
line = 22,
34+
message = "Unmapped target property: \"s1\".")
35+
}
36+
)
37+
public void shouldNotCompile() {
38+
}
39+
}

0 commit comments

Comments
 (0)