From 76c3aebd5d0dd8b8233885e5d84efeea2c70fdb9 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 9 Jun 2013 21:49:25 +0200 Subject: [PATCH] #35 Unifying messsage printing --- .../mapstruct/ap/MapperGenerationVisitor.java | 58 ++++++++++++------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java index d3fa131b3..58a1beaa9 100644 --- a/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java +++ b/processor/src/main/java/org/mapstruct/ap/MapperGenerationVisitor.java @@ -41,7 +41,6 @@ import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementKindVisitor6; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; -import javax.tools.Diagnostic.Kind; import javax.tools.JavaFileObject; import org.mapstruct.ap.conversion.Conversion; @@ -327,7 +326,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { ( property.getTargetType().isCollectionType() && property.getTargetType() .getCollectionImplementationType() != null ) ) ) { - reportError( + printMessage( + ReportingPolicy.ERROR, String.format( "Can't map property \"%s %s\" to \"%s %s\".", property.getSourceType(), @@ -349,7 +349,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { ( property.getSourceType().isCollectionType() && property.getSourceType() .getCollectionImplementationType() != null ) ) ) { - reportError( + printMessage( + ReportingPolicy.ERROR, String.format( "Can't map property \"%s %s\" to \"%s %s\".", property.getTargetType(), @@ -457,19 +458,35 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { if ( implementationRequired ) { if ( parameter.getType().isIterableType() && !returnType.isIterableType() ) { - reportError( "Can't generate mapping method from iterable type to non-iterable type.", method ); + printMessage( + ReportingPolicy.ERROR, + "Can't generate mapping method from iterable type to non-iterable type.", + method + ); mappingErroneous = true; } if ( !parameter.getType().isIterableType() && returnType.isIterableType() ) { - reportError( "Can't generate mapping method from non-iterable type to iterable type.", method ); + printMessage( + ReportingPolicy.ERROR, + "Can't generate mapping method from non-iterable type to iterable type.", + method + ); mappingErroneous = true; } if ( parameter.getType().isPrimitive() ) { - reportError( "Can't generate mapping method with primitive parameter type.", method ); + printMessage( + ReportingPolicy.ERROR, + "Can't generate mapping method with primitive parameter type.", + method + ); mappingErroneous = true; } if ( returnType.isPrimitive() ) { - reportError( "Can't generate mapping method with primitive return type.", method ); + printMessage( + ReportingPolicy.ERROR, + "Can't generate mapping method with primitive return type.", + method + ); mappingErroneous = true; } @@ -605,7 +622,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { for ( Mapping mappedProperty : mappings.values() ) { if ( !sourcePropertyNames.contains( mappedProperty.getSourceName() ) ) { - reportError( + printMessage( + ReportingPolicy.ERROR, String.format( "Unknown property \"%s\" in parameter type %s.", mappedProperty.getSourceName(), @@ -614,7 +632,8 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { ); } if ( !targetPropertyNames.contains( mappedProperty.getTargetName() ) ) { - reportError( + printMessage( + ReportingPolicy.ERROR, String.format( "Unknown property \"%s\" in return type %s.", mappedProperty.getTargetName(), @@ -670,22 +689,19 @@ public class MapperGenerationVisitor extends ElementKindVisitor6 { return typeUtil.retrieveType( method.getReturnType() ); } - private void reportError(String message, Element element) { - processingEnvironment.getMessager().printMessage( Kind.ERROR, message, element ); - mappingErroneous = true; - } - - private void reportError(String message, Element element, AnnotationMirror annotationMirror, - AnnotationValue annotationValue) { - processingEnvironment.getMessager() - .printMessage( Kind.ERROR, message, element, annotationMirror, annotationValue ); - mappingErroneous = true; - } - private void printMessage(ReportingPolicy reportingPolicy, String message, Element element) { processingEnvironment.getMessager().printMessage( reportingPolicy.getDiagnosticKind(), message, element ); if ( reportingPolicy.failsBuild() ) { mappingErroneous = true; } } + + private void printMessage(ReportingPolicy reportingPolicy, String message, Element element, + AnnotationMirror annotationMirror, AnnotationValue annotationValue) { + processingEnvironment.getMessager() + .printMessage( reportingPolicy.getDiagnosticKind(), message, element, annotationMirror, annotationValue ); + if ( reportingPolicy.failsBuild() ) { + mappingErroneous = true; + } + } }