diff --git a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java index 1485e431f..599c7532f 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -118,7 +118,13 @@ public class IterableMappingMethod extends MappingMethod { ); if ( assignment == null ) { - ctx.getMessager().printMessage( method.getExecutable(), Message.ITERABLEMAPPING_MAPPING_NOT_FOUND ); + if ( method instanceof ForgedMethod ) { + // leave messaging to calling property mapping + return null; + } + else { + ctx.getMessager().printMessage( method.getExecutable(), Message.ITERABLEMAPPING_MAPPING_NOT_FOUND ); + } } else { if ( method instanceof ForgedMethod ) { diff --git a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java index f660c3215..65e3df0c4 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java @@ -131,7 +131,14 @@ public class MapMappingMethod extends MappingMethod { ); if ( keyAssignment == null ) { - ctx.getMessager().printMessage( method.getExecutable(), Message.MAPMAPPING_KEY_MAPPING_NOT_FOUND ); + if ( method instanceof ForgedMethod ) { + // leave messaging to calling property mapping + return null; + } + else { + ctx.getMessager().printMessage( method.getExecutable(), + Message.MAPMAPPING_KEY_MAPPING_NOT_FOUND ); + } } // find mapping method or conversion for value @@ -162,7 +169,14 @@ public class MapMappingMethod extends MappingMethod { } if ( valueAssignment == null ) { - ctx.getMessager().printMessage( method.getExecutable(), Message.MAPMAPPING_VALUE_MAPPING_NOT_FOUND ); + if ( method instanceof ForgedMethod ) { + // leave messaging to calling property mapping + return null; + } + else { + ctx.getMessager().printMessage( method.getExecutable(), + Message.MAPMAPPING_VALUE_MAPPING_NOT_FOUND ); + } } // mapNullToDefault diff --git a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java index efce549fb..5ab61fff0 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -467,17 +467,18 @@ public class PropertyMapping extends ModelElement { .method( methodRef ) .build(); - if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) { - ctx.getMappingsToGenerate().add( iterableMappingMethod ); - } - else { - String existingName = ctx.getExistingMappingMethod( iterableMappingMethod ).getName(); - methodRef = new ForgedMethod( existingName, methodRef ); - } - - assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType ); - assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) ); + if ( iterableMappingMethod != null ) { + if ( !ctx.getMappingsToGenerate().contains( iterableMappingMethod ) ) { + ctx.getMappingsToGenerate().add( iterableMappingMethod ); + } + else { + String existingName = ctx.getExistingMappingMethod( iterableMappingMethod ).getName(); + methodRef = new ForgedMethod( existingName, methodRef ); + } + assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType ); + assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) ); + } } else if ( sourceType.isMapType() && targetType.isMapType() ) { @@ -489,15 +490,17 @@ public class PropertyMapping extends ModelElement { .method( methodRef ) .build(); - if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) { - ctx.getMappingsToGenerate().add( mapMappingMethod ); + if ( mapMappingMethod != null ) { + if ( !ctx.getMappingsToGenerate().contains( mapMappingMethod ) ) { + ctx.getMappingsToGenerate().add( mapMappingMethod ); + } + else { + String existingName = ctx.getExistingMappingMethod( mapMappingMethod ).getName(); + methodRef = new ForgedMethod( existingName, methodRef ); + } + assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType ); + assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) ); } - else { - String existingName = ctx.getExistingMappingMethod( mapMappingMethod ).getName(); - methodRef = new ForgedMethod( existingName, methodRef ); - } - assignment = AssignmentFactory.createMethodReference( methodRef, null, targetType ); - assignment.setAssignment( AssignmentFactory.createDirect( sourceReference ) ); } return assignment; } diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/Bar.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/Bar.java new file mode 100644 index 000000000..a730cb62b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/Bar.java @@ -0,0 +1,27 @@ +/** + * Copyright 2012-2015 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.forged; + +/** + * + * @author Sjaak Derksen + */ +public class Bar { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java index 15ee41570..fa3a53f5b 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/CollectionMappingTest.java @@ -21,11 +21,15 @@ package org.mapstruct.ap.test.collection.forged; import com.google.common.collect.ImmutableMap; import java.util.Map; +import javax.tools.Diagnostic.Kind; import static org.fest.assertions.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.compilation.annotation.CompilationResult; +import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic; +import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.util.Collections; @@ -72,4 +76,46 @@ public class CollectionMappingTest { assertThat( source2.getBarMap() ).isEqualTo( sourceMap ); } + @Test + @WithClasses({ ErroneousCollectionNonMappableSetMapper.class, + ErroneousNonMappableSetSource.class, + ErroneousNonMappableSetTarget.class, + Foo.class, + Bar.class + }) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic(type = ErroneousCollectionNonMappableSetMapper.class, + kind = Kind.ERROR, + line = 30, + messageRegExp = "Can't map property \".* nonMappableSet\" to \".* nonMappableSet\". " + + "Consider to declare/implement a mapping method: .*."), + } + ) + public void shouldGenerateNonMappleMethodForSetMapping() { + } + + @Test + @WithClasses({ ErroneousCollectionNonMappableMapMapper.class, + ErroneousNonMappableMapSource.class, + ErroneousNonMappableMapTarget.class, + Foo.class, + Bar.class + }) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic(type = ErroneousCollectionNonMappableMapMapper.class, + kind = Kind.ERROR, + line = 30, + messageRegExp = "Can't map property \".* nonMappableMap\" to \".* nonMappableMap\". " + + "Consider to declare/implement a mapping method: .*."), + } + ) + public void shouldGenerateNonMappleMethodForMapMapping() { + } + + + } diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousCollectionNonMappableMapMapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousCollectionNonMappableMapMapper.java new file mode 100644 index 000000000..869abe34d --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousCollectionNonMappableMapMapper.java @@ -0,0 +1,31 @@ +/** + * Copyright 2012-2015 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.forged; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface ErroneousCollectionNonMappableMapMapper { + + ErroneousCollectionNonMappableMapMapper INSTANCE = + Mappers.getMapper( ErroneousCollectionNonMappableMapMapper.class ); + + ErroneousNonMappableMapTarget sourceToTarget(ErroneousNonMappableMapSource source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousCollectionNonMappableSetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousCollectionNonMappableSetMapper.java new file mode 100644 index 000000000..4f2a899dc --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousCollectionNonMappableSetMapper.java @@ -0,0 +1,31 @@ +/** + * Copyright 2012-2015 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.forged; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface ErroneousCollectionNonMappableSetMapper { + + ErroneousCollectionNonMappableSetMapper INSTANCE = + Mappers.getMapper( ErroneousCollectionNonMappableSetMapper.class ); + + ErroneousNonMappableSetTarget sourceToTarget(ErroneousNonMappableSetSource source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableMapSource.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableMapSource.java new file mode 100644 index 000000000..3484365b7 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableMapSource.java @@ -0,0 +1,36 @@ +/** + * Copyright 2012-2015 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.forged; + +import java.util.Map; + +public class ErroneousNonMappableMapSource { + + private Map nonMappableMap; + + public Map getNonMappableMap() { + return nonMappableMap; + } + + public void setNonMappableMap(Map nonMappableMap) { + this.nonMappableMap = nonMappableMap; + } + + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableMapTarget.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableMapTarget.java new file mode 100644 index 000000000..d3842dcdb --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableMapTarget.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2015 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.forged; + +import java.util.Map; + +public class ErroneousNonMappableMapTarget { + + private Map nonMappableMap; + + public Map getNonMappableMap() { + return nonMappableMap; + } + + public void setNonMappableMap(Map nonMappableMap) { + this.nonMappableMap = nonMappableMap; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableSetSource.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableSetSource.java new file mode 100644 index 000000000..5f73be4e2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableSetSource.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2015 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.forged; + +import java.util.Set; + +public class ErroneousNonMappableSetSource { + + private Set nonMappableSet; + + public Set getNonMappableSet() { + return nonMappableSet; + } + + public void setNonMappableSet(Set nonMappableSet) { + this.nonMappableSet = nonMappableSet; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableSetTarget.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableSetTarget.java new file mode 100644 index 000000000..6aa7f253b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/ErroneousNonMappableSetTarget.java @@ -0,0 +1,35 @@ +/** + * Copyright 2012-2015 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.forged; + +import java.util.Set; + +public class ErroneousNonMappableSetTarget { + + private Set nonMappableSet; + + public Set getNonMappableSet() { + return nonMappableSet; + } + + public void setNonMappableSet(Set nonMappableSet) { + this.nonMappableSet = nonMappableSet; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/forged/Foo.java b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/Foo.java new file mode 100644 index 000000000..65f548e31 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/forged/Foo.java @@ -0,0 +1,27 @@ +/** + * Copyright 2012-2015 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.forged; + +/** + * + * @author Sjaak Derksen + */ +public class Foo { + +}