diff --git a/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java b/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java index 93bcc7cf1..cd58e03e5 100644 --- a/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java +++ b/integrationtest/src/test/resources/simpleTest/src/test/java/org/mapstruct/itest/simple/ConversionTest.java @@ -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" ); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java b/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java index f829c75ee..b4e1f029d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/MethodMatcher.java @@ -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; } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bool/BooleanMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/bool/BooleanMappingTest.java index c87054d18..3bc0e053a 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bool/BooleanMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/bool/BooleanMappingTest.java @@ -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 ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bool/Person.java b/processor/src/test/java/org/mapstruct/ap/test/bool/Person.java index ae35f5af9..24651cb85 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bool/Person.java +++ b/processor/src/test/java/org/mapstruct/ap/test/bool/Person.java @@ -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; + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bool/PersonDto.java b/processor/src/test/java/org/mapstruct/ap/test/bool/PersonDto.java index 3f8bf77e0..8993ee402 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bool/PersonDto.java +++ b/processor/src/test/java/org/mapstruct/ap/test/bool/PersonDto.java @@ -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; + } + } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bool/PersonMapper.java b/processor/src/test/java/org/mapstruct/ap/test/bool/PersonMapper.java index cee7f3d7b..b240eea0a 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bool/PersonMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/bool/PersonMapper.java @@ -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 ); diff --git a/processor/src/test/java/org/mapstruct/ap/test/bool/YesNo.java b/processor/src/test/java/org/mapstruct/ap/test/bool/YesNo.java new file mode 100644 index 000000000..ebf159905 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bool/YesNo.java @@ -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; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bool/YesNoMapper.java b/processor/src/test/java/org/mapstruct/ap/test/bool/YesNoMapper.java new file mode 100644 index 000000000..171b66cef --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bool/YesNoMapper.java @@ -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() ); + } +}