This commit is contained in:
Gunnar Morling 2014-10-12 18:24:17 +02:00
parent 9adbb423c6
commit a5dc5d78ab
5 changed files with 282 additions and 264 deletions

View File

@ -27,8 +27,8 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.source.Mapping; import org.mapstruct.ap.model.source.Mapping;

View File

@ -18,11 +18,12 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import org.mapstruct.ap.model.assignment.Assignment;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import org.mapstruct.ap.model.assignment.Assignment;
import org.mapstruct.ap.model.assignment.SetterWrapper; import org.mapstruct.ap.model.assignment.SetterWrapper;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
@ -77,7 +78,8 @@ public class IterableMappingMethod extends MappingMethod {
Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() ); Strings.getSaveVariableName( sourceElementType.getName(), method.getParameterNames() );
Assignment assignment = ctx.getMappingResolver().getTargetAssignment( method, Assignment assignment = ctx.getMappingResolver().getTargetAssignment(
method,
"collection element", "collection element",
sourceElementType, sourceElementType,
targetElementType, targetElementType,

View File

@ -18,11 +18,12 @@
*/ */
package org.mapstruct.ap.model; package org.mapstruct.ap.model;
import org.mapstruct.ap.model.assignment.Assignment;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import org.mapstruct.ap.model.assignment.Assignment;
import org.mapstruct.ap.model.assignment.LocalVarWrapper; import org.mapstruct.ap.model.assignment.LocalVarWrapper;
import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Parameter;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
@ -90,7 +91,8 @@ public class MapMappingMethod extends MappingMethod {
Type keySourceType = sourceTypeParams.get( 0 ); Type keySourceType = sourceTypeParams.get( 0 );
Type keyTargetType = resultTypeParams.get( 0 ); Type keyTargetType = resultTypeParams.get( 0 );
Assignment keyAssignment = ctx.getMappingResolver().getTargetAssignment( method, Assignment keyAssignment = ctx.getMappingResolver().getTargetAssignment(
method,
"map key", "map key",
keySourceType, keySourceType,
keyTargetType, keyTargetType,
@ -101,8 +103,10 @@ public class MapMappingMethod extends MappingMethod {
); );
if ( keyAssignment == null ) { if ( keyAssignment == null ) {
String message = String.format( "Can't create implementation of method %s. Found no method nor " String message = String.format(
+ "built-in conversion for mapping source key type to target key type.", method ); "Can't create implementation of method %s. Found no method nor "
+ "built-in conversion for mapping source key type to target key type.", method
);
method.printMessage( ctx.getMessager(), Diagnostic.Kind.ERROR, message ); method.printMessage( ctx.getMessager(), Diagnostic.Kind.ERROR, message );
} }
@ -110,7 +114,8 @@ public class MapMappingMethod extends MappingMethod {
Type valueSourceType = sourceTypeParams.get( 1 ); Type valueSourceType = sourceTypeParams.get( 1 );
Type valueTargetType = resultTypeParams.get( 1 ); Type valueTargetType = resultTypeParams.get( 1 );
Assignment valueAssignment = ctx.getMappingResolver().getTargetAssignment( method, Assignment valueAssignment = ctx.getMappingResolver().getTargetAssignment(
method,
"map value", "map value",
valueSourceType, valueSourceType,
valueTargetType, valueTargetType,
@ -121,8 +126,10 @@ public class MapMappingMethod extends MappingMethod {
); );
if ( valueAssignment == null ) { if ( valueAssignment == null ) {
String message = String.format( "Can't create implementation of method %s. Found no method nor " String message = String.format(
+ "built-in conversion for mapping source value type to target value type.", method ); "Can't create implementation of method %s. Found no method nor "
+ "built-in conversion for mapping source value type to target value type.", method
);
method.printMessage( ctx.getMessager(), Diagnostic.Kind.ERROR, message ); method.printMessage( ctx.getMessager(), Diagnostic.Kind.ERROR, message );
} }

View File

@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.TreeSet; import java.util.TreeSet;
import javax.annotation.processing.Messager; import javax.annotation.processing.Messager;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
@ -88,7 +87,14 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
typeUtils, typeUtils,
messager, messager,
options, options,
new MappingResolverImpl( context.getMessager(), elementUtils, typeUtils, typeFactory, sourceModel, mapperReferences ), new MappingResolverImpl(
context.getMessager(),
elementUtils,
typeUtils,
typeFactory,
sourceModel,
mapperReferences
),
mapperTypeElement, mapperTypeElement,
sourceModel, sourceModel,
mapperReferences mapperReferences

View File

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.annotation.processing.Messager; import javax.annotation.processing.Messager;
import javax.lang.model.type.TypeMirror; import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
@ -74,7 +73,8 @@ public class MappingResolverImpl implements MappingResolver {
*/ */
private final Set<VirtualMappingMethod> usedVirtualMappings = new HashSet<VirtualMappingMethod>(); private final Set<VirtualMappingMethod> usedVirtualMappings = new HashSet<VirtualMappingMethod>();
public MappingResolverImpl(Messager messager, Elements elementUtils, Types typeUtils, TypeFactory typeFactory, List<SourceMethod> sourceModel, List<MapperReference> mapperReferences) { public MappingResolverImpl(Messager messager, Elements elementUtils, Types typeUtils, TypeFactory typeFactory,
List<SourceMethod> sourceModel, List<MapperReference> mapperReferences) {
this.messager = messager; this.messager = messager;
this.typeUtils = typeUtils; this.typeUtils = typeUtils;
this.typeFactory = typeFactory; this.typeFactory = typeFactory;
@ -122,7 +122,8 @@ public class MappingResolverImpl implements MappingResolver {
List<TypeMirror> qualifiers, List<TypeMirror> qualifiers,
String sourceReference) { String sourceReference) {
ResolvingAttempt attempt = new ResolvingAttempt( sourceModel, ResolvingAttempt attempt = new ResolvingAttempt(
sourceModel,
mapperReferences, mapperReferences,
mappingMethod, mappingMethod,
mappedElement, mappedElement,
@ -236,7 +237,6 @@ public class MappingResolverImpl implements MappingResolver {
/** /**
* Returns a reference to a method mapping the given source type to the given target type, if such a method * Returns a reference to a method mapping the given source type to the given target type, if such a method
* exists. * exists.
*
*/ */
private Assignment resolveViaMethod(Type sourceType, Type targetType) { private Assignment resolveViaMethod(Type sourceType, Type targetType) {
@ -288,8 +288,10 @@ public class MappingResolverImpl implements MappingResolver {
// 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( methodYCandidate.getSourceParameters().get( 0 ).getType(), methodRefY = resolveViaMethod(
targetType ); methodYCandidate.getSourceParameters().get( 0 ).getType(),
targetType
);
if ( methodRefY != null ) { if ( methodRefY != null ) {
Assignment methodRefX = resolveViaMethod( Assignment methodRefX = resolveViaMethod(
sourceType, sourceType,
@ -412,7 +414,8 @@ public class MappingResolverImpl implements MappingResolver {
"Ambiguous mapping methods found for mapping " + mappedElement + " from %s to %s: %s.", "Ambiguous mapping methods found for mapping " + mappedElement + " from %s to %s: %s.",
sourceType, sourceType,
returnType, returnType,
Strings.join( candidates, ", " ) ); Strings.join( candidates, ", " )
);
mappingMethod.printMessage( messager, Kind.ERROR, errorMsg ); mappingMethod.printMessage( messager, Kind.ERROR, errorMsg );
} }