From 2cabfacbf42c856c5416f456006aa46f8336771f Mon Sep 17 00:00:00 2001 From: sjaakd Date: Fri, 3 Oct 2014 23:18:21 +0200 Subject: [PATCH] #305 Fix for target-getter-only in combination with expressions or constants --- .../ap/processor/MapperCreationProcessor.java | 33 +++++++++++++--- .../source/constants/SourceConstantsTest.java | 2 +- .../ap/test/source/constants/Target.java | 7 +--- .../expressions/java/JavaExpressionTest.java | 21 ++++++++++ .../source/expressions/java/SourceList.java | 39 +++++++++++++++++++ .../java/SourceTargetListMapper.java | 37 ++++++++++++++++++ .../source/expressions/java/TargetList.java | 36 +++++++++++++++++ 7 files changed, 164 insertions(+), 11 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceList.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceTargetListMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/TargetList.java diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java index b4cf61f2b..0615045d6 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -584,7 +584,8 @@ public class MapperCreationProcessor implements ModelElementProcessor mapperReferences, diff --git a/processor/src/test/java/org/mapstruct/ap/test/source/constants/SourceConstantsTest.java b/processor/src/test/java/org/mapstruct/ap/test/source/constants/SourceConstantsTest.java index e2622abf8..06ff188a9 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/source/constants/SourceConstantsTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/source/constants/SourceConstantsTest.java @@ -43,7 +43,7 @@ import static org.fest.assertions.Assertions.assertThat; public class SourceConstantsTest { @Test - @IssueKey("187") + @IssueKey("187, 305") @WithClasses({ Source.class, Source2.class, diff --git a/processor/src/test/java/org/mapstruct/ap/test/source/constants/Target.java b/processor/src/test/java/org/mapstruct/ap/test/source/constants/Target.java index e30b70e8b..192110c63 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/source/constants/Target.java +++ b/processor/src/test/java/org/mapstruct/ap/test/source/constants/Target.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.test.source.constants; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -31,7 +32,7 @@ public class Target { private int integerConstant; private Long longWrapperConstant; private Date dateConstant; - private List nameConstants; + private List nameConstants = new ArrayList(); public String getPropertyThatShouldBeMapped() { return propertyThatShouldBeMapped; @@ -77,8 +78,4 @@ public class Target { return nameConstants; } - public void setNameConstants(List nameConstants) { - this.nameConstants = nameConstants; - } - } diff --git a/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/JavaExpressionTest.java b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/JavaExpressionTest.java index 01381e90b..ed1904406 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/JavaExpressionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/JavaExpressionTest.java @@ -21,6 +21,7 @@ package org.mapstruct.ap.test.source.expressions.java; import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import org.junit.Test; @@ -134,4 +135,24 @@ public class JavaExpressionTest { } + @IssueKey( "305" ) + @Test + @WithClasses({ + SourceList.class, + TargetList.class, + SourceTargetListMapper.class + }) + public void testGetterOnly() throws ParseException { + + SourceList source = new SourceList(); + source.setList( Arrays.asList( "test1" ) ); + + TargetList target = SourceTargetListMapper.INSTANCE.map( source ); + assertThat( target ).isNotNull(); + assertThat( target.getList() ).isEqualTo( Arrays.asList( "test2" ) ); + + + } + + } diff --git a/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceList.java b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceList.java new file mode 100644 index 000000000..6fc936e96 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceList.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.source.expressions.java; + +import java.util.List; + +/** + * + * @author Sjaak Derksen + */ +public class SourceList { + + private List list; + + public List getList() { + return list; + } + + public void setList( List list ) { + this.list = list; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceTargetListMapper.java b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceTargetListMapper.java new file mode 100644 index 000000000..e1b6ec7ee --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/SourceTargetListMapper.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.source.expressions.java; + +import java.util.Arrays; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper( imports = { Arrays.class } ) +public interface SourceTargetListMapper { + + SourceTargetListMapper INSTANCE = Mappers.getMapper( SourceTargetListMapper.class ); + + @Mapping( target = "list", expression = "java(Arrays.asList(\"test2\"))" ) + TargetList map( SourceList source ); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/TargetList.java b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/TargetList.java new file mode 100644 index 000000000..58a10c81d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/source/expressions/java/TargetList.java @@ -0,0 +1,36 @@ +/** + * 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.source.expressions.java; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author Sjaak Derksen + */ +public class TargetList { + + private List list = new ArrayList(); + + public List getList() { + return list; + } + +}