mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#604 Add import for property target type when using an update-method and no factory method is used to create a new instance
This commit is contained in:
parent
30c8b1eec0
commit
e177f25d63
@ -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() );
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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 );
|
||||
}
|
||||
|
||||
}
|
@ -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
|
||||
|
@ -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);
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user