mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#94 Allowing to map the same source property to several target properties
This commit is contained in:
parent
cd058663f4
commit
e82277e0fc
@ -18,7 +18,9 @@
|
||||
*/
|
||||
package org.mapstruct.ap.model.source;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.AnnotationValue;
|
||||
@ -44,11 +46,14 @@ public class Mapping {
|
||||
private final AnnotationValue sourceAnnotationValue;
|
||||
private final AnnotationValue targetAnnotationValue;
|
||||
|
||||
public static Map<String, Mapping> fromMappingsPrism(MappingsPrism mappingsAnnotation, Element element) {
|
||||
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
||||
public static Map<String, List<Mapping>> fromMappingsPrism(MappingsPrism mappingsAnnotation, Element element) {
|
||||
Map<String, List<Mapping>> mappings = new HashMap<String, List<Mapping>>();
|
||||
|
||||
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;
|
||||
|
@ -43,25 +43,17 @@ public class Method {
|
||||
private final List<Parameter> parameters;
|
||||
private final Type returnType;
|
||||
|
||||
private Map<String, Mapping> mappings;
|
||||
private Map<String, List<Mapping>> mappings;
|
||||
private IterableMapping iterableMapping;
|
||||
private MapMapping mapMapping;
|
||||
|
||||
private final Parameter targetParameter;
|
||||
|
||||
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) {
|
||||
|
||||
return new Method(
|
||||
null,
|
||||
executable,
|
||||
parameters,
|
||||
returnType,
|
||||
mappings,
|
||||
iterableMapping,
|
||||
mapMapping
|
||||
);
|
||||
return new Method( null, executable, parameters, returnType, mappings, iterableMapping, mapMapping );
|
||||
}
|
||||
|
||||
public static Method forReferencedMethod(Type declaringMapper, ExecutableElement executable,
|
||||
@ -72,14 +64,13 @@ public class Method {
|
||||
executable,
|
||||
parameters,
|
||||
returnType,
|
||||
Collections.<String, Mapping>emptyMap(),
|
||||
Collections.<String, List<Mapping>> emptyMap(),
|
||||
null,
|
||||
null
|
||||
);
|
||||
null );
|
||||
}
|
||||
|
||||
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.executable = executable;
|
||||
this.parameters = parameters;
|
||||
@ -102,9 +93,8 @@ public class Method {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mapper type declaring this method if it is not declared by
|
||||
* the mapper interface currently processed but by another mapper imported
|
||||
* via {@code Mapper#users()}.
|
||||
* Returns the mapper type declaring this method if it is not declared by the mapper interface currently processed
|
||||
* but by another mapper imported via {@code Mapper#users()}.
|
||||
*
|
||||
* @return The declaring mapper type
|
||||
*/
|
||||
@ -154,11 +144,11 @@ public class Method {
|
||||
return returnType;
|
||||
}
|
||||
|
||||
public Map<String, Mapping> getMappings() {
|
||||
public Map<String, List<Mapping>> getMappings() {
|
||||
return mappings;
|
||||
}
|
||||
|
||||
public void setMappings(Map<String, Mapping> mappings) {
|
||||
public void setMappings(Map<String, List<Mapping>> mappings) {
|
||||
this.mappings = mappings;
|
||||
}
|
||||
|
||||
@ -179,11 +169,9 @@ public class Method {
|
||||
}
|
||||
|
||||
public boolean reverses(Method method) {
|
||||
return
|
||||
getSourceParameters().size() == 1 &&
|
||||
method.getSourceParameters().size() == 1 &&
|
||||
equals( getSourceParameters().iterator().next().getType(), method.getResultType() ) &&
|
||||
equals( getResultType(), method.getSourceParameters().iterator().next().getType() );
|
||||
return getSourceParameters().size() == 1 && method.getSourceParameters().size() == 1
|
||||
&& equals( getSourceParameters().iterator().next().getType(), method.getResultType() )
|
||||
&& equals( getResultType(), method.getSourceParameters().iterator().next().getType() );
|
||||
}
|
||||
|
||||
public Parameter getTargetParameter() {
|
||||
@ -191,13 +179,13 @@ public class Method {
|
||||
}
|
||||
|
||||
public boolean isIterableMapping() {
|
||||
return getSourceParameters().size() == 1 &&
|
||||
getSourceParameters().iterator().next().getType().isIterableType() && getResultType().isIterableType();
|
||||
return getSourceParameters().size() == 1 && getSourceParameters().iterator().next().getType().isIterableType()
|
||||
&& getResultType().isIterableType();
|
||||
}
|
||||
|
||||
public boolean isMapMapping() {
|
||||
return getSourceParameters().size() == 1 && getSourceParameters().iterator().next().getType().isMapType() &&
|
||||
getResultType().isMapType();
|
||||
return getSourceParameters().size() == 1 && getSourceParameters().iterator().next().getType().isMapType()
|
||||
&& getResultType().isMapType();
|
||||
}
|
||||
|
||||
private boolean equals(Object o1, Object o2) {
|
||||
@ -219,12 +207,13 @@ public class Method {
|
||||
}
|
||||
|
||||
public Mapping getMapping(String targetPropertyName) {
|
||||
for ( Mapping mapping : mappings.values() ) {
|
||||
for ( Map.Entry<String, List<Mapping>> entry : mappings.entrySet() ) {
|
||||
for ( Mapping mapping : entry.getValue()) {
|
||||
if ( mapping.getTargetName().equals( targetPropertyName ) ) {
|
||||
return mapping;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -216,11 +216,16 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Mapping> reverse(Map<String, Mapping> mappings) {
|
||||
Map<String, Mapping> reversed = new HashMap<String, Mapping>();
|
||||
private Map<String, List<Mapping>> reverse(Map<String, List<Mapping>> mappings) {
|
||||
Map<String, List<Mapping>> reversed = new HashMap<String, List<Mapping>>();
|
||||
|
||||
for ( Mapping mapping : mappings.values() ) {
|
||||
reversed.put( mapping.getTargetName(), mapping.reverse() );
|
||||
for ( List<Mapping> mappingList : mappings.values() ) {
|
||||
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;
|
||||
}
|
||||
@ -237,9 +242,11 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
|
||||
for ( ExecutableElement getter : sourceGetters ) {
|
||||
Mapping sourceMapping = method.getMappings().get( sourcePropertyName );
|
||||
boolean mapsToOtherTarget =
|
||||
sourceMapping != null && !sourceMapping.getTargetName().equals( targetPropertyName );
|
||||
|
||||
List<Mapping> sourceMappings = method.getMappings().get( sourcePropertyName );
|
||||
if (method.getMappings().containsKey( sourcePropertyName ) ) {
|
||||
for (Mapping sourceMapping : sourceMappings) {
|
||||
boolean mapsToOtherTarget = !sourceMapping.getTargetName().equals( targetPropertyName );
|
||||
if ( executables.getPropertyName( getter ).equals( sourcePropertyName ) && !mapsToOtherTarget ) {
|
||||
return getPropertyMapping(
|
||||
methods,
|
||||
@ -251,6 +258,19 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (executables.getPropertyName( getter ).equals( sourcePropertyName ))
|
||||
{
|
||||
return getPropertyMapping(
|
||||
methods,
|
||||
method,
|
||||
parameter,
|
||||
getter,
|
||||
setterMethod,
|
||||
dateFormat
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@ -378,7 +398,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
|
||||
boolean foundUnmappedProperty = false;
|
||||
|
||||
for ( Mapping mappedProperty : method.getMappings().values() ) {
|
||||
for ( List<Mapping> mappedProperties : method.getMappings().values() ) {
|
||||
for (Mapping mappedProperty : mappedProperties) {
|
||||
if ( mappedProperty.getSourceParameterName() != null ) {
|
||||
Parameter sourceParameter = method.getSourceParameter( mappedProperty.getSourceParameterName() );
|
||||
|
||||
@ -444,7 +465,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Metho
|
||||
foundUnmappedProperty = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return !foundUnmappedProperty;
|
||||
}
|
||||
|
||||
|
@ -266,14 +266,17 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
||||
*
|
||||
* @return The mappings for the given method, keyed by source property name
|
||||
*/
|
||||
private Map<String, Mapping> getMappings(ExecutableElement method) {
|
||||
Map<String, Mapping> mappings = new HashMap<String, Mapping>();
|
||||
private Map<String, List<Mapping>> getMappings(ExecutableElement method) {
|
||||
Map<String, List<Mapping>> mappings = new HashMap<String, List<Mapping>>();
|
||||
|
||||
MappingPrism mappingAnnotation = MappingPrism.getInstanceOn( method );
|
||||
MappingsPrism mappingsAnnotation = MappingsPrism.getInstanceOn( method );
|
||||
|
||||
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 ) {
|
||||
|
@ -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 );
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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 );
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user