From d61ca26652113b973d2e6318f0364c134add9fe4 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Mon, 2 Feb 2015 21:27:46 +0100 Subject: [PATCH] #339 introduce centralized enum for messages --- .../mapstruct/ap/model/BeanMappingMethod.java | 47 ++--- .../mapstruct/ap/model/EnumMappingMethod.java | 67 +++---- .../ap/model/IterableMappingMethod.java | 9 +- .../mapstruct/ap/model/MapMappingMethod.java | 15 +- .../ap/model/MappingBuilderContext.java | 8 +- .../mapstruct/ap/model/PropertyMapping.java | 38 ++-- .../common/DateFormatValidationResult.java | 16 +- .../common/DateFormatValidatorFactory.java | 14 +- .../common/DefaultConversionContext.java | 13 +- .../ap/model/source/BeanMapping.java | 17 +- .../ap/model/source/IterableMapping.java | 12 +- .../mapstruct/ap/model/source/MapMapping.java | 13 +- .../mapstruct/ap/model/source/Mapping.java | 66 +++---- .../ap/model/source/SourceMethod.java | 10 +- .../ap/model/source/SourceReference.java | 25 +-- .../DefaultModelElementProcessorContext.java | 28 ++- .../ap/processor/MapperCreationProcessor.java | 186 +++++++----------- .../processor/MethodRetrievalProcessor.java | 55 ++---- .../ap/processor/ModelElementProcessor.java | 4 +- .../creation/MappingResolverImpl.java | 35 ++-- .../util/AnnotationProcessingException.java | 3 +- .../mapstruct/ap/util/FormattingMessager.java | 83 ++++++++ .../java/org/mapstruct/ap/util/Message.java | 100 ++++++++++ .../common/DefaultConversionContextTest.java | 15 +- 24 files changed, 481 insertions(+), 398 deletions(-) create mode 100644 processor/src/main/java/org/mapstruct/ap/util/FormattingMessager.java create mode 100644 processor/src/main/java/org/mapstruct/ap/util/Message.java diff --git a/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java index 6420e11ae..ceb329b36 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/BeanMappingMethod.java @@ -43,6 +43,7 @@ import org.mapstruct.ap.option.ReportingPolicy; import org.mapstruct.ap.prism.BeanMappingPrism; import org.mapstruct.ap.prism.CollectionMappingStrategyPrism; import org.mapstruct.ap.prism.NullValueMappingPrism; +import org.mapstruct.ap.util.Message; import org.mapstruct.ap.util.Executables; import org.mapstruct.ap.util.MapperConfig; import org.mapstruct.ap.util.Strings; @@ -119,11 +120,10 @@ public class BeanMappingMethod extends MappingMethod { if ( beanMapping != null && beanMapping.getResultType() != null ) { resultType = ctx.getTypeFactory().getType( beanMapping.getResultType() ); if ( !resultType.isAssignableTo( method.getResultType() ) ) { - ctx.getMessager().printMessage( - Diagnostic.Kind.ERROR, - String.format( "%s not assignable to: %s.", resultType, method.getResultType() ), + ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, method.getExecutable(), - beanMappingPrism.mirror ); + beanMappingPrism.mirror, + Message.beanmapping_notassignable, resultType, method.getResultType()); } } } @@ -154,15 +154,12 @@ public class BeanMappingMethod extends MappingMethod { // fetch the target property ExecutableElement targetProperty = unprocessedTargetProperties.get( mapping.getTargetName() ); if ( targetProperty == null ) { - ctx.getMessager().printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Unknown property \"%s\" in return type.", - mapping.getTargetName() - ), + ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, method.getExecutable(), mapping.getMirror(), - mapping.getSourceAnnotationValue() + mapping.getSourceAnnotationValue(), + Message.beanmapping_unknownpropertyinreturntype, + mapping.getTargetName() ); errorOccurred = true; } @@ -313,12 +310,10 @@ public class BeanMappingMethod extends MappingMethod { if ( propertyMapping != null && newPropertyMapping != null ) { // TODO improve error message - ctx.getMessager().printMessage( - Diagnostic.Kind.ERROR, - "Several possible source properties for target property \"" - + targetProperty.getKey() - + "\".", - method.getExecutable() + ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, + method.getExecutable(), + Message.beanmapping_severalpossiblesources, + targetProperty.getKey() ); break; } @@ -394,10 +389,10 @@ public class BeanMappingMethod extends MappingMethod { } // Should never really happen else { - ctx.getMessager().printMessage( - Diagnostic.Kind.ERROR, - String.format( "Found several matching getters for property \"%s\"", sourcePropertyName ), - method.getExecutable() + ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, + method.getExecutable(), + Message.beanmapping_severalpossibletargetaccessors, + sourcePropertyName ); return null; @@ -435,14 +430,14 @@ public class BeanMappingMethod extends MappingMethod { if ( !unprocessedTargetProperties.isEmpty() && unmappedTargetPolicy.requiresReport() ) { - ctx.getMessager().printMessage( - unmappedTargetPolicy.getDiagnosticKind(), + ctx.getMessager().printMessage( unmappedTargetPolicy.getDiagnosticKind(), + method.getExecutable(), + Message.beanmapping_unmappedtargets, MessageFormat.format( - "Unmapped target {0,choice,1#property|1 sourceModel; @@ -123,7 +123,7 @@ public class MappingBuilderContext { public MappingBuilderContext(TypeFactory typeFactory, Elements elementUtils, Types typeUtils, - Messager messager, + FormattingMessager messager, Options options, MappingResolver mappingResolver, TypeElement mapper, @@ -164,7 +164,7 @@ public class MappingBuilderContext { return typeUtils; } - public Messager getMessager() { + public FormattingMessager getMessager() { return messager; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java index 4afa07012..9ab518699 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -46,6 +46,7 @@ import org.mapstruct.ap.util.Strings; import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.DIRECT; import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED; import static org.mapstruct.ap.model.assignment.Assignment.AssignmentType.TYPE_CONVERTED_MAPPED; +import org.mapstruct.ap.util.Message; /** * Represents the mapping between a source and target property, e.g. from {@code String Source#foo} to @@ -171,18 +172,14 @@ public class PropertyMapping extends ModelElement { } } else { - ctx.getMessager().printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Can't map %s to \"%s %s\". " - + "Consider to declare/implement a mapping method: \"%s map(%s value)\".", - sourceElement, - targetType, - targetPropertyName, - targetType, - getSourceType() /* original source type */ - ), - method.getExecutable() + ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, + method.getExecutable(), + Message.propertymapping_mappingnotfound, + sourceElement, + targetType, + targetPropertyName, + targetType, + getSourceType() /* original source type */ ); } @@ -534,16 +531,13 @@ public class PropertyMapping extends ModelElement { } } else { - ctx.getMessager().printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Can't map \"%s %s\" to \"%s %s\".", - sourceType, - constantExpression, - targetType, - targetPropertyName - ), - method.getExecutable() + ctx.getMessager().printMessage( Diagnostic.Kind.ERROR, + method.getExecutable(), + Message.constantmapping_mappingnotfound, + sourceType, + constantExpression, + targetType, + targetPropertyName ); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidationResult.java b/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidationResult.java index 7855f5cc8..d38f926a0 100755 --- a/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidationResult.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidationResult.java @@ -18,13 +18,18 @@ */ package org.mapstruct.ap.model.common; +import javax.tools.Diagnostic; +import org.mapstruct.ap.util.Message; +import org.mapstruct.ap.util.FormattingMessager; + /** * Reflects the result of a date format validation */ final class DateFormatValidationResult { private final boolean isValid; - private final String validationInformation; + private final Message validationInfo; + private final Object[] validationInfoArgs; /** * Create a new instance. @@ -32,18 +37,19 @@ final class DateFormatValidationResult { * @param isValid determines of the validation was successful. * @param validationInformation a string representing the validation result */ - DateFormatValidationResult(boolean isValid, String validationInformation) { + DateFormatValidationResult(boolean isValid, Message validationInformation, Object... infoArgs) { this.isValid = isValid; - this.validationInformation = validationInformation; + this.validationInfo = validationInformation; + this.validationInfoArgs = infoArgs; } public boolean isValid() { return isValid; } - public String validationInformation() { - return validationInformation; + public void printErrorMessage(FormattingMessager messager) { + messager.printMessage( Diagnostic.Kind.ERROR, validationInfo, validationInfoArgs ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidatorFactory.java b/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidatorFactory.java index 323dd4f58..512b5b999 100755 --- a/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidatorFactory.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/DateFormatValidatorFactory.java @@ -24,6 +24,7 @@ import org.mapstruct.ap.util.JodaTimeConstants; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; +import org.mapstruct.ap.util.Message; /** * Factory for {@link DateFormatValidator}.

Based on the types of source / target type a specific {@link @@ -73,9 +74,8 @@ final class DateFormatValidatorFactory { dateFormatValidator = new DateFormatValidator() { @Override public DateFormatValidationResult validate(String dateFormat) { - return new DateFormatValidationResult( - true, String.format( - "No dateFormat cheeck is supported for types %s, %s", sourceType, targetType ) ); + return new DateFormatValidationResult( true, Message.general_unsupporteddateformatcheck, + sourceType, targetType ); } }; } @@ -179,14 +179,10 @@ final class DateFormatValidatorFactory { } private static DateFormatValidationResult validDateFormat(String dateFormat) { - return new DateFormatValidationResult( - true, String.format( - "given date format \"%s\" is valid.", dateFormat ) ); + return new DateFormatValidationResult( true, Message.general_validdate, dateFormat ); } private static DateFormatValidationResult invalidDateFormat(String dateFormat, Throwable e) { - return new DateFormatValidationResult( - false, String.format( - "given date format \"%s\" is invalid. Message: \"%s\"", dateFormat, e.getMessage() ) ); + return new DateFormatValidationResult( false, Message.general_invaliddate, dateFormat, e.getMessage() ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/common/DefaultConversionContext.java b/processor/src/main/java/org/mapstruct/ap/model/common/DefaultConversionContext.java index 039cc64f4..f00370c8d 100755 --- a/processor/src/main/java/org/mapstruct/ap/model/common/DefaultConversionContext.java +++ b/processor/src/main/java/org/mapstruct/ap/model/common/DefaultConversionContext.java @@ -20,8 +20,7 @@ package org.mapstruct.ap.model.common; import org.mapstruct.ap.util.Strings; -import javax.annotation.processing.Messager; -import javax.tools.Diagnostic; +import org.mapstruct.ap.util.FormattingMessager; /** * Default implementation of the {@link ConversionContext} passed to conversion providers. @@ -30,14 +29,14 @@ import javax.tools.Diagnostic; */ public class DefaultConversionContext implements ConversionContext { - private final Messager messager; + private final FormattingMessager messager; private final Type sourceType; private final Type targetType; private final String dateFormat; private final TypeFactory typeFactory; - public DefaultConversionContext(TypeFactory typeFactory, Messager messager, Type sourceType, Type targetType, - String dateFormat) { + public DefaultConversionContext(TypeFactory typeFactory, FormattingMessager messager, Type sourceType, + Type targetType, String dateFormat) { this.typeFactory = typeFactory; this.messager = messager; this.sourceType = sourceType; @@ -55,7 +54,7 @@ public class DefaultConversionContext implements ConversionContext { DateFormatValidationResult validationResult = dateFormatValidator.validate( dateFormat ); if ( !validationResult.isValid() ) { - messager.printMessage( Diagnostic.Kind.ERROR, validationResult.validationInformation() ); + validationResult.printErrorMessage( messager ); } } } @@ -75,7 +74,7 @@ public class DefaultConversionContext implements ConversionContext { return typeFactory; } - protected Messager getMessager() { + protected FormattingMessager getMessager() { return messager; } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/BeanMapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/BeanMapping.java index 05ff4a0c4..30f12c090 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/BeanMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/BeanMapping.java @@ -19,12 +19,13 @@ package org.mapstruct.ap.model.source; import java.util.List; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; import org.mapstruct.ap.prism.BeanMappingPrism; +import org.mapstruct.ap.util.Message; /** @@ -37,19 +38,15 @@ public class BeanMapping { private final List qualifiers; private final TypeMirror resultType; - public static BeanMapping fromPrism( BeanMappingPrism beanMapping, ExecutableElement method, Messager messager ) { + public static BeanMapping fromPrism( BeanMappingPrism beanMapping, ExecutableElement method, + FormattingMessager messager ) { if ( beanMapping == null ) { return null; } boolean resultTypeIsDefined = !TypeKind.VOID.equals( beanMapping.resultType().getKind() ); if ( !resultTypeIsDefined && beanMapping.qualifiedBy().isEmpty() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "'resultType' and 'qualifiedBy' are undefined in @BeanMapping, " - + "define at least one of them.", - method - ); + messager.printMessage( Diagnostic.Kind.ERROR, method, Message.beanmapping_noelements ); } return new BeanMapping( @@ -58,9 +55,7 @@ public class BeanMapping { ); } - private BeanMapping( - List qualifiers, - TypeMirror mirror) { + private BeanMapping( List qualifiers, TypeMirror mirror) { this.qualifiers = qualifiers; this.resultType = mirror; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/IterableMapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/IterableMapping.java index 3b73958ea..59767f477 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/IterableMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/IterableMapping.java @@ -19,7 +19,7 @@ package org.mapstruct.ap.model.source; import java.util.List; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.ExecutableElement; @@ -28,6 +28,7 @@ import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; import org.mapstruct.ap.prism.IterableMappingPrism; +import org.mapstruct.ap.util.Message; /** * Represents an iterable mapping as configured via {@code @IterableMapping}. @@ -43,7 +44,7 @@ public class IterableMapping { private final AnnotationValue dateFormatAnnotationValue; public static IterableMapping fromPrism(IterableMappingPrism iterableMapping, ExecutableElement method, - Messager messager) { + FormattingMessager messager) { if ( iterableMapping == null ) { return null; } @@ -52,12 +53,7 @@ public class IterableMapping { if ( !elementTargetTypeIsDefined && iterableMapping.dateFormat().isEmpty() && iterableMapping.qualifiedBy().isEmpty() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "'dateformat', 'qualifiedBy' and 'elementTargetType' are undefined in @IterableMapping, " - + "define at least one of them.", - method - ); + messager.printMessage( Diagnostic.Kind.ERROR, method, Message.iterablemapping_noelements ); } return new IterableMapping( diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/MapMapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/MapMapping.java index e5740cd0d..901705a13 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/MapMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/MapMapping.java @@ -19,7 +19,7 @@ package org.mapstruct.ap.model.source; import java.util.List; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.TypeKind; @@ -27,6 +27,7 @@ import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; import org.mapstruct.ap.prism.MapMappingPrism; +import org.mapstruct.ap.util.Message; /** * Represents a map mapping as configured via {@code @MapMapping}. @@ -43,7 +44,8 @@ public class MapMapping { private final TypeMirror keyQualifyingTargetType; private final TypeMirror valueQualifyingTargetType; - public static MapMapping fromPrism(MapMappingPrism mapMapping, ExecutableElement method, Messager messager) { + public static MapMapping fromPrism(MapMappingPrism mapMapping, ExecutableElement method, + FormattingMessager messager) { if ( mapMapping == null ) { return null; } @@ -56,12 +58,7 @@ public class MapMapping { && mapMapping.valueQualifiedBy().isEmpty() && !keyTargetTypeIsDefined && !valueTargetTypeIsDefined ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "'keyDateFormat', 'keyQualifiedBy', 'keyTargetType', 'valueDateFormat', 'valueQualfiedBy' and " - + "'valueTargetType' are all undefined in @MapMapping, define at least one of them.", - method - ); + messager.printMessage( Diagnostic.Kind.ERROR, method, Message.mapmapping_noelements ); } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java index 37e20bbb4..28be2dc08 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/Mapping.java @@ -24,7 +24,7 @@ import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.ElementKind; @@ -39,6 +39,7 @@ import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.prism.CollectionMappingStrategyPrism; import org.mapstruct.ap.prism.MappingPrism; import org.mapstruct.ap.prism.MappingsPrism; +import org.mapstruct.ap.util.Message; /** * Represents a property mapping as configured via {@code @Mapping}. @@ -66,7 +67,7 @@ public class Mapping { public static Map> fromMappingsPrism(MappingsPrism mappingsAnnotation, ExecutableElement method, - Messager messager) { + FormattingMessager messager) { Map> mappings = new HashMap>(); for ( MappingPrism mappingPrism : mappingsAnnotation.value() ) { @@ -81,10 +82,10 @@ public class Mapping { mappingsOfProperty.add( mapping ); if ( mappingsOfProperty.size() > 1 && !isEnumType( method.getReturnType() ) ) { - messager.printMessage( - Kind.ERROR, - "Target property \"" + mappingPrism.target() + "\" must not be mapped more than once.", - method + messager.printMessage( Kind.ERROR, + method, + Message.propertymapping_duplicatetargets, + mappingPrism.target() ); } } @@ -93,40 +94,37 @@ public class Mapping { return mappings; } - public static Mapping fromMappingPrism(MappingPrism mappingPrism, ExecutableElement element, Messager messager) { + public static Mapping fromMappingPrism(MappingPrism mappingPrism, ExecutableElement element, + FormattingMessager messager) { if ( mappingPrism.target().isEmpty() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "Target must not be empty in @Mapping", + messager.printMessage( Diagnostic.Kind.ERROR, element, mappingPrism.mirror, - mappingPrism.values.target() + mappingPrism.values.target(), + Message.propertymapping_emptytarget ); return null; } if ( !mappingPrism.source().isEmpty() && !mappingPrism.constant().isEmpty() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "Source and constant are both defined in @Mapping, either define a source or a constant", - element + messager.printMessage( Diagnostic.Kind.ERROR, + element, + Message.propertymapping_sourceandconstantbothdefined ); return null; } else if ( !mappingPrism.source().isEmpty() && !mappingPrism.expression().isEmpty() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "Source and expression are both defined in @Mapping, either define a source or an expression", - element + messager.printMessage( Diagnostic.Kind.ERROR, + element, + Message.propertymapping_sourceandexpressionbothdefined ); return null; } else if ( !mappingPrism.expression().isEmpty() && !mappingPrism.constant().isEmpty() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "Expression and constant are both defined in @Mapping, either define an expression or a constant", - element + messager.printMessage( Diagnostic.Kind.ERROR, + element, + Message.propertymapping_expressionandconstantbothdefined ); return null; } @@ -173,7 +171,8 @@ public class Mapping { this.resultType = resultType; } - private static String getExpression(MappingPrism mappingPrism, ExecutableElement element, Messager messager) { + private static String getExpression(MappingPrism mappingPrism, ExecutableElement element, + FormattingMessager messager) { if ( mappingPrism.expression().isEmpty() ) { return null; } @@ -181,12 +180,11 @@ public class Mapping { Matcher javaExpressionMatcher = JAVA_EXPRESSION.matcher( mappingPrism.expression() ); if ( !javaExpressionMatcher.matches() ) { - messager.printMessage( - Diagnostic.Kind.ERROR, - "Value must be given in the form \"java()\"", + messager.printMessage( Diagnostic.Kind.ERROR, element, mappingPrism.mirror, - mappingPrism.values.expression() + mappingPrism.values.expression(), + Message.propertymapping_invalidexpression ); return null; } @@ -199,7 +197,7 @@ public class Mapping { ( (DeclaredType) mirror ).asElement().getKind() == ElementKind.ENUM; } - public void init(SourceMethod method, Messager messager, TypeFactory typeFactory) { + public void init(SourceMethod method, FormattingMessager messager, TypeFactory typeFactory) { if ( !method.isEnumMapping() ) { sourceReference = new SourceReference.BuilderFromMapping() @@ -274,7 +272,7 @@ public class Mapping { return method.getResultType().getTargetAccessors( cms ).containsKey( name ); } - public Mapping reverse(SourceMethod method, Messager messager, TypeFactory typeFactory) { + public Mapping reverse(SourceMethod method, FormattingMessager messager, TypeFactory typeFactory) { // mapping can only be reversed if the source was not a constant nor an expression nor a nested property if ( constant != null || javaExpression != null ) { @@ -297,10 +295,10 @@ public class Mapping { if ( sourceReference != null && sourceReference.getPropertyEntries().isEmpty() ) { // parameter mapping only, apparently the @InheritReverseConfiguration is intentional // but erroneous. Lets raise an error to warn. - messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( "Parameter %s cannot be reversed", sourceReference.getParameter() ), - method.getExecutable() + messager.printMessage( Diagnostic.Kind.ERROR, + method.getExecutable(), + Message.propertymapping_reversalproblem, + sourceReference.getParameter() ); return null; } diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java index f2bb2099d..3f6e493e9 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/SourceMethod.java @@ -23,7 +23,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.util.Types; @@ -50,7 +50,7 @@ public class SourceMethod implements Method { private final Types typeUtils; private final TypeFactory typeFactory; - private final Messager messager; + private final FormattingMessager messager; private final Type declaringMapper; private final ExecutableElement executable; @@ -77,7 +77,7 @@ public class SourceMethod implements Method { private MapMapping mapMapping = null; private Types typeUtils; private TypeFactory typeFactory = null; - private Messager messager = null; + private FormattingMessager messager = null; private MapperConfig mapperConfig = null; public Builder() { @@ -133,7 +133,7 @@ public class SourceMethod implements Method { return this; } - public Builder setMessager(Messager messager) { + public Builder setMessager(FormattingMessager messager) { this.messager = messager; return this; } @@ -174,7 +174,7 @@ public class SourceMethod implements Method { private SourceMethod( Type declaringMapper, ExecutableElement executable, List parameters, Type returnType, List exceptionTypes, Map> mappings, IterableMapping iterableMapping, MapMapping mapMapping, Types typeUtils, - TypeFactory typeFactory, Messager messager, MapperConfig config) { + TypeFactory typeFactory, FormattingMessager messager, MapperConfig config) { this.declaringMapper = declaringMapper; this.executable = executable; this.parameters = parameters; diff --git a/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java b/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java index 55acae48c..53186ce0b 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java +++ b/processor/src/main/java/org/mapstruct/ap/model/source/SourceReference.java @@ -22,13 +22,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.ExecutableElement; import javax.tools.Diagnostic; import org.mapstruct.ap.model.common.Parameter; import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.TypeFactory; +import org.mapstruct.ap.util.Message; import org.mapstruct.ap.util.Executables; import org.mapstruct.ap.util.Strings; @@ -67,10 +68,10 @@ public class SourceReference { private Mapping mapping; private SourceMethod method; - private Messager messager; + private FormattingMessager messager; private TypeFactory typeFactory; - public BuilderFromMapping messager(Messager messager) { + public BuilderFromMapping messager(FormattingMessager messager) { this.messager = messager; return this; } @@ -114,7 +115,7 @@ public class SourceReference { String sourceParameterName = segments[0]; parameter = method.getSourceParameter( sourceParameterName ); if ( parameter == null ) { - reportMappingError( "Method has no parameter named \"%s\".", sourceParameterName ); + reportMappingError( Message.propertymapping_invalidparametername, sourceParameterName ); isValid = false; } } @@ -154,15 +155,13 @@ public class SourceReference { if ( !foundEntryMatch ) { if ( parameter != null ) { - reportMappingError( - "The type of parameter \"%s\" has no property named \"%s\".", + reportMappingError( Message.propertymapping_nopropertyinparameter, parameter.getName(), Strings.join( Arrays.asList( sourcePropertyNames ), "." ) ); } else { - reportMappingError( - "No property named \"%s\" exists in source parameter(s).", + reportMappingError( Message.propertymapping_invalidpropertyname, mapping.getSourceName() ); } @@ -193,12 +192,14 @@ public class SourceReference { return sourceEntries; } - private void reportMappingError(String message, Object... objects) { + private void reportMappingError(Message msg, Object... objects) { messager.printMessage( Diagnostic.Kind.ERROR, - String.format( message, objects ), - method.getExecutable(), mapping.getMirror(), - mapping.getSourceAnnotationValue() + method.getExecutable(), + mapping.getMirror(), + mapping.getSourceAnnotationValue(), + msg, + objects ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/processor/DefaultModelElementProcessorContext.java b/processor/src/main/java/org/mapstruct/ap/processor/DefaultModelElementProcessorContext.java index 0523344fa..1acda1597 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/DefaultModelElementProcessorContext.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/DefaultModelElementProcessorContext.java @@ -18,6 +18,7 @@ */ package org.mapstruct.ap.processor; +import org.mapstruct.ap.util.FormattingMessager; import javax.annotation.processing.Filer; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; @@ -31,6 +32,7 @@ import javax.tools.Diagnostic.Kind; import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.option.Options; import org.mapstruct.ap.processor.ModelElementProcessor.ProcessorContext; +import org.mapstruct.ap.util.Message; import org.mapstruct.ap.version.VersionInformation; /** @@ -78,7 +80,7 @@ public class DefaultModelElementProcessorContext implements ProcessorContext { } @Override - public Messager getMessager() { + public FormattingMessager getMessager() { return messager; } @@ -97,7 +99,7 @@ public class DefaultModelElementProcessorContext implements ProcessorContext { return messager.isErroneous(); } - private static class DelegatingMessager implements Messager { + private static class DelegatingMessager implements FormattingMessager { private final Messager delegate; private boolean isErroneous = false; @@ -107,32 +109,37 @@ public class DefaultModelElementProcessorContext implements ProcessorContext { } @Override - public void printMessage(Kind kind, CharSequence msg) { - delegate.printMessage( kind, msg ); + public void printMessage( Kind kind, Message msg, Object... args) { + String message = String.format( msg.getDescription(), args ); + delegate.printMessage( kind, message); if ( kind == Kind.ERROR ) { isErroneous = true; } } @Override - public void printMessage(Kind kind, CharSequence msg, Element e) { - delegate.printMessage( kind, msg, e ); + public void printMessage( Kind kind, Element e, Message msg, Object... args) { + String message = String.format( msg.getDescription(), args ); + delegate.printMessage( kind, message, e ); if ( kind == Kind.ERROR ) { isErroneous = true; } } @Override - public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) { - delegate.printMessage( kind, msg, e, a ); + public void printMessage( Kind kind, Element e, AnnotationMirror a, Message msg, Object... args) { + String message = String.format( msg.getDescription(), args ); + delegate.printMessage( kind, message, e, a ); if ( kind == Kind.ERROR ) { isErroneous = true; } } @Override - public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) { - delegate.printMessage( kind, msg, e, a, v ); + public void printMessage( Kind kind, Element e, AnnotationMirror a, AnnotationValue v, Message msg, + Object... args) { + String message = String.format( msg.getDescription(), args ); + delegate.printMessage( kind, message, e, a, v ); if ( kind == Kind.ERROR ) { isErroneous = true; } @@ -141,5 +148,6 @@ public class DefaultModelElementProcessorContext implements ProcessorContext { public boolean isErroneous() { return isErroneous; } + } } diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java index f3859e7a3..2e9c055b4 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -24,7 +24,7 @@ import java.util.List; import java.util.SortedSet; import java.util.TreeSet; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeKind; @@ -55,6 +55,7 @@ import org.mapstruct.ap.prism.InheritConfigurationPrism; import org.mapstruct.ap.prism.InheritInverseConfigurationPrism; import org.mapstruct.ap.prism.MapperPrism; import org.mapstruct.ap.processor.creation.MappingResolverImpl; +import org.mapstruct.ap.util.Message; import org.mapstruct.ap.util.MapperConfig; import org.mapstruct.ap.util.Strings; import org.mapstruct.ap.version.VersionInformation; @@ -69,7 +70,7 @@ public class MapperCreationProcessor implements ModelElementProcessor mappingMethods = new ArrayList( methods.size() ); @@ -208,15 +204,7 @@ public class MapperCreationProcessor implements ModelElementProcessor candidates, SourceMethod method, InheritInverseConfigurationPrism reversePrism) { - messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Given name \"%s\" matches several candidate methods: %s().", - reversePrism.name(), Strings.join( candidates, "(), " ) - ), + messager.printMessage( Diagnostic.Kind.ERROR, method.getExecutable(), - reversePrism.mirror + reversePrism.mirror, + Message.inheritinverseconfiguration_duplicatematches, + reversePrism.name(), + Strings.join( candidates, "(), " ) + ); } private void reportErrorWhenNonMatchingName(SourceMethod onlyCandidate, SourceMethod method, InheritInverseConfigurationPrism reversePrism) { - messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Given name \"%s\" does not match the only candidate. Did you mean: \"%s\".", - reversePrism.name(), onlyCandidate.getName() - ), + messager.printMessage( Diagnostic.Kind.ERROR, method.getExecutable(), - reversePrism.mirror + reversePrism.mirror, + Message.inheritinverseconfiguration_nonamematch, + reversePrism.name(), + onlyCandidate.getName() ); } @@ -638,14 +609,11 @@ public class MapperCreationProcessor implements ModelElementProcessor candidates, SourceMethod method, InheritConfigurationPrism prism) { - messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Given name \"%s\" matches several candidate methods: %s().", - prism.name(), Strings.join( candidates, "(), " ) - ), + messager.printMessage( Diagnostic.Kind.ERROR, method.getExecutable(), - prism.mirror + prism.mirror, + Message.inheritconfiguration_duplicatematches, + prism.name(), + Strings.join( candidates, "(), " ) ); } private void reportErrorWhenNonMatchingName(SourceMethod onlyCandidate, SourceMethod method, InheritConfigurationPrism prims) { - messager.printMessage( - Diagnostic.Kind.ERROR, - String.format( - "Given name \"%s\" does not match the only candidate. Did you mean: \"%s\".", - prims.name(), onlyCandidate.getName() - ), + messager.printMessage( Diagnostic.Kind.ERROR, method.getExecutable(), - prims.mirror + prims.mirror, + Message.inheritconfiguration_nonamematch, + prims.name(), + onlyCandidate.getName() ); } - - } diff --git a/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java b/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java index e52931edc..9abe985c7 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MethodRetrievalProcessor.java @@ -25,7 +25,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.TypeElement; @@ -49,6 +49,7 @@ import org.mapstruct.ap.prism.MapMappingPrism; import org.mapstruct.ap.prism.MappingPrism; import org.mapstruct.ap.prism.MappingsPrism; import org.mapstruct.ap.util.AnnotationProcessingException; +import org.mapstruct.ap.util.Message; import org.mapstruct.ap.util.MapperConfig; /** @@ -61,7 +62,7 @@ import org.mapstruct.ap.util.MapperConfig; */ public class MethodRetrievalProcessor implements ModelElementProcessor> { - private Messager messager; + private FormattingMessager messager; private TypeFactory typeFactory; private Types typeUtils; private Elements elementUtils; @@ -271,88 +272,60 @@ public class MethodRetrievalProcessor implements ModelElementProcessor { TypeFactory getTypeFactory(); - Messager getMessager(); + FormattingMessager getMessager(); Options getOptions(); diff --git a/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java b/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java index 88cdb2e0b..7ccc0000e 100755 --- a/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/creation/MappingResolverImpl.java @@ -23,7 +23,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import javax.annotation.processing.Messager; +import org.mapstruct.ap.util.FormattingMessager; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; @@ -49,6 +49,7 @@ import org.mapstruct.ap.model.source.builtin.BuiltInMethod; import org.mapstruct.ap.model.source.selector.MethodSelectors; import org.mapstruct.ap.model.source.selector.SelectionCriteria; import org.mapstruct.ap.prism.BeanMappingPrism; +import org.mapstruct.ap.util.Message; import org.mapstruct.ap.util.Strings; /** @@ -60,7 +61,7 @@ import org.mapstruct.ap.util.Strings; */ public class MappingResolverImpl implements MappingResolver { - private final Messager messager; + private final FormattingMessager messager; private final Types typeUtils; private final TypeFactory typeFactory; @@ -77,8 +78,9 @@ public class MappingResolverImpl implements MappingResolver { */ private final Set usedVirtualMappings = new HashSet(); - public MappingResolverImpl(Messager messager, Elements elementUtils, Types typeUtils, TypeFactory typeFactory, - List sourceModel, List mapperReferences) { + public MappingResolverImpl(FormattingMessager messager, Elements elementUtils, Types typeUtils, + TypeFactory typeFactory, List sourceModel, + List mapperReferences) { this.messager = messager; this.typeUtils = typeUtils; this.typeFactory = typeFactory; @@ -462,14 +464,23 @@ public class MappingResolverImpl implements MappingResolver { // into the target type if ( candidates.size() > 1 ) { - String errorMsg = String.format( - "Ambiguous mapping methods found for %s %s: %s.", - mappedElement != null ? "mapping " + mappedElement + " to" : "factorizing", - returnType, - Strings.join( candidates, ", " ) - ); - - messager.printMessage( Diagnostic.Kind.ERROR, errorMsg, mappingMethod.getExecutable() ); + if ( mappedElement != null ) { + messager.printMessage( Diagnostic.Kind.ERROR, + mappingMethod.getExecutable(), + Message.general_ambigiousmappingmethod, + mappedElement, + returnType, + Strings.join( candidates, ", " ) + ); + } + else { + messager.printMessage( Diagnostic.Kind.ERROR, + mappingMethod.getExecutable(), + Message.general_ambigiousfactorymethod, + returnType, + Strings.join( candidates, ", " ) + ); + } } if ( !candidates.isEmpty() ) { diff --git a/processor/src/main/java/org/mapstruct/ap/util/AnnotationProcessingException.java b/processor/src/main/java/org/mapstruct/ap/util/AnnotationProcessingException.java index c097d056e..255953344 100644 --- a/processor/src/main/java/org/mapstruct/ap/util/AnnotationProcessingException.java +++ b/processor/src/main/java/org/mapstruct/ap/util/AnnotationProcessingException.java @@ -18,7 +18,6 @@ */ package org.mapstruct.ap.util; -import javax.annotation.processing.Messager; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; @@ -26,7 +25,7 @@ import javax.lang.model.element.Element; /** * Indicates an error during annotation processing. Should only be thrown in non-recoverable situations such as errors * due to incomplete compilations etc. Expected errors to be propagated to the user of the annotation processor should - * be raised using the {@link Messager} API instead. + * be raised using the {@link FormattingMessager} API instead. * * @author Gunnar Morling */ diff --git a/processor/src/main/java/org/mapstruct/ap/util/FormattingMessager.java b/processor/src/main/java/org/mapstruct/ap/util/FormattingMessager.java new file mode 100644 index 000000000..4e21c794f --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/util/FormattingMessager.java @@ -0,0 +1,83 @@ +/** + * Copyright 2012-2015 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.util; + +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.tools.Diagnostic; + +public interface FormattingMessager { + + /** + * Prints a message of the specified kind. + * + * @param kind the kind of message + * @param msg the message + * @param args Arguments referenced by the format specifiers in the format string. If there are more arguments + * than format specifiers, the extra arguments are ignored + */ + void printMessage( Diagnostic.Kind kind, Message msg, Object... args); + + /** + * Prints a message of the specified kind at the location of the + * element. + * + * @param kind the kind of message + * @param e the element to use as a position hint + * @param msg the message + * @param args Arguments referenced by the format specifiers in the format string. If there are more arguments + * than format specifiers, the extra arguments are ignored + */ + void printMessage( Diagnostic.Kind kind, Element e, Message msg, Object... args); + + /** + * Prints a message of the specified kind at the location of the + * annotation mirror of the annotated element. + * + * @param kind the kind of message + * @param e the annotated element + * @param a the annotation to use as a position hint + * @param msg the message + * @param args Arguments referenced by the format specifiers in the format string. If there are more arguments + * than format specifiers, the extra arguments are ignored + * + */ + void printMessage( Diagnostic.Kind kind, Element e, AnnotationMirror a, Message msg, Object... args); + + /** + * Prints a message of the specified kind at the location of the + * annotation value inside the annotation mirror of the annotated + * element. + * + * @param kind the kind of message + * @param e the annotated element + * @param a the annotation containing the annotation value + * @param v the annotation value to use as a position hint + * @param msg the message + * @param args Arguments referenced by the format specifiers in the format string. If there are more arguments + * than format specifiers, the extra arguments are ignored + */ + void printMessage( Diagnostic.Kind kind, + Element e, + AnnotationMirror a, + AnnotationValue v, + Message msg, + Object... args); +} diff --git a/processor/src/main/java/org/mapstruct/ap/util/Message.java b/processor/src/main/java/org/mapstruct/ap/util/Message.java new file mode 100644 index 000000000..657810477 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/util/Message.java @@ -0,0 +1,100 @@ +/** + * Copyright 2012-2015 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.util; + +/** + * + * @author Sjaak Derksen + */ +public enum Message { + + // CHECKSTYLE:OFF + beanmapping_noelements( "'resultType' and 'qualifiedBy' are undefined in @BeanMapping, define at least one of them." ), + beanmapping_notassignable( "%s not assignable to: %s." ), + beanmapping_unknownpropertyinreturntype( "Unknown property \"%s\" in return type." ), + beanmapping_severalpossiblesources( "Several possible source properties for target property \"%s\"." ), + beanmapping_severalpossibletargetaccessors( "Found several matching getters for property \"%s\"" ), + beanmapping_unmappedtargets( "Unmapped target %s" ), + propertymapping_mappingnotfound( "Can't map %s to \"%s %s\". Consider to declare/implement a mapping method: \"%s map(%s value)\"." ), + propertymapping_duplicatetargets( "Target property \"%s\" must not be mapped more than once." ), + propertymapping_emptytarget( "Target must not be empty in @Mapping" ), + propertymapping_sourceandconstantbothdefined( "Source and constant are both defined in @Mapping, either define a source or a constant" ), + propertymapping_sourceandexpressionbothdefined( "Source and expression are both defined in @Mapping, either define a source or an expression" ), + propertymapping_expressionandconstantbothdefined( "Expression and constant are both defined in @Mapping, either define an expression or a constant" ), + propertymapping_invalidexpression( "Value must be given in the form \"java()\"" ), + propertymapping_reversalproblem( "Parameter %s cannot be reversed" ), + propertymapping_invalidparametername( "Method has no parameter named \"%s\"." ), + propertymapping_nopropertyinparameter( "The type of parameter \"%s\" has no property named \"%s\"." ), + propertymapping_invalidpropertyname( "No property named \"%s\" exists in source parameter(s)." ), + constantmapping_mappingnotfound( "Can't map \"%s %s\" to \"%s %s\"." ), + mapmapping_key_mappingnotfound( "Can't create implementation of method %s. Found no method nor built-in conversion for mapping source key type to target key type." ), + mapmapping_value_mappingnotfound( "Can't create implementation of method %s. Found no method nor built-in conversion for mapping source value type to target value type." ), + mapmapping_noelements( "'keyDateFormat', 'keyQualifiedBy', 'keyTargetType', 'valueDateFormat', 'valueQualfiedBy' and 'valueTargetType' are all undefined in @MapMapping, define at least one of them." ), + iterablemapping_mappingnotfound( "Can't create implementation of method %s. Found no method nor built-in conversion for mapping source element type into target element type." ), + iterablemapping_noelements( "'dateformat', 'qualifiedBy' and 'elementTargetType' are undefined in @IterableMapping, define at least one of them." ), + enummapping_multipletargets( "One enum constant must not be mapped to more than one target constant, but constant %s is mapped to %s." ), + enummapping_undefinedsource( "A source constant must be specified for mappings of an enum mapping method." ), + enummapping_nonexistingconstant( "Constant %s doesn't exist in enum type %s." ), + enummapping_undefinedtarget( "A target constant must be specified for mappings of an enum mapping method." ), + enummapping_unmappedtargets( "The following constants from the source enum have no corresponding constant in the target enum and must be be mapped via @Mapping: %s" ), + decorator_nosubtype( "Specified decorator type is no subtype of the annotated mapper type." ), + decorator_constructor( "Specified decorator type has no default constructor nor a constructor with a single parameter accepting the decorated mapper type." ), + general_noimplementation( "No implementation type is registered for return type %s." ), + general_ambigiousmappingmethod( "Ambiguous mapping methods found for mapping %s to %s: %s." ), + general_ambigiousfactorymethod( "Ambiguous mapping methods found for factorizing %s: %s." ), + general_unsupporteddateformatcheck( "No dateFormat cheeck is supported for types %s, %s" ), + general_validdate( "given date format \"%s\" is valid." ), + general_invaliddate( "given date format \"%s\" is invalid. Message: \"%s\"" ), + retrieval_noinputargs( "Can't generate mapping method with no input arguments." ), + retrieval_duplicatemappingtargets( "Can't generate mapping method with more than one @MappingTarget parameter." ), + retrieval_voidmappingmethod( "Can't generate mapping method with return type void." ), + retrieval_nonassignableresulttype( "The result type is not assignable to the the return type." ), + retrieval_iterabletononiterable( "Can't generate mapping method from iterable type to non-iterable type." ), + retrieval_mappinghastargettypeparameter( "Can't generate mapping method that has a parameter annotated with @TargetType." ), + retrieval_noniterabletoiterable( "Can't generate mapping method from non-iterable type to iterable type." ), + retrieval_primitiveparameter( "Can't generate mapping method with primitive parameter type." ), + retrieval_primitivereturn( "Can't generate mapping method with primitive return type." ), + retrieval_enumtononenum( "Can't generate mapping method from enum type to non-enum type." ), + retrieval_nonenumtoenum( "Can't generate mapping method from non-enum type to enum type." ), + inheritconfiguration_both( "Method cannot be annotated with both a @InheritConfiguration and @InheritInverseConfiguration" ), + inheritinverseconfiguration_referencehasinverse( "Resolved inverse mapping method %s() should not carry the @InheritInverseConfiguration annotation itself." ), + inheritinverseconfiguration_referencehasforward( "Resolved inverse mapping method %s() should not carry the @InheritConfiguration annotation." ), + inheritinverseconfiguration_duplicates( "Several matching inverse methods exist: %s(). Specify a name explicitly." ), + inheritinverseconfiguration_invalidname( "None of the candidates %s() matches given name: \"%s\"." ), + inheritinverseconfiguration_duplicatematches( "Given name \"%s\" matches several candidate methods: %s()." ), + inheritinverseconfiguration_nonamematch( "Given name \"%s\" does not match the only candidate. Did you mean: \"%s\"." ), + inheritconfiguration_referencehasforward( "Resolved mapping method %s() should not carry the @InheritConfiguration annotation itself." ), + inheritconfiguration_referencehasinverse( "Resolved mapping method %s() should not carry the @InheritInverseConfiguration annotation." ), + inheritconfiguration_duplicates( "Several matching methods exist: %s(). Specify a name explicitly." ), + inheritconfiguration_invalidname( "None of the candidates %s() matches given name: \"%s\"." ), + inheritconfiguration_duplicatematches( "Given name \"%s\" matches several candidate methods: %s()." ), + inheritconfiguration_nonamematch( "Given name \"%s\" does not match the only candidate. Did you mean: \"%s\"." ); + // CHECKSTYLE:ON + + private final String description; + + private Message(String description) { + this.description = description; + } + + public String getDescription() { + return description; + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/model/common/DefaultConversionContextTest.java b/processor/src/test/java/org/mapstruct/ap/model/common/DefaultConversionContextTest.java index 9ad05e225..67da8410c 100755 --- a/processor/src/test/java/org/mapstruct/ap/model/common/DefaultConversionContextTest.java +++ b/processor/src/test/java/org/mapstruct/ap/model/common/DefaultConversionContextTest.java @@ -22,7 +22,6 @@ import org.junit.Test; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.util.JavaTimeConstants; -import javax.annotation.processing.Messager; import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.Element; @@ -34,6 +33,8 @@ import java.lang.annotation.Annotation; import java.util.List; import static org.fest.assertions.Assertions.assertThat; +import org.mapstruct.ap.util.Message; +import org.mapstruct.ap.util.FormattingMessager; /** * Testing DefaultConversionContext for dateFormat @@ -119,26 +120,26 @@ public class DefaultConversionContextTest { false ); } - private static class StatefulMessagerMock implements Messager { + private static class StatefulMessagerMock implements FormattingMessager { private Diagnostic.Kind lastKindPrinted; @Override - public void printMessage(Diagnostic.Kind kind, CharSequence msg) { + public void printMessage( Diagnostic.Kind kind, Message msg, Object... arg) { lastKindPrinted = kind; } @Override - public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e) { + public void printMessage( Diagnostic.Kind kind, Element e, Message msg, Object... arg) { } @Override - public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a) { + public void printMessage( Diagnostic.Kind kind, Element e, AnnotationMirror a, Message msg, Object... arg) { } @Override - public void printMessage(Diagnostic.Kind kind, CharSequence msg, Element e, AnnotationMirror a, - AnnotationValue v) { + public void printMessage( Diagnostic.Kind kind, Element e, AnnotationMirror a, AnnotationValue v, + Message msg, Object... arg) { } public Diagnostic.Kind getLastKindPrinted() {