mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#255 several sources does not combine with constants and expressions
This commit is contained in:
parent
c434e1dcd3
commit
683acdc7e3
@ -406,30 +406,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
// check if there's a mapping defined
|
||||
Mapping mapping = method.getMappingByTargetPropertyName( targetPropertyName );
|
||||
String dateFormat = null;
|
||||
|
||||
String sourcePropertyName;
|
||||
if ( mapping != null ) {
|
||||
dateFormat = mapping.getDateFormat();
|
||||
|
||||
if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||
|
||||
// check constants first
|
||||
if ( !mapping.getConstant().isEmpty() ) {
|
||||
return getConstantMapping(
|
||||
mapperReferences,
|
||||
methods,
|
||||
method,
|
||||
"\"" + mapping.getConstant() + "\"",
|
||||
targetAccessor,
|
||||
dateFormat
|
||||
);
|
||||
}
|
||||
|
||||
if ( !mapping.getJavaExpression().isEmpty() ) {
|
||||
return getJavaExpressionMapping( method, mapping.getJavaExpression(), targetAccessor );
|
||||
}
|
||||
}
|
||||
|
||||
sourcePropertyName = mapping.getSourcePropertyName();
|
||||
}
|
||||
else {
|
||||
@ -535,7 +514,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
}
|
||||
|
||||
PropertyMapping propertyMapping = null;
|
||||
if ( mapping != null && mapping.getSourceParameterName() != null ) {
|
||||
if ( mapping != null ) {
|
||||
if ( mapping.getSourceParameterName() != null ) {
|
||||
// this is a parameterized property, so sourceParameter.property
|
||||
Parameter parameter = method.getSourceParameter( mapping.getSourceParameterName() );
|
||||
propertyMapping = getPropertyMapping(
|
||||
mapperReferences,
|
||||
@ -546,6 +527,30 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
||||
parameter
|
||||
);
|
||||
}
|
||||
else if ( Executables.isSetterMethod( targetAccessor ) ) {
|
||||
|
||||
if ( !mapping.getConstant().isEmpty() ) {
|
||||
// its a constant
|
||||
propertyMapping = getConstantMapping(
|
||||
mapperReferences,
|
||||
methods,
|
||||
method,
|
||||
"\"" + mapping.getConstant() + "\"",
|
||||
targetAccessor,
|
||||
mapping.getDateFormat()
|
||||
);
|
||||
}
|
||||
|
||||
else if ( !mapping.getJavaExpression().isEmpty() ) {
|
||||
// its an expression
|
||||
propertyMapping = getJavaExpressionMapping(
|
||||
method,
|
||||
mapping.getJavaExpression(),
|
||||
targetAccessor
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( propertyMapping == null ) {
|
||||
for ( Parameter sourceParameter : method.getSourceParameters() ) {
|
||||
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 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.constants;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Source1 {
|
||||
|
||||
private String someProp;
|
||||
|
||||
public String getSomeProp() {
|
||||
return someProp;
|
||||
}
|
||||
|
||||
public void setSomeProp( String someProp ) {
|
||||
this.someProp = someProp;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 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.constants;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Source2 {
|
||||
|
||||
private String anotherProp;
|
||||
|
||||
public String getAnotherProp() {
|
||||
return anotherProp;
|
||||
}
|
||||
|
||||
public void setAnotherProp( String anotherProp ) {
|
||||
this.anotherProp = anotherProp;
|
||||
}
|
||||
|
||||
}
|
@ -46,6 +46,7 @@ public class SourceConstantsTest {
|
||||
@IssueKey("187")
|
||||
@WithClasses({
|
||||
Source.class,
|
||||
Source2.class,
|
||||
Target.class,
|
||||
SourceTargetMapper.class,
|
||||
StringListMapper.class
|
||||
@ -182,6 +183,29 @@ public class SourceConstantsTest {
|
||||
public void errorOnNeitherSourceNorExpression() throws ParseException {
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@IssueKey("255")
|
||||
@WithClasses({
|
||||
Source1.class,
|
||||
Source2.class,
|
||||
Target2.class,
|
||||
SourceTargetMapperSeveralSources.class
|
||||
})
|
||||
public void shouldMapSameSourcePropertyToSeveralTargetPropertiesFromSeveralSources() throws ParseException {
|
||||
Source1 source1 = new Source1();
|
||||
source1.setSomeProp( "someProp" );
|
||||
|
||||
Source2 source2 = new Source2();
|
||||
source2.setAnotherProp( "anotherProp" );
|
||||
Target2 target = SourceTargetMapperSeveralSources.INSTANCE.sourceToTarget( source1, source2 );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getSomeProp() ).isEqualTo( "someProp" );
|
||||
assertThat( target.getAnotherProp() ).isEqualTo( "anotherProp" );
|
||||
assertThat( target.getSomeConstant() ).isEqualTo( "stringConstant" );
|
||||
}
|
||||
|
||||
private Date getDate(String format, String date) throws ParseException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat( format );
|
||||
Date result = dateFormat.parse( date );
|
||||
|
@ -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.source.constants;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper
|
||||
public interface SourceTargetMapperSeveralSources {
|
||||
|
||||
SourceTargetMapperSeveralSources INSTANCE = Mappers.getMapper( SourceTargetMapperSeveralSources.class );
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "someProp", target = "someProp" ),
|
||||
@Mapping(source = "anotherProp", target = "anotherProp" ),
|
||||
@Mapping(target = "someConstant", constant = "stringConstant"),
|
||||
})
|
||||
Target2 sourceToTarget(Source1 s1, Source2 s2);
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 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.constants;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Target2 {
|
||||
|
||||
private String someProp;
|
||||
private String anotherProp;
|
||||
private String someConstant;
|
||||
|
||||
public String getSomeProp() {
|
||||
return someProp;
|
||||
}
|
||||
|
||||
public void setSomeProp( String someProp ) {
|
||||
this.someProp = someProp;
|
||||
}
|
||||
|
||||
public String getAnotherProp() {
|
||||
return anotherProp;
|
||||
}
|
||||
|
||||
public void setAnotherProp( String anotherProp ) {
|
||||
this.anotherProp = anotherProp;
|
||||
}
|
||||
|
||||
public String getSomeConstant() {
|
||||
return someConstant;
|
||||
}
|
||||
|
||||
public void setSomeConstant( String someConstant ) {
|
||||
this.someConstant = someConstant;
|
||||
}
|
||||
|
||||
}
|
@ -28,15 +28,17 @@ import org.mapstruct.ap.testutil.WithClasses;
|
||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
import org.mapstruct.ap.testutil.IssueKey;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class, TimeAndFormat.class })
|
||||
@WithClasses({ Source.class, Target.class, TimeAndFormat.class })
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
public class JavaExpressionTest {
|
||||
|
||||
@Test
|
||||
@WithClasses({ SourceTargetMapper.class })
|
||||
public void testJavaExpressionInsertion() throws ParseException {
|
||||
Source source = new Source();
|
||||
String format = "dd-MM-yyyy,hh:mm:ss";
|
||||
@ -50,8 +52,33 @@ public class JavaExpressionTest {
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getTimeAndFormat().getTime() ).isEqualTo( time );
|
||||
assertThat( target.getTimeAndFormat().getFormat() ).isEqualTo( format );
|
||||
assertThat( target.getAnotherProp() ).isNull();
|
||||
}
|
||||
|
||||
@IssueKey( "255" )
|
||||
@Test
|
||||
@WithClasses({ SourceTargetMapperSeveralSources.class, Source2.class })
|
||||
public void testJavaExpressionInsertionWithSeveralSources() throws ParseException {
|
||||
Source source1 = new Source();
|
||||
String format = "dd-MM-yyyy,hh:mm:ss";
|
||||
Date time = getTime( format, "09-01-2014,01:35:03" );
|
||||
|
||||
source1.setFormat( format );
|
||||
source1.setTime( time );
|
||||
|
||||
Source2 source2 = new Source2();
|
||||
source2.setAnotherProp( "test" );
|
||||
|
||||
|
||||
Target target = SourceTargetMapperSeveralSources.INSTANCE.sourceToTarget( source1, source2 );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getTimeAndFormat().getTime() ).isEqualTo( time );
|
||||
assertThat( target.getTimeAndFormat().getFormat() ).isEqualTo( format );
|
||||
assertThat( target.getAnotherProp() ).isEqualTo( "test" );
|
||||
}
|
||||
|
||||
|
||||
private Date getTime(String format, String date) throws ParseException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat( format );
|
||||
Date result = dateFormat.parse( date );
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
public class Source2 {
|
||||
|
||||
private String anotherProp;
|
||||
|
||||
public String getAnotherProp() {
|
||||
return anotherProp;
|
||||
}
|
||||
|
||||
public void setAnotherProp( String anotherProp ) {
|
||||
this.anotherProp = anotherProp;
|
||||
}
|
||||
|
||||
}
|
@ -20,6 +20,7 @@ package org.mapstruct.ap.test.source.expressions.java;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
@ -31,7 +32,10 @@ public interface SourceTargetMapper {
|
||||
|
||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||
|
||||
@Mappings( {
|
||||
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
|
||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))")
|
||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
||||
@Mapping( target = "anotherProp", ignore = true )
|
||||
} )
|
||||
Target sourceToTarget(Source s);
|
||||
}
|
||||
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* 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 org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Sjaak Derksen
|
||||
*/
|
||||
@Mapper
|
||||
public interface SourceTargetMapperSeveralSources {
|
||||
|
||||
|
||||
SourceTargetMapperSeveralSources INSTANCE = Mappers.getMapper( SourceTargetMapperSeveralSources.class );
|
||||
|
||||
@Mappings( {
|
||||
@Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
|
||||
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
|
||||
@Mapping( source = "anotherProp", target = "anotherProp" )
|
||||
} )
|
||||
Target sourceToTarget(Source s, Source2 s1);
|
||||
}
|
@ -24,11 +24,20 @@ package org.mapstruct.ap.test.source.expressions.java;
|
||||
public class Target {
|
||||
|
||||
private TimeAndFormat timeAndFormat;
|
||||
private String anotherProp;
|
||||
|
||||
public TimeAndFormat getTimeAndFormat() {
|
||||
return timeAndFormat;
|
||||
}
|
||||
|
||||
public String getAnotherProp() {
|
||||
return anotherProp;
|
||||
}
|
||||
|
||||
public void setAnotherProp( String anotherProp ) {
|
||||
this.anotherProp = anotherProp;
|
||||
}
|
||||
|
||||
public void setTimeAndFormat(TimeAndFormat timeAndFormat) {
|
||||
this.timeAndFormat = timeAndFormat;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user