#255 several sources does not combine with constants and expressions

This commit is contained in:
sjaakd 2014-07-02 21:00:24 +02:00
parent c434e1dcd3
commit 683acdc7e3
11 changed files with 353 additions and 34 deletions

View File

@ -406,30 +406,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
// check if there's a mapping defined // check if there's a mapping defined
Mapping mapping = method.getMappingByTargetPropertyName( targetPropertyName ); Mapping mapping = method.getMappingByTargetPropertyName( targetPropertyName );
String dateFormat = null; String dateFormat = null;
String sourcePropertyName; String sourcePropertyName;
if ( mapping != null ) { if ( mapping != null ) {
dateFormat = mapping.getDateFormat(); 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(); sourcePropertyName = mapping.getSourcePropertyName();
} }
else { else {
@ -535,7 +514,9 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
} }
PropertyMapping propertyMapping = null; 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() ); Parameter parameter = method.getSourceParameter( mapping.getSourceParameterName() );
propertyMapping = getPropertyMapping( propertyMapping = getPropertyMapping(
mapperReferences, mapperReferences,
@ -546,6 +527,30 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
parameter 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 ) { if ( propertyMapping == null ) {
for ( Parameter sourceParameter : method.getSourceParameters() ) { for ( Parameter sourceParameter : method.getSourceParameters() ) {

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -46,6 +46,7 @@ public class SourceConstantsTest {
@IssueKey("187") @IssueKey("187")
@WithClasses({ @WithClasses({
Source.class, Source.class,
Source2.class,
Target.class, Target.class,
SourceTargetMapper.class, SourceTargetMapper.class,
StringListMapper.class StringListMapper.class
@ -182,6 +183,29 @@ public class SourceConstantsTest {
public void errorOnNeitherSourceNorExpression() throws ParseException { 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 { private Date getDate(String format, String date) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat( format ); SimpleDateFormat dateFormat = new SimpleDateFormat( format );
Date result = dateFormat.parse( date ); Date result = dateFormat.parse( date );

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -28,15 +28,17 @@ import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Assertions.assertThat;
import org.mapstruct.ap.testutil.IssueKey;
/** /**
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class, TimeAndFormat.class }) @WithClasses({ Source.class, Target.class, TimeAndFormat.class })
@RunWith(AnnotationProcessorTestRunner.class) @RunWith(AnnotationProcessorTestRunner.class)
public class JavaExpressionTest { public class JavaExpressionTest {
@Test @Test
@WithClasses({ SourceTargetMapper.class })
public void testJavaExpressionInsertion() throws ParseException { public void testJavaExpressionInsertion() throws ParseException {
Source source = new Source(); Source source = new Source();
String format = "dd-MM-yyyy,hh:mm:ss"; String format = "dd-MM-yyyy,hh:mm:ss";
@ -50,8 +52,33 @@ public class JavaExpressionTest {
assertThat( target ).isNotNull(); assertThat( target ).isNotNull();
assertThat( target.getTimeAndFormat().getTime() ).isEqualTo( time ); assertThat( target.getTimeAndFormat().getTime() ).isEqualTo( time );
assertThat( target.getTimeAndFormat().getFormat() ).isEqualTo( format ); 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 { private Date getTime(String format, String date) throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat( format ); SimpleDateFormat dateFormat = new SimpleDateFormat( format );
Date result = dateFormat.parse( date ); Date result = dateFormat.parse( date );

View File

@ -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;
}
}

View File

@ -20,6 +20,7 @@ package org.mapstruct.ap.test.source.expressions.java;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mapping; import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers; import org.mapstruct.factory.Mappers;
/** /**
@ -31,7 +32,10 @@ public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mapping(target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java." @Mappings( {
+ "TimeAndFormat( s.getTime(), s.getFormat() ))") @Mapping( target = "timeAndFormat", expression = "java( new org.mapstruct.ap.test.source.expressions.java."
+ "TimeAndFormat( s.getTime(), s.getFormat() ))" ),
@Mapping( target = "anotherProp", ignore = true )
} )
Target sourceToTarget(Source s); Target sourceToTarget(Source s);
} }

View File

@ -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);
}

View File

@ -24,11 +24,20 @@ package org.mapstruct.ap.test.source.expressions.java;
public class Target { public class Target {
private TimeAndFormat timeAndFormat; private TimeAndFormat timeAndFormat;
private String anotherProp;
public TimeAndFormat getTimeAndFormat() { public TimeAndFormat getTimeAndFormat() {
return timeAndFormat; return timeAndFormat;
} }
public String getAnotherProp() {
return anotherProp;
}
public void setAnotherProp( String anotherProp ) {
this.anotherProp = anotherProp;
}
public void setTimeAndFormat(TimeAndFormat timeAndFormat) { public void setTimeAndFormat(TimeAndFormat timeAndFormat) {
this.timeAndFormat = timeAndFormat; this.timeAndFormat = timeAndFormat;
} }