From e177f25d639035af036b38ee256018622c5560fe Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Tue, 21 Jul 2015 21:01:34 +0200 Subject: [PATCH] #604 Add import for property target type when using an update-method and no factory method is used to create a new instance --- .../ap/internal/model/PropertyMapping.java | 6 +-- .../model/assignment/UpdateWrapper.java | 16 ++++++- .../ap/test/updatemethods/BossDto.java | 44 +++++++++++++++++++ .../ap/test/updatemethods/BossEntity.java | 44 +++++++++++++++++++ .../ConstructableDepartmentEntity.java | 31 +++++++++++++ .../selection/ExternalSelectionTest.java | 24 +++++++++- .../selection/OrganizationMapper3.java | 38 ++++++++++++++++ 7 files changed, 197 insertions(+), 6 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossDto.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossEntity.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/updatemethods/ConstructableDepartmentEntity.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3.java 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 63a9892ee..37e55cd86 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 @@ -256,7 +256,7 @@ public class PropertyMapping extends ModelElement { Assignment factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null ); result = new UpdateWrapper( result, method.getThrownTypes(), factoryMethod, - targetType.getImplementationType() ); + targetType ); } else { result = new SetterWrapper( result, method.getThrownTypes() ); @@ -330,7 +330,7 @@ public class PropertyMapping extends ModelElement { Assignment factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null ); result = new UpdateWrapper( result, method.getThrownTypes(), factoryMethod, - targetType.getImplementationType() ); + targetType ); } else { // wrap the assignment in the setter method @@ -623,7 +623,7 @@ public class PropertyMapping extends ModelElement { Assignment factoryMethod = ctx.getMappingResolver().getFactoryMethod( method, targetType, null, null ); assignment = new UpdateWrapper( assignment, method.getThrownTypes(), factoryMethod, - targetType.getImplementationType() ); + targetType ); } else { assignment = new SetterWrapper( assignment, method.getThrownTypes() ); diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.java b/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.java index dd70f119a..f2fd6833c 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/assignment/UpdateWrapper.java @@ -41,7 +41,21 @@ public class UpdateWrapper extends AssignmentWrapper { super( decoratedAssignment ); this.thrownTypesToExclude = thrownTypesToExclude; this.factoryMethod = factoryMethod; - this.targetImplementationType = targetImplementationType; + this.targetImplementationType = determineImplType( factoryMethod, targetImplementationType ); + } + + private static Type determineImplType(Assignment factoryMethod, Type targetType) { + if ( targetType.getImplementationType() != null ) { + // it's probably a collection or something + return targetType.getImplementationType(); + } + + if ( factoryMethod == null ) { + // no factory method means we create a new instance ourself and thus need to import the type + return targetType; + } + + return null; } @Override diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossDto.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossDto.java new file mode 100644 index 000000000..18a64125b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossDto.java @@ -0,0 +1,44 @@ +/** + * 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.updatemethods; + +/** + * @author Andreas Gudian + * + */ +public class BossDto { + private String name; + private DepartmentDto department; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public DepartmentDto getDepartment() { + return department; + } + + public void setDepartment(DepartmentDto department) { + this.department = department; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossEntity.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossEntity.java new file mode 100644 index 000000000..38f7296e9 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/BossEntity.java @@ -0,0 +1,44 @@ +/** + * 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.updatemethods; + +/** + * @author Andreas Gudian + * + */ +public class BossEntity { + private String name; + private ConstructableDepartmentEntity department; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ConstructableDepartmentEntity getDepartment() { + return department; + } + + public void setDepartment(ConstructableDepartmentEntity department) { + this.department = department; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/ConstructableDepartmentEntity.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/ConstructableDepartmentEntity.java new file mode 100644 index 000000000..c1f576c8c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/ConstructableDepartmentEntity.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.updatemethods; + +/** + * @author Andreas Gudian + * + */ +public class ConstructableDepartmentEntity extends DepartmentEntity { + + public ConstructableDepartmentEntity() { + super( null ); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java index 0661226d8..7f8ccb0b1 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/ExternalSelectionTest.java @@ -21,11 +21,14 @@ package org.mapstruct.ap.test.updatemethods.selection; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import static org.fest.assertions.Assertions.assertThat; + import org.junit.Test; import org.junit.runner.RunWith; +import org.mapstruct.ap.test.updatemethods.BossDto; +import org.mapstruct.ap.test.updatemethods.BossEntity; import org.mapstruct.ap.test.updatemethods.CompanyDto; import org.mapstruct.ap.test.updatemethods.CompanyEntity; +import org.mapstruct.ap.test.updatemethods.ConstructableDepartmentEntity; import org.mapstruct.ap.test.updatemethods.DepartmentDto; import org.mapstruct.ap.test.updatemethods.DepartmentEntity; import org.mapstruct.ap.test.updatemethods.DepartmentEntityFactory; @@ -37,6 +40,8 @@ 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; + /** * * @author Sjaak Derksen @@ -56,7 +61,10 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; EmployeeDto.class, EmployeeEntity.class, SecretaryDto.class, - SecretaryEntity.class + SecretaryEntity.class, + BossDto.class, + BossEntity.class, + ConstructableDepartmentEntity.class }) public class ExternalSelectionTest { @@ -71,6 +79,18 @@ public class ExternalSelectionTest { OrganizationMapper1.INSTANCE.toCompanyEntity( dto, entity ); } + @Test + @WithClasses({ + OrganizationMapper3.class + }) + @IssueKey("604") + public void shouldSelectGeneratedExternalMapperWithImportForPropertyType() { + + BossEntity entity = new BossEntity(); + BossDto dto = new BossDto(); + OrganizationMapper3.INSTANCE.toBossEntity( dto, entity ); + } + @Test @WithClasses({ OrganizationMapper2.class diff --git a/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3.java b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3.java new file mode 100644 index 000000000..032451427 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/updatemethods/selection/OrganizationMapper3.java @@ -0,0 +1,38 @@ +/** + * 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.updatemethods.selection; + +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.ap.test.updatemethods.BossDto; +import org.mapstruct.ap.test.updatemethods.BossEntity; +import org.mapstruct.factory.Mappers; + +/** + * + * @author Sjaak Derksen + */ +@Mapper(uses = { ExternalMapper.class }) +public interface OrganizationMapper3 { + + OrganizationMapper3 INSTANCE = Mappers.getMapper( OrganizationMapper3.class ); + + void toBossEntity(BossDto dto, @MappingTarget BossEntity entity); + +}