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 a2903c303..7ba07225f 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -30,9 +30,13 @@ import java.util.Set; import javax.annotation.processing.Messager; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.ExecutableType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.ElementFilter; import javax.lang.model.util.Elements; +import javax.lang.model.util.Types; import javax.tools.Diagnostic.Kind; import org.mapstruct.ap.MapperPrism; @@ -71,6 +75,7 @@ public class MapperCreationProcessor implements ModelElementProcessor sourceModel) { this.elementUtils = context.getElementUtils(); + this.typeUtils = context.getTypeUtils(); this.messager = context.getMessager(); this.options = context.getOptions(); @@ -554,18 +560,42 @@ public class MapperCreationProcessor implements ModelElementProcessor + *
  • the source type is assignable to the target type
  • + *
  • a mapping method exists
  • + *
  • a built-in conversion exists
  • + *
  • the property is of a collection or map type and the constructor of the target type (either itself or its + * implementation type) accepts the source type.
  • + * * * @param method The mapping method owning the property mapping. * @param property The property mapping to check. */ private void reportErrorIfPropertyCanNotBeMapped(Method method, PropertyMapping property) { + boolean collectionOrMapTargetTypeHasCompatibleConstructor = false; + + if ( property.getTargetType().isCollectionType() || property.getTargetType().isMapType() ) { + if ( property.getTargetType().getImplementationType() != null ) { + collectionOrMapTargetTypeHasCompatibleConstructor = hasCompatibleConstructor( + property.getSourceType(), + property.getTargetType().getImplementationType() + ); + } + else { + collectionOrMapTargetTypeHasCompatibleConstructor = hasCompatibleConstructor( + property.getSourceType(), + property.getTargetType() + ); + } + } + if ( property.getSourceType().isAssignableTo( property.getTargetType() ) || property.getMappingMethod() != null || property.getConversion() != null || - property.getTargetType().getImplementationType() != null ) { + ( ( property.getTargetType().isCollectionType() || property.getTargetType().isMapType() ) && + collectionOrMapTargetTypeHasCompatibleConstructor ) ) { return; } @@ -581,4 +611,39 @@ public class MapperCreationProcessor implements ModelElementProcessor targetTypeConstructors = ElementFilter.constructorsIn( + targetType.getTypeElement() + .getEnclosedElements() + ); + + for ( ExecutableElement constructor : targetTypeConstructors ) { + if ( constructor.getParameters().size() != 1 ) { + continue; + } + + //get the constructor resolved against the type arguments of specific target type + ExecutableType typedConstructor = (ExecutableType) typeUtils.asMemberOf( + (DeclaredType) targetType.getTypeMirror(), constructor + ); + + if ( typeUtils.isAssignable( + sourceType.getTypeMirror(), + typedConstructor.getParameterTypes().iterator().next() + ) ) { + return true; + } + } + + return false; + } } diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/AnotherTarget.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/AnotherTarget.java new file mode 100644 index 000000000..fadb4a089 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/AnotherTarget.java @@ -0,0 +1,34 @@ +/** + * Copyright 2012-2013 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.erroneous; + +import java.util.Map; + +public class AnotherTarget { + + private Map barMap; + + public Map getBarMap() { + return barMap; + } + + public void setBarMap(Map barMap) { + this.barMap = barMap; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMapper.java new file mode 100644 index 000000000..83f6ff6ea --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMapper.java @@ -0,0 +1,27 @@ +/** + * Copyright 2012-2013 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.erroneous; + +import org.mapstruct.Mapper; + +@Mapper +public interface ErroneousCollectionMapper { + + Target sourceToTarget(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java new file mode 100644 index 000000000..0d57916bb --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionMappingTest.java @@ -0,0 +1,86 @@ +/** + * Copyright 2012-2013 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.erroneous; + +import javax.tools.Diagnostic.Kind; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.MapperTestBase; +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.testng.annotations.Test; + +/** + * Test for illegal mappings between collection types, iterable and non-iterable types etc. + * + * @author Gunnar Morling + */ +public class ErroneousCollectionMappingTest extends MapperTestBase { + + @Test + @IssueKey("6") + @WithClasses({ ErroneousCollectionToNonCollectionMapper.class }) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic(type = ErroneousCollectionToNonCollectionMapper.class, + kind = Kind.ERROR, + line = 28, + messageRegExp = "Can't generate mapping method from iterable type to non-iterable type"), + @Diagnostic(type = ErroneousCollectionToNonCollectionMapper.class, + kind = Kind.ERROR, + line = 30, + messageRegExp = "Can't generate mapping method from non-iterable type to iterable type") + } + ) + public void shouldFailToGenerateImplementationBetweenCollectionAndNonCollection() { + } + + @Test + @WithClasses({ ErroneousCollectionMapper.class, Source.class, Target.class }) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic(type = ErroneousCollectionMapper.class, + kind = Kind.ERROR, + line = 26, + messageRegExp = "Can't map property \"java\\.util\\.Set fooSet\" to" + + " \"java\\.util\\.Set fooSet\""), + } + ) + public void shouldFailToGenerateImplementationDueToDifferentlyParameterizedCollections() { + } + + @Test + @WithClasses({ ErroneousMapMapper.class, Source.class, AnotherTarget.class }) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic(type = ErroneousMapMapper.class, + kind = Kind.ERROR, + line = 26, + messageRegExp = "Can't map property \"java\\.util\\.Map" + + " barMap\" to \"java.util.Map barMap\"") + } + ) + public void shouldFailToGenerateImplementationDueToDifferentlyParameterizedMaps() { + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErronuousMapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionToNonCollectionMapper.java similarity index 94% rename from processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErronuousMapper.java rename to processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionToNonCollectionMapper.java index 21e0b24bd..e58011085 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErronuousMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousCollectionToNonCollectionMapper.java @@ -23,7 +23,7 @@ import java.util.Set; import org.mapstruct.Mapper; @Mapper -public interface ErronuousMapper { +public interface ErroneousCollectionToNonCollectionMapper { Integer stringSetToInteger(Set strings); diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousMapMapper.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousMapMapper.java new file mode 100644 index 000000000..c3a0585f2 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErroneousMapMapper.java @@ -0,0 +1,27 @@ +/** + * Copyright 2012-2013 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.erroneous; + +import org.mapstruct.Mapper; + +@Mapper +public interface ErroneousMapMapper { + + AnotherTarget sourceToTarget(Source source); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErronuousCollectionMappingTest.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErronuousCollectionMappingTest.java deleted file mode 100644 index 8e061927b..000000000 --- a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/ErronuousCollectionMappingTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2012-2013 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.erroneous; - -import javax.tools.Diagnostic.Kind; - -import org.mapstruct.ap.testutil.IssueKey; -import org.mapstruct.ap.testutil.MapperTestBase; -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.testng.annotations.Test; - -/** - * Test for illegal mappings between iterable and non-iterable types. - * - * @author Gunnar Morling - */ -@WithClasses({ ErronuousMapper.class }) -public class ErronuousCollectionMappingTest extends MapperTestBase { - - @Test - @IssueKey("6") - @ExpectedCompilationOutcome( - value = CompilationResult.FAILED, - diagnostics = { - @Diagnostic(type = ErronuousMapper.class, - kind = Kind.ERROR, - line = 28, - messageRegExp = "Can't generate mapping method from iterable type to non-iterable type\\."), - @Diagnostic(type = ErronuousMapper.class, - kind = Kind.ERROR, - line = 30, - messageRegExp = "Can't generate mapping method from non-iterable type to iterable type\\.") - } - ) - public void shouldFailToGenerateMappingFromListToString() { - } -} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/Source.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/Source.java new file mode 100644 index 000000000..5c64ebc44 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/Source.java @@ -0,0 +1,45 @@ +/** + * Copyright 2012-2013 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.erroneous; + +import java.util.Map; +import java.util.Set; + +public class Source { + + private Set fooSet; + + private Map barMap; + + public Set getFooSet() { + return fooSet; + } + + public void setFooSet(Set fooSet) { + this.fooSet = fooSet; + } + + public Map getBarMap() { + return barMap; + } + + public void setBarMap(Map barMap) { + this.barMap = barMap; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/Target.java b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/Target.java new file mode 100644 index 000000000..7a0f184df --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/collection/erroneous/Target.java @@ -0,0 +1,34 @@ +/** + * Copyright 2012-2013 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.erroneous; + +import java.util.Set; + +public class Target { + + private Set fooSet; + + public Set getFooSet() { + return fooSet; + } + + public void setFooSet(Set fooSet) { + this.fooSet = fooSet; + } +}