#157 introduction of conversionY( methodX ( parameter ) )

This commit is contained in:
sjaakd 2014-03-23 00:30:21 +01:00
parent 23f3d4a3c1
commit 01e918b3e0
7 changed files with 353 additions and 299 deletions

View File

@ -38,6 +38,12 @@ public class TypeConversion extends ModelElement {
private final String sourceReference; private final String sourceReference;
private final String openExpression; private final String openExpression;
private final String closeExpression; private final String closeExpression;
/**
* A reference to mapping method in case this is a two-step mapping, e.g. from
* {@code JAXBElement<Bar>} to {@code Foo} to for which a nested method call will be generated:
* {@code setFoo(barToFoo( jaxbElemToValue( bar) ) )}
*/
private MethodReference methodRefChild;
public TypeConversion( Set<Type> importTypes, public TypeConversion( Set<Type> importTypes,
List<Type> exceptionTypes, List<Type> exceptionTypes,
@ -72,4 +78,12 @@ public class TypeConversion extends ModelElement {
public String getCloseExpression() { public String getCloseExpression() {
return closeExpression; return closeExpression;
} }
public void setMethodRefChild( MethodReference methodRefChild ) {
this.methodRefChild = methodRefChild;
}
public MethodReference getMethodRefChild() {
return methodRefChild;
}
} }

View File

@ -121,70 +121,107 @@ public class MappingResolver {
String dateFormat, String dateFormat,
String sourceReference ) { String sourceReference ) {
MethodReference mappingMethodReference = resolveViaMethod( ResolvingAttempt attempt = new ResolvingAttempt( mappingMethod,
mappingMethod,
mappedElement, mappedElement,
mapperReferences, mapperReferences,
methods, methods,
sourceType,
targetType,
targetPropertyName,
dateFormat
);
TargetAssignment assignment = null;
if (mappingMethodReference != null ) {
assignment = new TargetAssignment(mappingMethodReference );
}
else if (sourceType.isAssignableTo( targetType ) ) {
assignment = new TargetAssignment();
}
else {
TypeConversion conversion = resolveViaConversion( sourceType, targetType, dateFormat, sourceReference );
if ( conversion != null ) {
assignment = new TargetAssignment(conversion );
}
else {
mappingMethodReference = resolveViaMethodAndMethod(
mappingMethod,
mappedElement,
mapperReferences,
methods,
sourceType,
targetType,
targetPropertyName,
dateFormat
);
if ( mappingMethodReference != null ) {
assignment = new TargetAssignment( mappingMethodReference );
}
else {
mappingMethodReference = resolveViaConversionAndMethod(
mappingMethod,
mappedElement,
mapperReferences,
methods,
sourceType,
targetType,
targetPropertyName, targetPropertyName,
dateFormat, dateFormat,
sourceReference ); sourceReference,
this
);
return attempt.getTargetAssignment( sourceType, targetType );
} }
if ( mappingMethodReference != null ) {
assignment = new TargetAssignment( mappingMethodReference ); public Set<VirtualMappingMethod> getVirtualMethodsToGenerate() {
} return virtualMethods;
}
}
return assignment;
} }
private TypeConversion resolveViaConversion( Type sourceType, private static class ResolvingAttempt {
Type targetType,
private final SourceMethod mappingMethod;
private final String mappedElement;
private final List<MapperReference> mapperReferences;
private final List<SourceMethod> methods;
private final String targetPropertyName;
private final String dateFormat;
private final String sourceReference;
private final MappingResolver context;
// resolving via 2 steps creates the possibillity of wrong matches, first builtin method matches,
// second doesn't. In that case, the first builtin method should not lead to a virtual method
// so this set must be cleared.
private final Set<VirtualMappingMethod> virtualMethodCandidates;
private ResolvingAttempt( SourceMethod mappingMethod,
String mappedElement,
List<MapperReference> mapperReferences,
List<SourceMethod> methods,
String targetPropertyName,
String dateFormat, String dateFormat,
String sourceReference ) { String sourceReference,
ConversionProvider conversionProvider = conversions.getConversion( sourceType, targetType ); MappingResolver context ) {
this.mappingMethod = mappingMethod;
this.mappedElement = mappedElement;
this.mapperReferences = mapperReferences;
this.methods = methods;
this.targetPropertyName = targetPropertyName;
this.dateFormat = dateFormat;
this.sourceReference = sourceReference;
this.context = context;
this.virtualMethodCandidates = new HashSet<VirtualMappingMethod>();
}
private TargetAssignment getTargetAssignment( Type sourceType, Type targetType ) {
// first simpele mapping method
MethodReference mappingMethodReference = resolveViaMethod( sourceType, targetType );
if ( mappingMethodReference != null ) {
context.virtualMethods.addAll( virtualMethodCandidates );
return new TargetAssignment( mappingMethodReference );
}
// then direct assignable
if ( sourceType.isAssignableTo( targetType ) ) {
return new TargetAssignment();
}
// then type conversion
TypeConversion conversion = resolveViaConversion( sourceType, targetType );
if ( conversion != null ) {
return new TargetAssignment( conversion );
}
// 2 step method, first: method(method(souurce))
mappingMethodReference = resolveViaMethodAndMethod( sourceType, targetType );
if ( mappingMethodReference != null ) {
context.virtualMethods.addAll( virtualMethodCandidates );
return new TargetAssignment( mappingMethodReference );
}
// 2 step method, then: method(conversion(souurce))
mappingMethodReference = resolveViaConversionAndMethod( sourceType, targetType );
if ( mappingMethodReference != null ) {
context.virtualMethods.addAll( virtualMethodCandidates );
return new TargetAssignment( mappingMethodReference );
}
// 2 step method, finally: conversion(method(souurce))
conversion = resolveViaMethodAndConversion( sourceType, targetType );
if ( conversion != null ) {
context.virtualMethods.addAll( virtualMethodCandidates );
return new TargetAssignment( conversion );
}
// if nothing works, alas, the result is null
return null;
}
private TypeConversion resolveViaConversion( Type sourceType, Type targetType ) {
ConversionProvider conversionProvider = context.conversions.getConversion( sourceType, targetType );
if ( conversionProvider == null ) { if ( conversionProvider == null ) {
return null; return null;
@ -192,49 +229,35 @@ public class MappingResolver {
return conversionProvider.to( return conversionProvider.to(
sourceReference, sourceReference,
new DefaultConversionContext( typeFactory, targetType, dateFormat ) new DefaultConversionContext( context.typeFactory, targetType, dateFormat )
); );
} }
/** /**
* Returns a reference to a method mapping the given source type to the given target type, if such a method exists. * Returns a reference to a method mapping the given source type to the given target type, if such a method
* exists.
* *
*/ */
private MethodReference resolveViaMethod( SourceMethod mappingMethod, private MethodReference resolveViaMethod( Type sourceType, Type targetType ) {
String mappedElement,
List<MapperReference> mapperReferences,
List<SourceMethod> methods,
Type sourceType,
Type targetType,
String targetPropertyName,
String dateFormat ) {
// first try to find a matching source method // first try to find a matching source method
SourceMethod matchingSourceMethod = getBestMatch( SourceMethod matchingSourceMethod = getBestMatch( methods, sourceType, targetType );
mappingMethod,
mappedElement,
methods,
sourceType,
targetType,
targetPropertyName
);
if ( matchingSourceMethod != null ) { if ( matchingSourceMethod != null ) {
return getMappingMethodReference( matchingSourceMethod, mapperReferences, targetType ); return getMappingMethodReference( matchingSourceMethod, mapperReferences, targetType );
} }
// then a matching built-in method // then a matching built-in method
BuiltInMethod matchingBuiltInMethod = getBestMatch( BuiltInMethod matchingBuiltInMethod =
mappingMethod, getBestMatch( context.builtInMethods.getBuiltInMethods(), sourceType, targetType );
mappedElement,
builtInMethods.getBuiltInMethods(),
sourceType,
targetType,
targetPropertyName
);
return matchingBuiltInMethod != null ? if ( matchingBuiltInMethod != null ) {
getMappingMethodReference( matchingBuiltInMethod, targetType, dateFormat ) : null; virtualMethodCandidates.add( new VirtualMappingMethod( matchingBuiltInMethod ) );
ConversionContext ctx = new DefaultConversionContext( context.typeFactory, targetType, dateFormat );
return new MethodReference( matchingBuiltInMethod, ctx );
}
return null;
} }
/** /**
@ -247,49 +270,27 @@ public class MappingResolver {
* </ul> * </ul>
* then this method tries to resolve this combination and make a mapping methodY( methodX ( parameter ) ) * then this method tries to resolve this combination and make a mapping methodY( methodX ( parameter ) )
*/ */
private MethodReference resolveViaMethodAndMethod( SourceMethod mappingMethod, private MethodReference resolveViaMethodAndMethod( Type sourceType, Type targetType ) {
String mappedElement,
List<MapperReference> mapperReferences,
List<SourceMethod> methods,
Type sourceType,
Type targetType,
String targetPropertyName,
String dateFormat ) {
List<Method> methodYCandidates = new ArrayList<Method>( methods ); List<Method> methodYCandidates = new ArrayList<Method>( methods );
methodYCandidates.addAll( builtInMethods.getBuiltInMethods() ); methodYCandidates.addAll( context.builtInMethods.getBuiltInMethods() );
MethodReference methodRefY = null; MethodReference methodRefY = null;
// Iterate over all source methods. Check if the return type matches with the parameter that we need. // Iterate over all source methods. Check if the return type matches with the parameter that we need.
// so assume we need a method from A to C we look for a methodX from A to B (all methods in the // so assume we need a method from A to C we look for a methodX from A to B (all methods in the
// list form such a candidate). // list form such a candidate).
// For each of the candidates, we need to look if there's a methodY, either // For each of the candidates, we need to look if there's a methodY, either
// sourceMethod or builtIn that fits the signature B to C. Only then there is a match. If we have a match // sourceMethod or builtIn that fits the signature B to C. Only then there is a match. If we have a match
// a nested method call can be called. so C = methodY( methodX (A) ) // a nested method call can be called. so C = methodY( methodX (A) )
for ( Method methodYCandidate : methodYCandidates ) { for ( Method methodYCandidate : methodYCandidates ) {
if ( methodYCandidate.getSourceParameters().size() == 1 ) { if ( methodYCandidate.getSourceParameters().size() == 1 ) {
methodRefY = resolveViaMethod( methodRefY = resolveViaMethod( methodYCandidate.getSourceParameters().get( 0 ).getType(),
mappingMethod, targetType );
mappedElement,
mapperReferences,
methods,
methodYCandidate.getSourceParameters().get( 0 ).getType(),
targetType,
targetPropertyName,
dateFormat
);
if ( methodRefY != null ) { if ( methodRefY != null ) {
MethodReference methodRefX = resolveViaMethod( MethodReference methodRefX = resolveViaMethod(
mappingMethod,
mappedElement,
mapperReferences,
methods,
sourceType, sourceType,
methodYCandidate.getSourceParameters().get( 0 ).getType(), methodYCandidate.getSourceParameters().get( 0 ).getType()
targetPropertyName,
dateFormat
); );
if ( methodRefX != null ) { if ( methodRefX != null ) {
methodRefY.setMethodRefChild( methodRefX ); methodRefY.setMethodRefChild( methodRefX );
@ -297,6 +298,7 @@ public class MappingResolver {
} }
else { else {
// both should match; // both should match;
virtualMethodCandidates.clear();
methodRefY = null; methodRefY = null;
} }
} }
@ -313,46 +315,31 @@ public class MappingResolver {
* </ul> * </ul>
* then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) ) * then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) )
*/ */
private MethodReference resolveViaConversionAndMethod( SourceMethod mappingMethod, private MethodReference resolveViaConversionAndMethod( Type sourceType, Type targetType ) {
String mappedElement,
List<MapperReference> mapperReferences,
List<SourceMethod> methods,
Type sourceType,
Type targetType,
String targetPropertyName,
String dateFormat,
String sourceReference ) {
List<Method> methodYCandidates = new ArrayList<Method>( methods ); List<Method> methodYCandidates = new ArrayList<Method>( methods );
methodYCandidates.addAll( builtInMethods.getBuiltInMethods() ); methodYCandidates.addAll( context.builtInMethods.getBuiltInMethods() );
MethodReference methodRefY = null; MethodReference methodRefY = null;
for ( Method methodYCandidate : methodYCandidates ) { for ( Method methodYCandidate : methodYCandidates ) {
if ( methodYCandidate.getSourceParameters().size() == 1 ) { if ( methodYCandidate.getSourceParameters().size() == 1 ) {
methodRefY = resolveViaMethod( methodRefY = resolveViaMethod(
mappingMethod,
mappedElement,
mapperReferences,
methods,
methodYCandidate.getSourceParameters().get( 0 ).getType(), methodYCandidate.getSourceParameters().get( 0 ).getType(),
targetType, targetType
targetPropertyName,
dateFormat
); );
if ( methodRefY != null ) { if ( methodRefY != null ) {
TypeConversion conversionXRef = resolveViaConversion( TypeConversion conversionXRef = resolveViaConversion(
sourceType, sourceType,
methodYCandidate.getSourceParameters().get( 0 ).getType(), methodYCandidate.getSourceParameters().get( 0 ).getType()
dateFormat,
sourceReference
); );
if ( conversionXRef != null ) { if ( conversionXRef != null ) {
methodRefY.setTypeConversionChild( conversionXRef ); methodRefY.setTypeConversionChild( conversionXRef );
break; break;
} }
else { else {
// both should match; // both should match
virtualMethodCandidates.clear();
methodRefY = null; methodRefY = null;
} }
} }
@ -361,15 +348,51 @@ public class MappingResolver {
return methodRefY; return methodRefY;
} }
/**
* Suppose mapping required from A to C and:
* <ul>
* <li>there is a conversion from A to B, conversionX</li>
* <li>there is a method from B to C, methodY</li>
* </ul>
* then this method tries to resolve this combination and make a mapping methodY( conversionX ( parameter ) )
*/
private TypeConversion resolveViaMethodAndConversion( Type sourceType, Type targetType ) {
private <T extends Method> T getBestMatch( SourceMethod mappingMethod, List<Method> methodXCandidates = new ArrayList<Method>( methods );
String mappedElement, methodXCandidates.addAll( context.builtInMethods.getBuiltInMethods() );
List<T> methods,
Type sourceType,
Type returnType,
String targetPropertyName ) {
List<T> candidates = methodSelectors.getMatchingMethods( TypeConversion conversionYRef = null;
// search the other way arround
for ( Method methodXCandidate : methodXCandidates ) {
if ( methodXCandidate.getSourceParameters().size() == 1 ) {
MethodReference methodRefX = resolveViaMethod(
sourceType,
methodXCandidate.getReturnType()
);
if ( methodRefX != null ) {
conversionYRef = resolveViaConversion(
methodXCandidate.getReturnType(),
targetType
);
if ( conversionYRef != null ) {
conversionYRef.setMethodRefChild( methodRefX );
break;
}
else {
// both should match;
virtualMethodCandidates.clear();
conversionYRef = null;
}
}
}
}
return conversionYRef;
}
private <T extends Method> T getBestMatch( List<T> methods, Type sourceType, Type returnType ) {
List<T> candidates = context.methodSelectors.getMatchingMethods(
mappingMethod, mappingMethod,
methods, methods,
sourceType, sourceType,
@ -377,10 +400,11 @@ public class MappingResolver {
targetPropertyName targetPropertyName
); );
// raise an error if more than one mapping method is suitable to map the given source type into the target type // raise an error if more than one mapping method is suitable to map the given source type
// into the target type
if ( candidates.size() > 1 ) { if ( candidates.size() > 1 ) {
messager.printMessage( context.messager.printMessage(
Kind.ERROR, Kind.ERROR,
String.format( String.format(
"Ambiguous mapping methods found for mapping " + mappedElement + " from %s to %s: %s.", "Ambiguous mapping methods found for mapping " + mappedElement + " from %s to %s: %s.",
@ -399,11 +423,6 @@ public class MappingResolver {
return null; return null;
} }
public Set<VirtualMappingMethod> getVirtualMethodsToGenerate() {
return virtualMethods;
}
private MethodReference getMappingMethodReference( SourceMethod method, private MethodReference getMappingMethodReference( SourceMethod method,
List<MapperReference> mapperReferences, List<MapperReference> mapperReferences,
Type targetType ) { Type targetType ) {
@ -424,12 +443,5 @@ public class MappingResolver {
} }
return null; return null;
} }
private MethodReference getMappingMethodReference(BuiltInMethod method, Type returnType, String dateFormat) {
virtualMethods.add( new VirtualMappingMethod( method ) );
ConversionContext ctx = new DefaultConversionContext( typeFactory, returnType, dateFormat );
return new MethodReference( method, ctx );
} }
} }

View File

@ -32,7 +32,7 @@
<@includeModel object=methodRefChild source=ext.source targetType=singleSourceParameterType.name/> <@includeModel object=methodRefChild source=ext.source targetType=singleSourceParameterType.name/>
<#elseif typeConversion??> <#elseif typeConversion??>
<#-- the nested case: a type conversion --> <#-- the nested case: a type conversion -->
<@includeModel object=typeConversion/> <@includeModel object=typeConversion source=ext.source targetType=singleSourceParameterType.name/>
<#else> <#else>
<#-- the non nested case --> <#-- the non nested case -->
${ext.source} ${ext.source}

View File

@ -52,7 +52,7 @@
<#compress> <#compress>
<#switch assignmentType> <#switch assignmentType>
<#case "TYPE_CONVERSION"> <#case "TYPE_CONVERSION">
<@includeModel object=typeConversion/> <@includeModel object=typeConversion source="${ext.source}" targetType=ext.targetType/>
<#break> <#break>
<#case "METHOD_REFERENCE"> <#case "METHOD_REFERENCE">
<@includeModel object=methodReference source="${ext.source}" targetType=ext.targetType raw=ext.raw/> <@includeModel object=methodReference source="${ext.source}" targetType=ext.targetType raw=ext.raw/>

View File

@ -18,4 +18,10 @@
limitations under the License. limitations under the License.
--> -->
<#if methodRefChild??>
<#-- the nested case: mapping method -->
${openExpression}<@includeModel object=methodRefChild source=ext.source targetType=ext.targetType/>${closeExpression}
<#else>
<#-- the non nested case: a type conversion -->
${openExpression}${sourceReference}${closeExpression} ${openExpression}${sourceReference}${closeExpression}
</#if>

View File

@ -83,6 +83,22 @@ public class NestedMappingMethodInvocationTest {
assertThat( target.getOrderDetails().getDescription() ).containsExactly( "elem1", "elem2" ); assertThat( target.getOrderDetails().getDescription() ).containsExactly( "elem1", "elem2" );
} }
@Test
@WithClasses( {
SourceTypeTargetDtoMapper.class,
SourceType.class,
ObjectFactory.class,
TargetDto.class
} )
public void shouldMapViaMethodAndConversion() throws DatatypeConfigurationException {
SourceTypeTargetDtoMapper instance = SourceTypeTargetDtoMapper.INSTANCE;
TargetDto target = instance.sourceToTarget( createSource() );
assertThat( target ).isNotNull();
assertThat( target.getDate() ).isEqualTo( new GregorianCalendar( 2013, 6, 6 ).getTime() );
}
@Test @Test
@WithClasses( { @WithClasses( {
SourceTypeTargetDtoMapper.class, SourceTypeTargetDtoMapper.class,
@ -143,6 +159,12 @@ public class NestedMappingMethodInvocationTest {
.newXMLGregorianCalendarDate( year, month, day, DatatypeConstants.FIELD_UNDEFINED ); .newXMLGregorianCalendarDate( year, month, day, DatatypeConstants.FIELD_UNDEFINED );
} }
private SourceType createSource() {
SourceType source = new SourceType();
source.setDate( new JAXBElement<String>( QNAME, String.class, "06.07.2013" ) );
return source;
}
private TargetDto createTarget() { private TargetDto createTarget() {
TargetDto target = new TargetDto(); TargetDto target = new TargetDto();
target.setDate( new GregorianCalendar( 2013, 6, 6 ).getTime() ); target.setDate( new GregorianCalendar( 2013, 6, 6 ).getTime() );

View File

@ -32,7 +32,7 @@ public interface SourceTypeTargetDtoMapper {
SourceTypeTargetDtoMapper INSTANCE = Mappers.getMapper( SourceTypeTargetDtoMapper.class ); SourceTypeTargetDtoMapper INSTANCE = Mappers.getMapper( SourceTypeTargetDtoMapper.class );
@Mapping(source = "date", target = "date", dateFormat = "dd.MM.yyyy") @Mapping(source = "date", target = "date", dateFormat = "dd.MM.yyyy")
TargetDto sourceToTarget(SourceType source);
SourceType targetToSource( TargetDto source ); SourceType targetToSource( TargetDto source );
// TargetDto sourceToTarget(SourceType source);
} }