#229 Add null-check before auto-unboxing in direct assignments

This commit is contained in:
Andreas Gudian 2014-06-22 12:55:27 +02:00 committed by Gunnar Morling
parent a6a9971c30
commit 5afc627735
4 changed files with 43 additions and 9 deletions

View File

@ -791,13 +791,13 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
if ( assignment.getType() == DIRECT ) { if ( assignment.getType() == DIRECT ) {
assignment = new NullCheckWrapper( assignment ); assignment = new NullCheckWrapper( assignment );
} }
} }
else { else {
if ( targetAccessorType == TargetAccessorType.SETTER ) { if ( targetAccessorType == TargetAccessorType.SETTER ) {
assignment = new SetterWrapper( assignment, method.getThrownTypes() ); assignment = new SetterWrapper( assignment, method.getThrownTypes() );
if ( !sourceType.isPrimitive() && ( assignment.getType() == TYPE_CONVERTED ) ) { if ( !sourceType.isPrimitive() &&
( assignment.getType() == TYPE_CONVERTED ||
assignment.getType() == DIRECT && targetType.isPrimitive() ) ) {
// for primitive types null check is not possible at all, but a conversion needs // for primitive types null check is not possible at all, but a conversion needs
// a null check. // a null check.
assignment = new NullCheckWrapper( assignment ); assignment = new NullCheckWrapper( assignment );

View File

@ -18,13 +18,14 @@
*/ */
package org.mapstruct.ap.test.conversion.nativetypes; package org.mapstruct.ap.test.conversion.nativetypes;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
@WithClasses({ @WithClasses({
BooleanSource.class, BooleanSource.class,
BooleanTarget.class, BooleanTarget.class,
@ -58,4 +59,14 @@ public class BooleanConversionTest {
assertThat( source.isB() ).isEqualTo( true ); assertThat( source.isB() ).isEqualTo( true );
assertThat( source.getBool() ).isEqualTo( true ); assertThat( source.getBool() ).isEqualTo( true );
} }
@Test
@IssueKey( "229" )
public void wrapperToPrimitveIsNullSafe() {
BooleanTarget target = new BooleanTarget();
BooleanSource source = BooleanMapper.INSTANCE.targetToSource( target );
assertThat( source ).isNotNull();
}
} }

View File

@ -18,13 +18,14 @@
*/ */
package org.mapstruct.ap.test.conversion.nativetypes; package org.mapstruct.ap.test.conversion.nativetypes;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
@WithClasses({ @WithClasses({
CharSource.class, CharSource.class,
CharTarget.class, CharTarget.class,
@ -54,4 +55,14 @@ public class CharConversionTest {
assertThat( source ).isNotNull(); assertThat( source ).isNotNull();
assertThat( source.getC() ).isEqualTo( 'G' ); assertThat( source.getC() ).isEqualTo( 'G' );
} }
@Test
@IssueKey( "229" )
public void wrapperToPrimitveIsNullSafe() {
CharTarget target = new CharTarget();
CharSource source = CharMapper.INSTANCE.targetToSource( target );
assertThat( source ).isNotNull();
}
} }

View File

@ -18,13 +18,14 @@
*/ */
package org.mapstruct.ap.test.conversion.nativetypes; package org.mapstruct.ap.test.conversion.nativetypes;
import static org.fest.assertions.Assertions.assertThat;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat;
@WithClasses({ @WithClasses({
ByteSource.class, ByteSource.class,
ByteTarget.class, ByteTarget.class,
@ -450,4 +451,15 @@ public class NumberConversionTest {
assertThat( target.getD() ).isEqualTo( 11d ); assertThat( target.getD() ).isEqualTo( 11d );
assertThat( target.getDd() ).isEqualTo( Double.valueOf( 12d ) ); assertThat( target.getDd() ).isEqualTo( Double.valueOf( 12d ) );
} }
@Test
@IssueKey( "229" )
public void wrapperToPrimitveIsNullSafe() {
assertThat( SourceTargetMapper.INSTANCE.sourceToTarget( new ByteWrapperSource() ) ).isNotNull();
assertThat( SourceTargetMapper.INSTANCE.sourceToTarget( new DoubleWrapperSource() ) ).isNotNull();
assertThat( SourceTargetMapper.INSTANCE.sourceToTarget( new ShortWrapperSource() ) ).isNotNull();
assertThat( SourceTargetMapper.INSTANCE.sourceToTarget( new IntWrapperSource() ) ).isNotNull();
assertThat( SourceTargetMapper.INSTANCE.sourceToTarget( new FloatWrapperSource() ) ).isNotNull();
assertThat( SourceTargetMapper.INSTANCE.sourceToTarget( new LongWrapperSource() ) ).isNotNull();
}
} }