diff --git a/processor/src/main/java/org/mapstruct/ap/model/assignment/AdderWrapper.java b/processor/src/main/java/org/mapstruct/ap/model/assignment/AdderWrapper.java index b3e0f1038..49cf519b8 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/assignment/AdderWrapper.java +++ b/processor/src/main/java/org/mapstruct/ap/model/assignment/AdderWrapper.java @@ -19,7 +19,9 @@ package org.mapstruct.ap.model.assignment; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.mapstruct.ap.model.Assignment; import org.mapstruct.ap.model.common.Type; @@ -69,6 +71,14 @@ public class AdderWrapper extends AssignmentWrapper { return sourceReference; } + @Override + public Set getImportTypes() { + Set imported = new HashSet(); + imported.addAll( super.getImportTypes() ); + imported.add( sourceType ); + return imported; + } + public String getIteratorReference() { return getAssignment().getSourceReference(); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java index bf0569361..0f17990f8 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/AdderTest.java @@ -20,6 +20,7 @@ package org.mapstruct.ap.test.collection.adder; import java.util.ArrayList; import java.util.Arrays; +import static org.fest.assertions.Assertions.assertThat; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,9 +41,11 @@ import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; -import static org.fest.assertions.Assertions.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import org.mapstruct.ap.test.collection.adder.source.Foo; +import org.mapstruct.ap.test.collection.adder.source.Source2; +import org.mapstruct.ap.test.collection.adder.target.Target2; /** * @author Sjaak Derksen @@ -239,4 +242,22 @@ public class AdderTest { assertThat( target.getPets().get( 0 ) ).isEqualTo( 2L ); assertTrue( AdderUsageObserver.isUsed() ); } + + @IssueKey( "310" ) + @Test + @WithClasses( { + Target2.class, + Source2.class, + Source2Target2Mapper.class, + Foo.class + } ) + public void testMissingImport() throws DogException { + + Source2 source = new Source2(); + source.setAttributes( Arrays.asList( new Foo() ) ); + + Target2 target = Source2Target2Mapper.INSTANCE.toTarget( source ); + assertThat( target ).isNotNull(); + assertThat( target.getAttributes().size() ).isEqualTo( 1 ); + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/Source2Target2Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/Source2Target2Mapper.java new file mode 100644 index 000000000..825e86db2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/Source2Target2Mapper.java @@ -0,0 +1,38 @@ +/** + * 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.collection.adder; + +import org.mapstruct.CollectionMappingStrategy; +import org.mapstruct.Mapper; +import org.mapstruct.ap.test.collection.adder.source.Source2; +import org.mapstruct.ap.test.collection.adder.target.Target2; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper( collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED ) +public abstract class Source2Target2Mapper { + + public static final Source2Target2Mapper INSTANCE = Mappers.getMapper( Source2Target2Mapper.class ); + + public abstract Target2 toTarget( Source2 source ); + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/source/Foo.java b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/source/Foo.java new file mode 100644 index 000000000..bb12a1485 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/source/Foo.java @@ -0,0 +1,27 @@ +/** + * 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.collection.adder.source; + +/** + * + * @author Sjaak Derksen + */ +public class Foo { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/source/Source2.java b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/source/Source2.java new file mode 100644 index 000000000..741ceaf89 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/source/Source2.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.collection.adder.source; + +import java.util.List; + +/** + * + * @author Sjaak Derksen + */ +public class Source2 { + + private List attributes; + + public List getAttributes() { + return attributes; + } + + public void setAttributes( List attributes ) { + this.attributes = attributes; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/adder/target/Target2.java b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/target/Target2.java new file mode 100644 index 000000000..3350ab476 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/adder/target/Target2.java @@ -0,0 +1,43 @@ +/* + * Copyright 2014 Sjaak Derksen. + * + * 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.collection.adder.target; + +import java.util.ArrayList; +import java.util.List; +import org.mapstruct.ap.test.collection.adder.source.Foo; + +/** + * + * @author Sjaak Derksen + */ +public class Target2 { + + private List attributes = new ArrayList(); + + public Foo addAttribute( Foo foo ) { + attributes.add( foo ); + return foo; + } + + public List getAttributes() { + return attributes; + } + + public void setAttributes( List attributes ) { + this.attributes = attributes; + } + +}