mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#305 Fix for target-getter-only in combination with expressions or constants
This commit is contained in:
parent
5cd90ae6cf
commit
2cabfacbf4
@ -584,7 +584,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
parameter
|
parameter
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if ( Executables.isSetterMethod( targetAccessor ) ) {
|
else if ( Executables.isSetterMethod( targetAccessor ) ||
|
||||||
|
Executables.isGetterMethod( targetAccessor ) ) {
|
||||||
|
|
||||||
if ( !mapping.getConstant().isEmpty() ) {
|
if ( !mapping.getConstant().isEmpty() ) {
|
||||||
// its a constant
|
// its a constant
|
||||||
@ -961,7 +962,13 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
Type sourceType = typeFactory.getType( String.class );
|
Type sourceType = typeFactory.getType( String.class );
|
||||||
|
|
||||||
// target
|
// target
|
||||||
Type targetType = typeFactory.getSingleParameter( targetAccessor ).getType();
|
Type targetType;
|
||||||
|
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||||
|
targetType = typeFactory.getSingleParameter( targetAccessor ).getType();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
targetType = typeFactory.getReturnType( targetAccessor );
|
||||||
|
}
|
||||||
String targetPropertyName = Executables.getPropertyName( targetAccessor );
|
String targetPropertyName = Executables.getPropertyName( targetAccessor );
|
||||||
|
|
||||||
Assignment assignment = mappingResolver.getTargetAssignment(
|
Assignment assignment = mappingResolver.getTargetAssignment(
|
||||||
@ -981,6 +988,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
|
|
||||||
// target accessor is setter, so decorate assignment as setter
|
// target accessor is setter, so decorate assignment as setter
|
||||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
|
|
||||||
|
// wrap when dealing with getter only on target
|
||||||
|
if ( Executables.isGetterMethod( targetAccessor ) ) {
|
||||||
|
assignment = new GetterCollectionOrMapWrapper( assignment );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
messager.printMessage(
|
messager.printMessage(
|
||||||
@ -1005,12 +1017,23 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
*/
|
*/
|
||||||
private PropertyMapping getJavaExpressionMapping(SourceMethod method,
|
private PropertyMapping getJavaExpressionMapping(SourceMethod method,
|
||||||
String javaExpression,
|
String javaExpression,
|
||||||
ExecutableElement targetAcessor) {
|
ExecutableElement targetAccessor) {
|
||||||
|
|
||||||
Type targetType = typeFactory.getSingleParameter( targetAcessor ).getType();
|
|
||||||
Assignment assignment = AssignmentFactory.createSimple( javaExpression );
|
Assignment assignment = AssignmentFactory.createSimple( javaExpression );
|
||||||
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
assignment = new SetterWrapper( assignment, method.getThrownTypes() );
|
||||||
return new PropertyMapping( targetAcessor.getSimpleName().toString(), targetType, assignment );
|
|
||||||
|
Type targetType;
|
||||||
|
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||||
|
targetType = typeFactory.getSingleParameter( targetAccessor ).getType();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
targetType = typeFactory.getReturnType( targetAccessor );
|
||||||
|
|
||||||
|
// target accessor is getter, so wrap the setter in getter map/ collection handling
|
||||||
|
assignment = new GetterCollectionOrMapWrapper( assignment );
|
||||||
|
}
|
||||||
|
|
||||||
|
return new PropertyMapping( targetAccessor.getSimpleName().toString(), targetType, assignment );
|
||||||
}
|
}
|
||||||
|
|
||||||
private IterableMappingMethod getIterableMappingMethod(List<MapperReference> mapperReferences,
|
private IterableMappingMethod getIterableMappingMethod(List<MapperReference> mapperReferences,
|
||||||
|
@ -43,7 +43,7 @@ import static org.fest.assertions.Assertions.assertThat;
|
|||||||
public class SourceConstantsTest {
|
public class SourceConstantsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@IssueKey("187")
|
@IssueKey("187, 305")
|
||||||
@WithClasses({
|
@WithClasses({
|
||||||
Source.class,
|
Source.class,
|
||||||
Source2.class,
|
Source2.class,
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test.source.constants;
|
package org.mapstruct.ap.test.source.constants;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ public class Target {
|
|||||||
private int integerConstant;
|
private int integerConstant;
|
||||||
private Long longWrapperConstant;
|
private Long longWrapperConstant;
|
||||||
private Date dateConstant;
|
private Date dateConstant;
|
||||||
private List<String> nameConstants;
|
private List<String> nameConstants = new ArrayList<String>();
|
||||||
|
|
||||||
public String getPropertyThatShouldBeMapped() {
|
public String getPropertyThatShouldBeMapped() {
|
||||||
return propertyThatShouldBeMapped;
|
return propertyThatShouldBeMapped;
|
||||||
@ -77,8 +78,4 @@ public class Target {
|
|||||||
return nameConstants;
|
return nameConstants;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setNameConstants(List<String> nameConstants) {
|
|
||||||
this.nameConstants = nameConstants;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ package org.mapstruct.ap.test.source.expressions.java;
|
|||||||
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
|
import org.mapstruct.ap.test.source.expressions.java.mapper.TimeAndFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -134,4 +135,24 @@ public class JavaExpressionTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IssueKey( "305" )
|
||||||
|
@Test
|
||||||
|
@WithClasses({
|
||||||
|
SourceList.class,
|
||||||
|
TargetList.class,
|
||||||
|
SourceTargetListMapper.class
|
||||||
|
})
|
||||||
|
public void testGetterOnly() throws ParseException {
|
||||||
|
|
||||||
|
SourceList source = new SourceList();
|
||||||
|
source.setList( Arrays.asList( "test1" ) );
|
||||||
|
|
||||||
|
TargetList target = SourceTargetListMapper.INSTANCE.map( source );
|
||||||
|
assertThat( target ).isNotNull();
|
||||||
|
assertThat( target.getList() ).isEqualTo( Arrays.asList( "test2" ) );
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* 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.source.expressions.java;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class SourceList {
|
||||||
|
|
||||||
|
private List<String> list;
|
||||||
|
|
||||||
|
public List<String> getList() {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setList( List<String> list ) {
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
/**
|
||||||
|
* 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.source.expressions.java;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper( imports = { Arrays.class } )
|
||||||
|
public interface SourceTargetListMapper {
|
||||||
|
|
||||||
|
SourceTargetListMapper INSTANCE = Mappers.getMapper( SourceTargetListMapper.class );
|
||||||
|
|
||||||
|
@Mapping( target = "list", expression = "java(Arrays.asList(\"test2\"))" )
|
||||||
|
TargetList map( SourceList source );
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/**
|
||||||
|
* 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.source.expressions.java;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class TargetList {
|
||||||
|
|
||||||
|
private List<String> list = new ArrayList<String>();
|
||||||
|
|
||||||
|
public List<String> getList() {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user