#94 Allowing to map the same source property to several target properties

This commit is contained in:
sjaakd 2014-01-19 23:45:39 +01:00 committed by Gunnar Morling
parent cd058663f4
commit e82277e0fc
10 changed files with 381 additions and 100 deletions

View File

@ -18,7 +18,9 @@
*/ */
package org.mapstruct.ap.model.source; package org.mapstruct.ap.model.source;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.AnnotationValue;
@ -44,11 +46,14 @@ public class Mapping {
private final AnnotationValue sourceAnnotationValue; private final AnnotationValue sourceAnnotationValue;
private final AnnotationValue targetAnnotationValue; private final AnnotationValue targetAnnotationValue;
public static Map<String, Mapping> fromMappingsPrism(MappingsPrism mappingsAnnotation, Element element) { public static Map<String, List<Mapping>> fromMappingsPrism(MappingsPrism mappingsAnnotation, Element element) {
Map<String, Mapping> mappings = new HashMap<String, Mapping>(); Map<String, List<Mapping>> mappings = new HashMap<String, List<Mapping>>();
for ( MappingPrism mapping : mappingsAnnotation.value() ) { for ( MappingPrism mapping : mappingsAnnotation.value() ) {
mappings.put( mapping.source(), fromMappingPrism( mapping, element ) ); if (!mappings.containsKey( mapping.source())) {
mappings.put( mapping.source(), new ArrayList<Mapping>() );
}
mappings.get( mapping.source() ).add( fromMappingPrism( mapping, element ) );
} }
return mappings; return mappings;

View File

@ -43,25 +43,17 @@ public class Method {
private final List<Parameter> parameters; private final List<Parameter> parameters;
private final Type returnType; private final Type returnType;
private Map<String, Mapping> mappings; private Map<String, List<Mapping>> mappings;
private IterableMapping iterableMapping; private IterableMapping iterableMapping;
private MapMapping mapMapping; private MapMapping mapMapping;
private final Parameter targetParameter; private final Parameter targetParameter;
public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters, public static Method forMethodRequiringImplementation(ExecutableElement executable, List<Parameter> parameters,
Type returnType, Map<String, Mapping> mappings, Type returnType, Map<String, List<Mapping>> mappings,
IterableMapping iterableMapping, MapMapping mapMapping) { IterableMapping iterableMapping, MapMapping mapMapping) {
return new Method( return new Method( null, executable, parameters, returnType, mappings, iterableMapping, mapMapping );
null,
executable,
parameters,
returnType,
mappings,
iterableMapping,
mapMapping
);
} }
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable, public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable,
@ -72,14 +64,13 @@ public class Method {
executable, executable,
parameters, parameters,
returnType, returnType,
Collections.<String, Mapping>emptyMap(), Collections.<String, List<Mapping>> emptyMap(),
null, null,
null null );
);
} }
private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters, Type returnType, private Method(Type declaringMapper, ExecutableElement executable, List<Parameter> parameters, Type returnType,
Map<String, Mapping> mappings, IterableMapping iterableMapping, MapMapping mapMapping) { Map<String, List<Mapping>> mappings, IterableMapping iterableMapping, MapMapping mapMapping) {
this.declaringMapper = declaringMapper; this.declaringMapper = declaringMapper;
this.executable = executable; this.executable = executable;
this.parameters = parameters; this.parameters = parameters;
@ -102,9 +93,8 @@ public class Method {
} }
/** /**
* Returns the mapper type declaring this method if it is not declared by * Returns the mapper type declaring this method if it is not declared by the mapper interface currently processed
* the mapper interface currently processed but by another mapper imported * but by another mapper imported via {@code Mapper#users()}.
* via {@code Mapper#users()}.
* *
* @return The declaring mapper type * @return The declaring mapper type
*/ */
@ -154,11 +144,11 @@ public class Method {
return returnType; return returnType;
} }
public Map<String, Mapping> getMappings() { public Map<String, List<Mapping>> getMappings() {
return mappings; return mappings;
} }
public void setMappings(Map<String, Mapping> mappings) { public void setMappings(Map<String, List<Mapping>> mappings) {
this.mappings = mappings; this.mappings = mappings;
} }
@ -179,11 +169,9 @@ public class Method {
} }
public boolean reverses(Method method) { public boolean reverses(Method method) {
return return getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
getSourceParameters().size() == 1 && && equals( getSourceParameters().iterator().next().getType(), method.getResultType() )
method.getSourceParameters().size() == 1 && && equals( getResultType(), method.getSourceParameters().iterator().next().getType() );
equals( getSourceParameters().iterator().next().getType(), method.getResultType() ) &&
equals( getResultType(), method.getSourceParameters().iterator().next().getType() );
} }
public Parameter getTargetParameter() { public Parameter getTargetParameter() {
@ -191,13 +179,13 @@ public class Method {
} }
public boolean isIterableMapping() { public boolean isIterableMapping() {
return getSourceParameters().size() == 1 && return getSourceParameters().size() == 1 && getSourceParameters().iterator().next().getType().isIterableType()
getSourceParameters().iterator().next().getType().isIterableType() && getResultType().isIterableType(); && getResultType().isIterableType();
} }
public boolean isMapMapping() { public boolean isMapMapping() {
return getSourceParameters().size() == 1 && getSourceParameters().iterator().next().getType().isMapType() && return getSourceParameters().size() == 1 && getSourceParameters().iterator().next().getType().isMapType()
getResultType().isMapType(); && getResultType().isMapType();
} }
private boolean equals(Object o1, Object o2) { private boolean equals(Object o1, Object o2) {
@ -219,12 +207,13 @@ public class Method {
} }
public Mapping getMapping(String targetPropertyName) { public Mapping getMapping(String targetPropertyName) {
for ( Mapping mapping : mappings.values() ) { for ( Map.Entry<String, List<Mapping>> entry : mappings.entrySet() ) {
if ( mapping.getTargetName().equals( targetPropertyName ) ) { for ( Mapping mapping : entry.getValue()) {
return mapping; if ( mapping.getTargetName().equals( targetPropertyName ) ) {
return mapping;
}
} }
} }
return null; return null;
} }

View File

@ -216,11 +216,16 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
} }
} }
private Map<String, Mapping> reverse(Map<String, Mapping> mappings) { private Map<String, List<Mapping>> reverse(Map<String, List<Mapping>> mappings) {
Map<String, Mapping> reversed = new HashMap<String, Mapping>(); Map<String, List<Mapping>> reversed = new HashMap<String, List<Mapping>>();
for ( Mapping mapping : mappings.values() ) { for ( List<Mapping> mappingList : mappings.values() ) {
reversed.put( mapping.getTargetName(), mapping.reverse() ); for (Mapping mapping : mappingList) {
if (!reversed.containsKey( mapping.getTargetName())) {
reversed.put( mapping.getTargetName(), new ArrayList<Mapping>());
}
reversed.get( mapping.getTargetName() ).add( mapping.reverse() );
}
} }
return reversed; return reversed;
} }
@ -237,10 +242,25 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
); );
for ( ExecutableElement getter : sourceGetters ) { for ( ExecutableElement getter : sourceGetters ) {
Mapping sourceMapping = method.getMappings().get( sourcePropertyName );
boolean mapsToOtherTarget = List<Mapping> sourceMappings = method.getMappings().get( sourcePropertyName );
sourceMapping != null && !sourceMapping.getTargetName().equals( targetPropertyName ); if (method.getMappings().containsKey( sourcePropertyName ) ) {
if ( executables.getPropertyName( getter ).equals( sourcePropertyName ) && !mapsToOtherTarget ) { for (Mapping sourceMapping : sourceMappings) {
boolean mapsToOtherTarget = !sourceMapping.getTargetName().equals( targetPropertyName );
if ( executables.getPropertyName( getter ).equals( sourcePropertyName ) && !mapsToOtherTarget ) {
return getPropertyMapping(
methods,
method,
parameter,
getter,
setterMethod,
dateFormat
);
}
}
}
else if (executables.getPropertyName( getter ).equals( sourcePropertyName ))
{
return getPropertyMapping( return getPropertyMapping(
methods, methods,
method, method,
@ -378,31 +398,17 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
boolean foundUnmappedProperty = false; boolean foundUnmappedProperty = false;
for ( Mapping mappedProperty : method.getMappings().values() ) { for ( List<Mapping> mappedProperties : method.getMappings().values() ) {
if ( mappedProperty.getSourceParameterName() != null ) { for (Mapping mappedProperty : mappedProperties) {
Parameter sourceParameter = method.getSourceParameter( mappedProperty.getSourceParameterName() ); if ( mappedProperty.getSourceParameterName() != null ) {
Parameter sourceParameter = method.getSourceParameter( mappedProperty.getSourceParameterName() );
if ( sourceParameter == null ) { if ( sourceParameter == null ) {
messager.printMessage(
Kind.ERROR,
String.format(
"Method has no parameter named \"%s\".",
mappedProperty.getSourceParameterName()
),
method.getExecutable(),
mappedProperty.getMirror(),
mappedProperty.getSourceAnnotationValue()
);
foundUnmappedProperty = true;
}
else {
if ( !hasProperty( sourceParameter, mappedProperty.getSourcePropertyName() ) ) {
messager.printMessage( messager.printMessage(
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"The type of parameter \"%s\" has no property named \"%s\".", "Method has no parameter named \"%s\".",
mappedProperty.getSourceParameterName(), mappedProperty.getSourceParameterName()
mappedProperty.getSourcePropertyName()
), ),
method.getExecutable(), method.getExecutable(),
mappedProperty.getMirror(), mappedProperty.getMirror(),
@ -410,41 +416,56 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
); );
foundUnmappedProperty = true; foundUnmappedProperty = true;
} }
} else {
if ( !hasProperty( sourceParameter, mappedProperty.getSourcePropertyName() ) ) {
messager.printMessage(
Kind.ERROR,
String.format(
"The type of parameter \"%s\" has no property named \"%s\".",
mappedProperty.getSourceParameterName(),
mappedProperty.getSourcePropertyName()
),
method.getExecutable(),
mappedProperty.getMirror(),
mappedProperty.getSourceAnnotationValue()
);
foundUnmappedProperty = true;
}
}
} }
else if ( !hasSourceProperty( else if ( !hasSourceProperty(
method, method,
mappedProperty.getSourcePropertyName() mappedProperty.getSourcePropertyName()
) ) { ) ) {
messager.printMessage( messager.printMessage(
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"No property named \"%s\" exists in source parameter(s).", "No property named \"%s\" exists in source parameter(s).",
mappedProperty.getSourceName() mappedProperty.getSourceName()
), ),
method.getExecutable(), method.getExecutable(),
mappedProperty.getMirror(), mappedProperty.getMirror(),
mappedProperty.getSourceAnnotationValue() mappedProperty.getSourceAnnotationValue()
); );
foundUnmappedProperty = true; foundUnmappedProperty = true;
} }
if ( !targetProperties.contains( mappedProperty.getTargetName() ) ) { if ( !targetProperties.contains( mappedProperty.getTargetName() ) ) {
messager.printMessage( messager.printMessage(
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"Unknown property \"%s\" in return type %s.", "Unknown property \"%s\" in return type %s.",
mappedProperty.getTargetName(), mappedProperty.getTargetName(),
method.getResultType() method.getResultType()
), ),
method.getExecutable(), method.getExecutable(),
mappedProperty.getMirror(), mappedProperty.getMirror(),
mappedProperty.getTargetAnnotationValue() mappedProperty.getTargetAnnotationValue()
); );
foundUnmappedProperty = true; foundUnmappedProperty = true;
}
} }
} }
return !foundUnmappedProperty; return !foundUnmappedProperty;
} }

View File

@ -266,14 +266,17 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
* *
* @return The mappings for the given method, keyed by source property name * @return The mappings for the given method, keyed by source property name
*/ */
private Map<String, Mapping> getMappings(ExecutableElement method) { private Map<String, List<Mapping>> getMappings(ExecutableElement method) {
Map<String, Mapping> mappings = new HashMap<String, Mapping>(); Map<String, List<Mapping>> mappings = new HashMap<String, List<Mapping>>();
MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method ); MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method );
MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method ); MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method );
if ( mappingAnnotation != null ) { if ( mappingAnnotation != null ) {
mappings.put( mappingAnnotation.source(), Mapping.fromMappingPrism( mappingAnnotation, method ) ); if (!mappings.containsKey( mappingAnnotation.source())) {
mappings.put( mappingAnnotation.source(), new ArrayList<Mapping>() );
}
mappings.get( mappingAnnotation.source() ).add( Mapping.fromMappingPrism( mappingAnnotation, method ) );
} }
if ( mappingsAnnotation != null ) { if ( mappingsAnnotation != null ) {

View File

@ -0,0 +1,58 @@
/**
* 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.severaltargets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.mapstruct.ap.testutil.MapperTestBase;
import org.mapstruct.ap.testutil.WithClasses;
import org.testng.annotations.Test;
import static org.fest.assertions.Assertions.assertThat;
import org.mapstruct.ap.testutil.IssueKey;
/**
* Test for the generation of implementation of abstract base classes.
*
* @author Sjaak Derksen
*/
@WithClasses({ Source.class, Target.class, SourceTargetMapper.class, TimeAndFormat.class,
TimeAndFormatMapper.class })
public class MultipleSourcesTest extends MapperTestBase {
@Test
@IssueKey("94")
public void shouldMapMultipleSources() throws ParseException {
Source source = new Source();
String sourceFormat = "dd-MM-yyyy";
SimpleDateFormat dateFormat = new SimpleDateFormat( sourceFormat );
Date sourceTime = dateFormat.parse( "09-01-2014" );
TimeAndFormat sourceTimeAndFormat = new TimeAndFormat();
sourceTimeAndFormat.setTfFormat( sourceFormat );
sourceTimeAndFormat.setTfTime( sourceTime );
source.setTimeAndFormat( sourceTimeAndFormat );
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
assertThat( target ).isNotNull();
assertThat( target.getFormat() ).isEqualTo( sourceFormat );
assertThat( target.getTime() ).isEqualTo( sourceTime );
}
}

View File

@ -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.severaltargets;
/**
*
* @author Sjaak Derksen
*/
public class Source {
private TimeAndFormat timeAndFormat;
public TimeAndFormat getTimeAndFormat() {
return timeAndFormat;
}
public void setTimeAndFormat(TimeAndFormat timeAndFormat) {
this.timeAndFormat = timeAndFormat;
}
}

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.severaltargets;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
/**
*
* @author Sjaak Derksen
*/
@Mapper(uses = TimeAndFormatMapper.class)
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
@Mappings(
{
@Mapping(source = "timeAndFormat", target = "time"),
@Mapping(source = "timeAndFormat", target = "format")
})
Target sourceToTarget( Source s );
}

View File

@ -0,0 +1,47 @@
/**
* 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.severaltargets;
import java.util.Date;
/**
*
* @author Sjaak Derksen
*/
public class Target {
private String format;
private Date time;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}

View File

@ -0,0 +1,46 @@
/**
* 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.severaltargets;
import java.util.Date;
/**
*
* @author Sjaak Derksen
*/
public class TimeAndFormat {
private Date tfTime;
private String tfFormat;
public Date getTfTime() {
return tfTime;
}
public void setTfTime(Date tfTime) {
this.tfTime = tfTime;
}
public String getTfFormat() {
return tfFormat;
}
public void setTfFormat(String tfFormat) {
this.tfFormat = tfFormat;
}
}

View File

@ -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.severaltargets;
import java.util.Date;
/**
*
* @author Sjaak Derksen
*/
public class TimeAndFormatMapper {
public String getFormat(TimeAndFormat t) {
return t.getTfFormat();
}
public Date getTime(TimeAndFormat t) {
return t.getTfTime();
}
}