#2077 nullpointer due to no-getter source (#2078)

This commit is contained in:
Sjaak Derksen 2020-04-25 17:01:22 +02:00 committed by GitHub
parent 3bffe96983
commit 7c62aec281
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 23 deletions

View File

@ -721,30 +721,33 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
sourceRef = getSourceRef( method.getSourceParameters().get( 0 ), targetPropertyName ); sourceRef = getSourceRef( method.getSourceParameters().get( 0 ), targetPropertyName );
} }
if ( sourceRef.isValid() ) { if ( sourceRef != null ) {
// sourceRef == null is not considered an error here
if ( sourceRef.isValid() ) {
// targetProperty == null can occur: we arrived here because we want as many errors // targetProperty == null can occur: we arrived here because we want as many errors
// as possible before we stop analysing // as possible before we stop analysing
propertyMapping = new PropertyMappingBuilder() propertyMapping = new PropertyMappingBuilder()
.mappingContext( ctx ) .mappingContext( ctx )
.sourceMethod( method ) .sourceMethod( method )
.target( targetPropertyName, targetReadAccessor, targetWriteAccessor ) .target( targetPropertyName, targetReadAccessor, targetWriteAccessor )
.sourcePropertyName( mapping.getSourceName() ) .sourcePropertyName( mapping.getSourceName() )
.sourceReference( sourceRef ) .sourceReference( sourceRef )
.selectionParameters( mapping.getSelectionParameters() ) .selectionParameters( mapping.getSelectionParameters() )
.formattingParameters( mapping.getFormattingParameters() ) .formattingParameters( mapping.getFormattingParameters() )
.existingVariableNames( existingVariableNames ) .existingVariableNames( existingVariableNames )
.dependsOn( mapping.getDependsOn() ) .dependsOn( mapping.getDependsOn() )
.defaultValue( mapping.getDefaultValue() ) .defaultValue( mapping.getDefaultValue() )
.defaultJavaExpression( mapping.getDefaultJavaExpression() ) .defaultJavaExpression( mapping.getDefaultJavaExpression() )
.mirror( mapping.getMirror() ) .mirror( mapping.getMirror() )
.options( mapping ) .options( mapping )
.build(); .build();
handledTargets.add( targetPropertyName ); handledTargets.add( targetPropertyName );
unprocessedSourceParameters.remove( sourceRef.getParameter() ); unprocessedSourceParameters.remove( sourceRef.getParameter() );
} }
else { else {
errorOccured = true; errorOccured = true;
}
} }
} }
// remaining are the mappings without a 'source' so, 'only' a date format or qualifiers // remaining are the mappings without a 'source' so, 'only' a date format or qualifiers

View File

@ -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._2077;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.factory.Mappers;
/**
* @author Sjaak Derksen
*/
@Mapper( unmappedTargetPolicy = ReportingPolicy.ERROR )
public interface Issue2077ErroneousMapper {
Issue2077ErroneousMapper INSTANCE = Mappers.getMapper( Issue2077ErroneousMapper.class );
@Mapping(target = "s1", defaultValue = "xyz" )
Target map(String source);
class Target {
private String s1;
public String getS1() {
return s1;
}
public void setS1(String s1) {
this.s1 = s1;
}
}
}

View File

@ -0,0 +1,39 @@
/*
* 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._2077;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static javax.tools.Diagnostic.Kind.ERROR;
/**
* @author Sjaak Derksen
*/
@IssueKey("2077")
@RunWith(AnnotationProcessorTestRunner.class)
public class Issue2077Test {
@Test
@WithClasses(Issue2077ErroneousMapper.class)
@ExpectedCompilationOutcome(
value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(type = Issue2077ErroneousMapper.class,
kind = ERROR,
line = 22,
message = "Unmapped target property: \"s1\".")
}
)
public void shouldNotCompile() {
}
}