diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java index e4f6410c5..0dc269c87 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/PropertyMapping.java @@ -348,6 +348,12 @@ public class PropertyMapping extends ModelElement { return result; } + // a nested source reference will take the bean mapping parameter itself, no null check is required + // since this is handled by the BeanMapping + if ( sourceReference.getPropertyEntries().size() > 1 ) { + return result; + } + // add a null / presence checked when required if ( sourceType.isPrimitive() ) { if ( getSourcePresenceCheckerRef() != null ) { diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/Commit.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/Commit.java new file mode 100644 index 000000000..6c75350e8 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/Commit.java @@ -0,0 +1,46 @@ +/** + * Copyright 2012-2016 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._843; + +import java.util.Date; + +/** + * + * @author Sjaak Derksen + */ +public class Commit { + + private static int callCounter; + + private Date authoredDate; + + public Date getAuthoredDate() { + callCounter++; + return authoredDate; + } + + public void setAuthoredDate(Date authoredDate) { + this.authoredDate = authoredDate; + } + + public static int getCallCounter() { + return callCounter; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/GitlabTag.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/GitlabTag.java new file mode 100644 index 000000000..b7764edd1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/GitlabTag.java @@ -0,0 +1,44 @@ +/** + * Copyright 2012-2016 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._843; + +/** + * + * @author Sjaak Derksen + */ +public class GitlabTag { + + private static int callCounter = 0; + + private Commit commit; + + public Commit getCommit() { + callCounter++; + return commit; + } + + public void setCommit(Commit commit) { + this.commit = commit; + } + + public static int getCallCounter() { + return callCounter; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/Issue843Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/Issue843Test.java new file mode 100644 index 000000000..a55219de6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/Issue843Test.java @@ -0,0 +1,58 @@ +/** + * Copyright 2012-2016 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._843; + +import java.util.Date; +import static org.assertj.core.api.Assertions.assertThat; +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; + +/** + * + * @author Sjaak Derksen + */ +@RunWith(AnnotationProcessorTestRunner.class) +@WithClasses({ + Commit.class, + TagInfo.class, + GitlabTag.class, + TagMapper.class +}) +@IssueKey("843") +public class Issue843Test { + + @Test + public void testMapperCreation() { + + Commit commit = new Commit(); + commit.setAuthoredDate( new Date() ); + GitlabTag gitlabTag = new GitlabTag(); + gitlabTag.setCommit( commit ); + + TagMapper.INSTANCE.gitlabTagToTagInfo( gitlabTag ); + + assertThat( Commit.getCallCounter() ).isEqualTo( 1 ); + assertThat( GitlabTag.getCallCounter() ).isEqualTo( 1 ); + + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/TagInfo.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/TagInfo.java new file mode 100644 index 000000000..12287ab4d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/TagInfo.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2016 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._843; + +import java.util.Date; + +/** + * + * @author Sjaak Derksen + */ +public class TagInfo { + + private Date date; + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/TagMapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/TagMapper.java new file mode 100644 index 000000000..86e4a30c6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_843/TagMapper.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012-2016 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._843; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.NullValueCheckStrategy; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper( nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS ) +public interface TagMapper { + + TagMapper INSTANCE = Mappers.getMapper( TagMapper.class ); + + @Mapping(target = "date", source = "gitlabTag.commit.authoredDate") + TagInfo gitlabTagToTagInfo(GitlabTag gitlabTag); +}