mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#113 renaming mapping attribute expression into constant, preparing for other expressions.
This commit is contained in:
parent
60cdf836ce
commit
87f7f55539
@ -42,7 +42,7 @@ public @interface Mapping {
|
|||||||
* <li>The source name of the configured property as defined by the JavaBeans specification.</li>
|
* <li>The source name of the configured property as defined by the JavaBeans specification.</li>
|
||||||
* <li>When used to map an enum constant, the name of the constant member is to be given<./li>.
|
* <li>When used to map an enum constant, the name of the constant member is to be given<./li>.
|
||||||
* </ol>
|
* </ol>
|
||||||
* Either this attribute or {@link #expression()} may be specified for a given mapping, but not both at the same
|
* Either this attribute or {@link #constant()} may be specified for a given mapping, but not both at the same
|
||||||
* time.
|
* time.
|
||||||
*
|
*
|
||||||
* @return The source name of the configured property or enum constant.
|
* @return The source name of the configured property or enum constant.
|
||||||
@ -66,15 +66,15 @@ public @interface Mapping {
|
|||||||
String dateFormat() default "";
|
String dateFormat() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A constant {@link String} expression based on which the specified target property is to be set. If the designated
|
* A constant {@link String} based on which the specified target property is to be set. If the designated
|
||||||
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
|
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
|
||||||
* method or built-in conversion.
|
* method or built-in conversion.
|
||||||
* <p>
|
* <p>
|
||||||
* Either this attribute or {@link #source()} may be specified for a given mapping, but not both at the same time.
|
* Either this attribute or {@link #source()} may be specified for a given mapping, but not both at the same time.
|
||||||
* If this attribute is given, the target property must be specified via {@link #target()}.
|
* If this attribute is given, the target property must be specified via {@link #target()}.
|
||||||
*
|
*
|
||||||
* @return A constant {@code String} expression specifying the value for the designated target prperty
|
* @return A constant {@code String} constant specifying the value for the designated target property
|
||||||
*/
|
*/
|
||||||
String expression() default "";
|
String constant() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -40,7 +40,7 @@ public @interface Mapping {
|
|||||||
* <li>The source name of the configured property as defined by the JavaBeans specification.</li>
|
* <li>The source name of the configured property as defined by the JavaBeans specification.</li>
|
||||||
* <li>When used to map an enum constant, the name of the constant member is to be given<./li>.
|
* <li>When used to map an enum constant, the name of the constant member is to be given<./li>.
|
||||||
* </ol>
|
* </ol>
|
||||||
* Either this attribute or {@link #expression()} may be specified for a given mapping, but not both at the same
|
* Either this attribute or {@link #constant()} may be specified for a given mapping, but not both at the same
|
||||||
* time.
|
* time.
|
||||||
*
|
*
|
||||||
* @return The source name of the configured property or enum constant.
|
* @return The source name of the configured property or enum constant.
|
||||||
@ -64,15 +64,15 @@ public @interface Mapping {
|
|||||||
String dateFormat() default "";
|
String dateFormat() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A constant {@link String} expression based on which the specified target property is to be set. If the designated
|
* A constant {@link String} based on which the specified target property is to be set. If the designated
|
||||||
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
|
* target property is not of type {@code String}, the value will be converted by applying a matching conversion
|
||||||
* method or built-in conversion.
|
* method or built-in conversion.
|
||||||
* <p>
|
* <p>
|
||||||
* Either this attribute or {@link #source()} may be specified for a given mapping, but not both at the same time.
|
* Either this attribute or {@link #source()} may be specified for a given mapping, but not both at the same time.
|
||||||
* If this attribute is given, the target property must be specified via {@link #target()}.
|
* If this attribute is given, the target property must be specified via {@link #target()}.
|
||||||
*
|
*
|
||||||
* @return A constant {@code String} expression specifying the value for the designated target prperty
|
* @return A constant {@code String} constant specifying the value for the designated target property
|
||||||
*/
|
*/
|
||||||
String expression() default "";
|
String constant() default "";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ public class Mapping {
|
|||||||
private final String sourceName;
|
private final String sourceName;
|
||||||
private final String sourceParameterName;
|
private final String sourceParameterName;
|
||||||
private final String sourcePropertyName;
|
private final String sourcePropertyName;
|
||||||
private final String expression;
|
private final String constant;
|
||||||
private final String targetName;
|
private final String targetName;
|
||||||
private final String dateFormat;
|
private final String dateFormat;
|
||||||
private final AnnotationMirror mirror;
|
private final AnnotationMirror mirror;
|
||||||
@ -74,18 +74,18 @@ public class Mapping {
|
|||||||
mappingPrism.values.source()
|
mappingPrism.values.source()
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( mappingPrism.source().isEmpty() && mappingPrism.expression().isEmpty() ) {
|
if ( mappingPrism.source().isEmpty() && mappingPrism.constant().isEmpty() ) {
|
||||||
messager.printMessage(
|
messager.printMessage(
|
||||||
Diagnostic.Kind.ERROR,
|
Diagnostic.Kind.ERROR,
|
||||||
"Either define a source or an expression in a Mapping",
|
"Either define a source or a constant in a Mapping",
|
||||||
element
|
element
|
||||||
);
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
else if ( !mappingPrism.source().isEmpty() && !mappingPrism.expression().isEmpty() ) {
|
else if ( !mappingPrism.source().isEmpty() && !mappingPrism.constant().isEmpty() ) {
|
||||||
messager.printMessage(
|
messager.printMessage(
|
||||||
Diagnostic.Kind.ERROR,
|
Diagnostic.Kind.ERROR,
|
||||||
"Source and expression are both defined in Mapping, either define a source or an expression",
|
"Source and constant are both defined in Mapping, either define a source or an expression",
|
||||||
element
|
element
|
||||||
);
|
);
|
||||||
return null;
|
return null;
|
||||||
@ -95,7 +95,7 @@ public class Mapping {
|
|||||||
mappingPrism.source(),
|
mappingPrism.source(),
|
||||||
sourceNameParts != null ? sourceNameParts[0] : null,
|
sourceNameParts != null ? sourceNameParts[0] : null,
|
||||||
sourceNameParts != null ? sourceNameParts[1] : mappingPrism.source(),
|
sourceNameParts != null ? sourceNameParts[1] : mappingPrism.source(),
|
||||||
mappingPrism.expression(),
|
mappingPrism.constant(),
|
||||||
mappingPrism.target(),
|
mappingPrism.target(),
|
||||||
mappingPrism.dateFormat(),
|
mappingPrism.dateFormat(),
|
||||||
mappingPrism.mirror,
|
mappingPrism.mirror,
|
||||||
@ -123,13 +123,13 @@ public class Mapping {
|
|||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mapping(String sourceName, String sourceParameterName, String sourcePropertyName, String expression,
|
private Mapping(String sourceName, String sourceParameterName, String sourcePropertyName, String constant,
|
||||||
String targetName, String dateFormat, AnnotationMirror mirror,
|
String targetName, String dateFormat, AnnotationMirror mirror,
|
||||||
AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) {
|
AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) {
|
||||||
this.sourceName = sourceName;
|
this.sourceName = sourceName;
|
||||||
this.sourceParameterName = sourceParameterName;
|
this.sourceParameterName = sourceParameterName;
|
||||||
this.sourcePropertyName = sourcePropertyName;
|
this.sourcePropertyName = sourcePropertyName;
|
||||||
this.expression = expression;
|
this.constant = constant;
|
||||||
this.targetName = targetName.equals( "" ) ? sourceName : targetName;
|
this.targetName = targetName.equals( "" ) ? sourceName : targetName;
|
||||||
this.dateFormat = dateFormat;
|
this.dateFormat = dateFormat;
|
||||||
this.mirror = mirror;
|
this.mirror = mirror;
|
||||||
@ -165,8 +165,8 @@ public class Mapping {
|
|||||||
return sourceParameterName;
|
return sourceParameterName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getExpression() {
|
public String getConstant() {
|
||||||
return expression;
|
return constant;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -193,13 +193,13 @@ public class Mapping {
|
|||||||
|
|
||||||
public Mapping reverse() {
|
public Mapping reverse() {
|
||||||
Mapping reverse = null;
|
Mapping reverse = null;
|
||||||
if ( expression != null ) {
|
if ( constant != null ) {
|
||||||
/* mapping can only be reversed if the source was not a constant */
|
/* mapping can only be reversed if the source was not a constant */
|
||||||
reverse = new Mapping(
|
reverse = new Mapping(
|
||||||
targetName,
|
targetName,
|
||||||
null,
|
null,
|
||||||
targetName,
|
targetName,
|
||||||
expression,
|
constant,
|
||||||
sourceName,
|
sourceName,
|
||||||
dateFormat,
|
dateFormat,
|
||||||
mirror,
|
mirror,
|
||||||
|
@ -411,8 +411,8 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
String sourcePropertyName;
|
String sourcePropertyName;
|
||||||
if ( mapping != null ) {
|
if ( mapping != null ) {
|
||||||
dateFormat = mapping.getDateFormat();
|
dateFormat = mapping.getDateFormat();
|
||||||
isSourceConstant = !mapping.getExpression().isEmpty();
|
isSourceConstant = !mapping.getConstant().isEmpty();
|
||||||
sourceConstant = "\"" + mapping.getExpression() + "\"";
|
sourceConstant = "\"" + mapping.getConstant() + "\"";
|
||||||
sourcePropertyName = mapping.getSourcePropertyName();
|
sourcePropertyName = mapping.getSourcePropertyName();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -675,7 +675,7 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if ( mappedProperty.getExpression().isEmpty() &&
|
else if ( mappedProperty.getConstant().isEmpty() &&
|
||||||
!hasSourceProperty( method, mappedProperty.getSourcePropertyName() ) ) {
|
!hasSourceProperty( method, mappedProperty.getSourcePropertyName() ) ) {
|
||||||
messager.printMessage(
|
messager.printMessage(
|
||||||
Kind.ERROR,
|
Kind.ERROR,
|
||||||
|
@ -33,11 +33,11 @@ public interface ErroneousMapper1 {
|
|||||||
ErroneousMapper1 INSTANCE = Mappers.getMapper( ErroneousMapper1.class );
|
ErroneousMapper1 INSTANCE = Mappers.getMapper( ErroneousMapper1.class );
|
||||||
|
|
||||||
@Mappings( {
|
@Mappings( {
|
||||||
@Mapping( target = "stringConstant", expression = "stringConstant"),
|
@Mapping( target = "stringConstant", constant = "stringConstant"),
|
||||||
@Mapping( source = "test" , target = "integerConstant", expression = "14"),
|
@Mapping( source = "test" , target = "integerConstant", constant = "14"),
|
||||||
@Mapping( target = "longWrapperConstant", expression = "3001"),
|
@Mapping( target = "longWrapperConstant", constant = "3001"),
|
||||||
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", expression = "09-01-2014"),
|
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", constant = "09-01-2014"),
|
||||||
@Mapping( target = "nameConstants", expression = "jack-jill-tom" )
|
@Mapping( target = "nameConstants", constant = "jack-jill-tom" )
|
||||||
} )
|
} )
|
||||||
Target sourceToTarget(Source s);
|
Target sourceToTarget(Source s);
|
||||||
}
|
}
|
||||||
|
@ -33,11 +33,11 @@ public interface ErroneousMapper2 {
|
|||||||
ErroneousMapper2 INSTANCE = Mappers.getMapper( ErroneousMapper2.class );
|
ErroneousMapper2 INSTANCE = Mappers.getMapper( ErroneousMapper2.class );
|
||||||
|
|
||||||
@Mappings( {
|
@Mappings( {
|
||||||
@Mapping( target = "stringConstant", expression = "stringConstant"),
|
@Mapping( target = "stringConstant", constant = "stringConstant"),
|
||||||
@Mapping( target = "integerConstant" ),
|
@Mapping( target = "integerConstant" ),
|
||||||
@Mapping( target = "longWrapperConstant", expression = "3001"),
|
@Mapping( target = "longWrapperConstant", constant = "3001"),
|
||||||
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", expression = "09-01-2014"),
|
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", constant = "09-01-2014"),
|
||||||
@Mapping( target = "nameConstants", expression = "jack-jill-tom" )
|
@Mapping( target = "nameConstants", constant = "jack-jill-tom" )
|
||||||
} )
|
} )
|
||||||
Target sourceToTarget(Source s);
|
Target sourceToTarget(Source s);
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ public class SourceConstantsTest {
|
|||||||
@Diagnostic(type = ErroneousMapper1.class,
|
@Diagnostic(type = ErroneousMapper1.class,
|
||||||
kind = Kind.ERROR,
|
kind = Kind.ERROR,
|
||||||
line = 42,
|
line = 42,
|
||||||
messageRegExp = "Source and expression are both defined in Mapping, either define a source or an "
|
messageRegExp = "Source and constant are both defined in Mapping, either define a source or an "
|
||||||
+ "expression"),
|
+ "expression"),
|
||||||
@Diagnostic(type = ErroneousMapper1.class,
|
@Diagnostic(type = ErroneousMapper1.class,
|
||||||
kind = Kind.WARNING,
|
kind = Kind.WARNING,
|
||||||
@ -121,7 +121,7 @@ public class SourceConstantsTest {
|
|||||||
@Diagnostic(type = ErroneousMapper2.class,
|
@Diagnostic(type = ErroneousMapper2.class,
|
||||||
kind = Kind.ERROR,
|
kind = Kind.ERROR,
|
||||||
line = 42,
|
line = 42,
|
||||||
messageRegExp = "Either define a source or an expression in a Mapping"),
|
messageRegExp = "Either define a source or a constant in a Mapping"),
|
||||||
@Diagnostic(type = ErroneousMapper2.class,
|
@Diagnostic(type = ErroneousMapper2.class,
|
||||||
kind = Kind.WARNING,
|
kind = Kind.WARNING,
|
||||||
line = 42,
|
line = 42,
|
||||||
|
@ -33,11 +33,11 @@ public interface SourceTargetMapper {
|
|||||||
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||||
|
|
||||||
@Mappings( {
|
@Mappings( {
|
||||||
@Mapping( target = "stringConstant", expression = "stringConstant"),
|
@Mapping( target = "stringConstant", constant = "stringConstant"),
|
||||||
@Mapping( target = "integerConstant", expression = "14"),
|
@Mapping( target = "integerConstant", constant = "14"),
|
||||||
@Mapping( target = "longWrapperConstant", expression = "3001"),
|
@Mapping( target = "longWrapperConstant", constant = "3001"),
|
||||||
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", expression = "09-01-2014"),
|
@Mapping( target = "dateConstant", dateFormat = "dd-MM-yyyy", constant = "09-01-2014"),
|
||||||
@Mapping( target = "nameConstants", expression = "jack-jill-tom" )
|
@Mapping( target = "nameConstants", constant = "jack-jill-tom" )
|
||||||
} )
|
} )
|
||||||
Target sourceToTarget(Source s);
|
Target sourceToTarget(Source s);
|
||||||
Source targetToSource(Target t);
|
Source targetToSource(Target t);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user