#398 allow autoboxing for primitive return types of property mapping methods to non-primitive properties

This commit is contained in:
Andreas Gudian 2014-12-30 19:18:28 +01:00
parent 7e6084f3d5
commit 24984cb5be
8 changed files with 149 additions and 8 deletions

View File

@ -37,7 +37,7 @@ public class ConversionTest {
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) );
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 43 ) );
assertThat( target.getBar() ).isEqualTo( 23 );
assertThat( target.getZip() ).isEqualTo( "73" );
}
@ -48,7 +48,7 @@ public class ConversionTest {
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 0 ) );
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 1 ) );
assertThat( target.getBar() ).isEqualTo( 0 );
assertThat( target.getZip() ).isEqualTo( "0" );
}
@ -62,7 +62,7 @@ public class ConversionTest {
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getBaz() ).isEqualTo( Long.valueOf( 42 ) );
assertThat( target.getBaz() ).isEqualTo( Long.valueOf( 43 ) );
assertThat( target.getQax() ).isEqualTo( 23 );
}
@ -77,7 +77,7 @@ public class ConversionTest {
assertThat( source ).isNotNull();
assertThat( source.getFoo() ).isEqualTo( 42 );
assertThat( source.getBar() ).isEqualTo( 23 );
assertThat( source.getBar() ).isEqualTo( 24 );
assertThat( source.getZip() ).isEqualTo( 73 );
}
@ -90,7 +90,7 @@ public class ConversionTest {
Source source = SourceTargetMapper.INSTANCE.targetToSource( target );
assertThat( source ).isNotNull();
assertThat( source.getBaz() ).isEqualTo( 42 );
assertThat( source.getBaz() ).isEqualTo( 43 );
assertThat( source.getQax() ).isEqualTo( 23 );
}
@ -104,7 +104,7 @@ public class ConversionTest {
Target target = SourceTargetAbstractMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) );
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 43 ) );
assertThat( target.getBar() ).isEqualTo( 23 );
assertThat( target.getZip() ).isEqualTo( "73" );
}

View File

@ -127,6 +127,17 @@ public class MethodMatcher {
return false;
}
}
else if ( candidateReturnType.getKind().isPrimitive() ) {
TypeMirror boxedCandidateReturnType =
typeUtils.boxedClass( (PrimitiveType) candidateReturnType ).asType();
TypeMatcher boxedReturnTypeMatcher =
new TypeMatcher( Assignability.VISITED_ASSIGNABLE_TO, genericTypesMap );
if ( !boxedReturnTypeMatcher.visit( boxedCandidateReturnType, targetType.getTypeMirror() ) ) {
return false;
}
}
else {
return false;
}

View File

@ -28,7 +28,9 @@ import static org.fest.assertions.Assertions.assertThat;
@WithClasses({
Person.class,
PersonDto.class,
PersonMapper.class
YesNo.class,
PersonMapper.class,
YesNoMapper.class
})
@RunWith(AnnotationProcessorTestRunner.class)
public class BooleanMappingTest {
@ -58,4 +60,19 @@ public class BooleanMappingTest {
//then
assertThat( personDto.getEngaged() ).isEqualTo( "true" );
}
@Test
public void shouldMapBooleanPropertyWithPropertyMappingMethod() {
// given
Person person = new Person();
person.setDivorced( new YesNo( true ) );
person.setWidowed( new YesNo( true ) );
// when
PersonDto personDto = PersonMapper.INSTANCE.personToDto( person );
// then
assertThat( personDto.getDivorced() ).isEqualTo( "yes" );
assertThat( personDto.getWidowed() ).isEqualTo( Boolean.TRUE );
}
}

View File

@ -22,6 +22,8 @@ public class Person {
private Boolean married;
private Boolean engaged;
private YesNo divorced;
private YesNo widowed;
public Boolean isMarried() {
return married;
@ -42,4 +44,20 @@ public class Person {
public void setEngaged(Boolean engaged) {
this.engaged = engaged;
}
public YesNo getDivorced() {
return divorced;
}
public void setDivorced(YesNo divorced) {
this.divorced = divorced;
}
public YesNo getWidowed() {
return widowed;
}
public void setWidowed(YesNo widowed) {
this.widowed = widowed;
}
}

View File

@ -22,6 +22,8 @@ public class PersonDto {
private String married;
private String engaged;
private String divorced;
private Boolean widowed;
public String getMarried() {
return married;
@ -38,4 +40,21 @@ public class PersonDto {
public void setEngaged(String engaged) {
this.engaged = engaged;
}
public String getDivorced() {
return divorced;
}
public void setDivorced(String divorced) {
this.divorced = divorced;
}
public Boolean getWidowed() {
return widowed;
}
public void setWidowed(Boolean widowed) {
this.widowed = widowed;
}
}

View File

@ -21,7 +21,7 @@ package org.mapstruct.ap.test.bool;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
@Mapper( uses = YesNoMapper.class )
public interface PersonMapper {
PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class );

View File

@ -0,0 +1,39 @@
/**
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.bool;
/**
* @author Andreas Gudian
*
*/
public class YesNo {
private boolean yes;
public YesNo(boolean yes) {
this.yes = yes;
}
public boolean isYes() {
return yes;
}
public void setYes(boolean yes) {
this.yes = yes;
}
}

View File

@ -0,0 +1,37 @@
/**
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.bool;
/**
* @author Andreas Gudian
*
*/
public class YesNoMapper {
public String toString(YesNo yesNo) {
if ( null != yesNo && yesNo.isYes() ) {
return "yes";
}
return "no";
}
public boolean toBool(YesNo yesNo) {
return ( null != yesNo && yesNo.isYes() );
}
}