From c465dd27c665bde3c93e926d0b53889d10ed7e6d Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Sat, 25 Mar 2017 08:44:21 +0100 Subject: [PATCH] #1155 Field accessors should be considered when resolving nested target properties --- .../model/source/TargetReference.java | 5 +- .../mapstruct/ap/test/bugs/_1155/Entity.java | 43 ++++++++++++++++ .../ap/test/bugs/_1155/Issue1155Mapper.java | 35 +++++++++++++ .../ap/test/bugs/_1155/Issue1155Test.java | 50 +++++++++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Entity.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Test.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java index a6259ea9b..00487d23e 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/TargetReference.java @@ -149,8 +149,9 @@ public class TargetReference { break; } - if ( (i == entryNames.length - 1) || (Executables.isSetterMethod( targetWriteAccessor ) ) ) { - // only intermediate nested properties when they are a true setter + if ( ( i == entryNames.length - 1 ) || ( Executables.isSetterMethod( targetWriteAccessor ) + || Executables.isFieldAccessor( targetWriteAccessor ) ) ) { + // only intermediate nested properties when they are a true setter or field accessor // the last may be other readAccessor (setter / getter / adder). if ( Executables.isGetterMethod( targetWriteAccessor ) || diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Entity.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Entity.java new file mode 100644 index 000000000..62a239435 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Entity.java @@ -0,0 +1,43 @@ +/** + * Copyright 2012-2017 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.bugs._1155; + +/** + * @author Filip Hrisafov + */ +class Entity { + + static class Client { + + //CHECKSTYLE:OFF + public long id; + //CHECKSTYLE:ON + } + + static class Dto { + + //CHECKSTYLE:OFF + public long clientId; + //CHECKSTYLE:ON + } + + //CHECKSTYLE:OFF + public Client client; + //CHECKSTYLE:ON +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Mapper.java new file mode 100644 index 000000000..5db16bd46 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Mapper.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2017 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.bugs._1155; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.factory.Mappers; + +/** + * @author Filip Hrisafov + */ +@Mapper +public interface Issue1155Mapper { + + Issue1155Mapper INSTANCE = Mappers.getMapper( Issue1155Mapper.class ); + + @Mapping(source = "clientId", target = "client.id") + Entity toEntity(Entity.Dto dto); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Test.java new file mode 100644 index 000000000..730c81b7e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1155/Issue1155Test.java @@ -0,0 +1,50 @@ +/** + * Copyright 2012-2017 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.bugs._1155; + +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.runner.AnnotationProcessorTestRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Filip Hrisafov + */ +@WithClasses({ + Entity.class, + Issue1155Mapper.class +}) +@RunWith(AnnotationProcessorTestRunner.class) +@IssueKey("1155") +public class Issue1155Test { + + @Test + public void shouldCompile() throws Exception { + + Entity.Dto dto = new Entity.Dto(); + dto.clientId = 10; + Entity entity = Issue1155Mapper.INSTANCE.toEntity( dto ); + + assertThat( entity.client ).isNotNull(); + assertThat( entity.client.id ).isEqualTo( 10 ); + } +}