From 668f66eb735909a5385a1e0162702ca29e78ff7e Mon Sep 17 00:00:00 2001 From: sjaakd Date: Mon, 5 Jan 2015 14:59:18 +0100 Subject: [PATCH] #405 Fixing factory methods, not using raw type for @TargetType and superfluous implementation classes imports --- .../org/mapstruct/ap/model/GeneratedType.java | 2 - .../ap/model/IterableMappingMethod.java | 4 +- .../mapstruct/ap/model/MapMappingMethod.java | 3 ++ .../org/mapstruct/ap/model/MappingMethod.java | 1 - ...pstruct.ap.model.IterableMappingMethod.ftl | 2 +- ...rg.mapstruct.ap.model.MapMappingMethod.ftl | 2 +- ...org.mapstruct.ap.model.MethodReference.ftl | 2 +- .../ap/test/bugs/_405/EntityFactory.java | 29 +++++++++++++ .../ap/test/bugs/_405/Issue405Test.java | 40 +++++++++++++++++ .../mapstruct/ap/test/bugs/_405/People.java | 43 +++++++++++++++++++ .../mapstruct/ap/test/bugs/_405/Person.java | 43 +++++++++++++++++++ .../ap/test/bugs/_405/PersonMapper.java | 42 ++++++++++++++++++ 12 files changed, 206 insertions(+), 7 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_405/EntityFactory.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Issue405Test.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_405/People.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Person.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_405/PersonMapper.java diff --git a/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java b/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java index 42e852d16..843b67597 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java +++ b/processor/src/main/java/org/mapstruct/ap/model/GeneratedType.java @@ -164,8 +164,6 @@ public abstract class GeneratedType extends ModelElement { } } - addWithDependents( collection, typeToAdd.getImplementationType() ); - for ( Type type : typeToAdd.getTypeParameters() ) { addWithDependents( collection, type ); } 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 28c72c66a..0f802136b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -166,7 +166,9 @@ public class IterableMappingMethod extends MappingMethod { if ( elementAssignment != null ) { types.addAll( elementAssignment.getImportTypes() ); } - + if ( factoryMethod == null ) { + types.addAll( getReturnType().getImportTypes() ); + } return types; } 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 20ee546b8..66283a85d 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MapMappingMethod.java @@ -195,6 +195,9 @@ public class MapMappingMethod extends MappingMethod { if ( valueAssignment != null ) { types.addAll( valueAssignment.getImportTypes() ); } + if ( factoryMethod == null ) { + types.addAll( getReturnType().getImportTypes() ); + } return types; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java index e09160f31..fb67cf9cf 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/MappingMethod.java @@ -111,7 +111,6 @@ public abstract class MappingMethod extends ModelElement { } types.add( getReturnType() ); - types.addAll( getReturnType().getImportTypes() ); types.addAll( thrownTypes ); return types; } diff --git a/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl index ced6f2d23..d9f119e4c 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.IterableMappingMethod.ftl @@ -107,7 +107,7 @@ <#macro iterableCreation> <@compress single_line=true> <#if factoryMethod??> - <@includeModel object=factoryMethod/> + <@includeModel object=factoryMethod targetType=resultType raw=true/> <#else> new <#if resultType.implementationType??> diff --git a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl index e55fd234a..4819128b1 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MapMappingMethod.ftl @@ -68,7 +68,7 @@ <#macro returnObjectCreation> <@compress single_line=true> <#if factoryMethod??> - <@includeModel object=factoryMethod/> + <@includeModel object=factoryMethod targetType=resultType raw=true/> <#else> new <#if resultType.implementationType??> diff --git a/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl b/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl index afd583f31..bbb6c2637 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.MethodReference.ftl @@ -25,7 +25,7 @@ <#list parameters as param> <#if param.targetType> <#-- a class is passed on for casting, see @TargetType --> - <@includeModel object=ext.targetType/>.class + <@includeModel object=ext.targetType raw=true/>.class <#else> <@includeModel object=assignment targetType=singleSourceParameterType raw=ext.raw/> diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/EntityFactory.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/EntityFactory.java new file mode 100644 index 000000000..eb93dcbe0 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/EntityFactory.java @@ -0,0 +1,29 @@ +/** + * 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.bugs._405; + + +import org.mapstruct.TargetType; + +public class EntityFactory { + + public T createEntity(@TargetType Class entityClass) { + return null; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Issue405Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Issue405Test.java new file mode 100644 index 000000000..29223400b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Issue405Test.java @@ -0,0 +1,40 @@ +/** + * 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.bugs._405; + +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; + +/** + * Reproducer for https://github.com/mapstruct/mapstruct/issues/405. + * + * @author Sjaak Derksen + */ +@IssueKey( "405" ) +@RunWith(AnnotationProcessorTestRunner.class) +public class Issue405Test { + + @Test + @WithClasses( { EntityFactory.class, Person.class, People.class, PersonMapper.class } ) + public void shouldGenerateFactoryCorrectMethodForIterables() { + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/People.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/People.java new file mode 100644 index 000000000..0b80a4e03 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/People.java @@ -0,0 +1,43 @@ +/** + * 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.bugs._405; + + +import java.util.List; + +public class People { + private String name; + private List children; + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Person.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Person.java new file mode 100644 index 000000000..ab1fae47b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/Person.java @@ -0,0 +1,43 @@ +/** + * 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.bugs._405; + + +import java.util.List; + +public class Person { + private String id; + private List children; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/PersonMapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/PersonMapper.java new file mode 100644 index 000000000..00e5251ae --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_405/PersonMapper.java @@ -0,0 +1,42 @@ +/** + * 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.bugs._405; + +import java.util.List; +import java.util.Map; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +@Mapper(uses = {EntityFactory.class } ) +public abstract class PersonMapper { + + public static final PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class ); + + @Mappings( { + @Mapping( source = "id", target = "name" ) } + ) + abstract People personToPeople(Person person); + + abstract List personListToPeopleList(List children); + + abstract Map personListToPeopleMap(Map children); + +}