From abba4fb71691bb33acb056382b83c0fba268ea28 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Sun, 7 Jul 2013 14:45:47 +0200 Subject: [PATCH] #26 Adding support for mappings between Date and String, optionally applying a format string --- core/src/main/java/org/mapstruct/Mapping.java | 16 +++- .../BigIntegerToPrimitiveConversion.java | 12 ++- .../BigIntegerToStringConversion.java | 12 ++- .../BigIntegerToWrapperConversion.java | 11 ++- .../ap/conversion/CharToStringConversion.java | 17 ++-- .../CharWrapperToStringConversion.java | 17 ++-- .../ap/conversion/ConversionProvider.java | 84 +++++++++++++++++++ .../mapstruct/ap/conversion/Conversions.java | 10 ++- .../ap/conversion/DateToStringConversion.java | 74 ++++++++++++++++ .../conversion/DefaultConversionContext.java | 48 +++++++++++ .../ap/conversion/EnumStringConversion.java | 17 ++-- .../PrimitiveToPrimitiveConversion.java | 17 ++-- .../PrimitiveToStringConversion.java | 17 ++-- .../PrimitiveToWrapperConversion.java | 19 +++-- .../ap/conversion/ReverseConversion.java | 28 ++++--- .../ap/conversion/SimpleConversion.java | 68 +++++++++++++++ .../conversion/WrapperToStringConversion.java | 16 ++-- .../WrapperToWrapperConversion.java | 20 +++-- .../ap/model/IterableMappingMethod.java | 4 +- .../mapstruct/ap/model/PropertyMapping.java | 18 ++-- .../mapstruct/ap/model/TypeConversion.java | 60 +++++++++++++ .../mapstruct/ap/model/source/Mapping.java | 16 ++-- .../ap/processor/MapperCreationProcessor.java | 21 +++-- ...org.mapstruct.ap.model.PropertyMapping.ftl | 26 +++++- .../org.mapstruct.ap.model.TypeConversion.ftl | 21 +++++ .../conversion/date/DateConversionTest.java | 75 +++++++++++++++++ .../ap/test/conversion/date/Source.java | 43 ++++++++++ .../conversion/date/SourceTargetMapper.java} | 16 ++-- .../ap/test/conversion/date/Target.java | 41 +++++++++ 29 files changed, 722 insertions(+), 122 deletions(-) create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/DateToStringConversion.java create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/DefaultConversionContext.java create mode 100644 processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java create mode 100644 processor/src/main/java/org/mapstruct/ap/model/TypeConversion.java create mode 100644 processor/src/main/resources/org.mapstruct.ap.model.TypeConversion.ftl create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java rename processor/src/{main/java/org/mapstruct/ap/conversion/Conversion.java => test/java/org/mapstruct/ap/test/conversion/date/SourceTargetMapper.java} (66%) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java diff --git a/core/src/main/java/org/mapstruct/Mapping.java b/core/src/main/java/org/mapstruct/Mapping.java index c0c31859b..95513ff1b 100644 --- a/core/src/main/java/org/mapstruct/Mapping.java +++ b/core/src/main/java/org/mapstruct/Mapping.java @@ -18,6 +18,9 @@ */ package org.mapstruct; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * Configures the mapping of one bean attribute. * @@ -33,9 +36,18 @@ public @interface Mapping { String source(); /** - * The target name of the configured property as defined by the JavaBeans specification. + * The target name of the configured property as defined by the JavaBeans specification. Defaults to the + * source name if not given. * * @return The target name of the configured property. */ - String target(); + String target() default ""; + + /** + * A format string as processable by {@link SimpleDateFormat} if the attribute is mapped from {@code String} to + * {@link Date} or vice-versa. Will be ignored for all other attribute types. + * + * @return A date format string as processable by {@link SimpleDateFormat}. + */ + String dateFormat() default ""; } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java index 94fcc34a1..d1895a875 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToPrimitiveConversion.java @@ -20,14 +20,12 @@ package org.mapstruct.ap.conversion; import java.math.BigInteger; -import org.mapstruct.ap.model.Type; - /** * Conversion between {@link BigInteger} and native number types. * * @author Gunnar Morling */ -public class BigIntegerToPrimitiveConversion implements Conversion { +public class BigIntegerToPrimitiveConversion extends SimpleConversion { private final Class targetType; @@ -40,19 +38,19 @@ public class BigIntegerToPrimitiveConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { - return sourcePropertyAccessor + "." + targetType.getName() + "Value()"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return sourceReference + "." + targetType.getName() + "Value()"; } @Override - public String from(String targetPropertyAccessor, Type type) { + public String getFromConversionString(String targetReference, Context conversionContext) { StringBuilder conversion = new StringBuilder( "BigInteger.valueOf( " ); if ( targetType == float.class || targetType == double.class ) { conversion.append( "(long) " ); } - conversion.append( targetPropertyAccessor ).append( " )" ); + conversion.append( targetReference ).append( " )" ); return conversion.toString(); } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java index 18c9b37e0..ce21d3867 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToStringConversion.java @@ -20,22 +20,20 @@ package org.mapstruct.ap.conversion; import java.math.BigInteger; -import org.mapstruct.ap.model.Type; - /** * Conversion between {@link BigInteger} and {@link String}. * * @author Gunnar Morling */ -public class BigIntegerToStringConversion implements Conversion { +public class BigIntegerToStringConversion extends SimpleConversion { @Override - public String to(String sourcePropertyAccessor, Type type) { - return sourcePropertyAccessor + ".toString()"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return sourceReference + ".toString()"; } @Override - public String from(String targetPropertyAccessor, Type type) { - return "new BigInteger( " + targetPropertyAccessor + " )"; + public String getFromConversionString(String targetReference, Context conversionContext) { + return "new BigInteger( " + targetReference + " )"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java index f94f06ccb..a67f7f96d 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/BigIntegerToWrapperConversion.java @@ -20,7 +20,6 @@ package org.mapstruct.ap.conversion; import java.math.BigInteger; -import org.mapstruct.ap.model.Type; import org.mapstruct.ap.util.NativeTypes; /** @@ -28,7 +27,7 @@ import org.mapstruct.ap.util.NativeTypes; * * @author Gunnar Morling */ -public class BigIntegerToWrapperConversion implements Conversion { +public class BigIntegerToWrapperConversion extends SimpleConversion { private final Class targetType; @@ -41,15 +40,15 @@ public class BigIntegerToWrapperConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { - return sourcePropertyAccessor + "." + targetType.getName() + "Value()"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return sourceReference + "." + targetType.getName() + "Value()"; } @Override - public String from(String targetPropertyAccessor, Type type) { + public String getFromConversionString(String targetReference, Context conversionContext) { StringBuilder conversion = new StringBuilder( "BigInteger.valueOf( " ); - conversion.append( targetPropertyAccessor ); + conversion.append( targetReference ); if ( targetType == float.class || targetType == double.class ) { conversion.append( ".longValue()" ); diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/CharToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/CharToStringConversion.java index c0f8e08ee..6d237fd10 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/CharToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/CharToStringConversion.java @@ -18,17 +18,20 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; - -public class CharToStringConversion implements Conversion { +/** + * Conversion between {@code char} and {@link String}. + * + * @author Gunnar Morling + */ +public class CharToStringConversion extends SimpleConversion { @Override - public String to(String sourcePropertyAccessor, Type type) { - return "String.valueOf( " + sourcePropertyAccessor + " )"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return "String.valueOf( " + sourceReference + " )"; } @Override - public String from(String targetPropertyAccessor, Type type) { - return targetPropertyAccessor + ".charAt( 0 )"; + public String getFromConversionString(String targetReference, Context conversionContext) { + return targetReference + ".charAt( 0 )"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/CharWrapperToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/CharWrapperToStringConversion.java index 95d35e2fb..aade505f1 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/CharWrapperToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/CharWrapperToStringConversion.java @@ -18,17 +18,20 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; - -public class CharWrapperToStringConversion implements Conversion { +/** + * Conversion between {@link Character} and {@link String}. + * + * @author Gunnar Morling + */ +public class CharWrapperToStringConversion extends SimpleConversion { @Override - public String to(String sourcePropertyAccessor, Type type) { - return sourcePropertyAccessor + ".toString()"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return sourceReference + ".toString()"; } @Override - public String from(String targetPropertyAccessor, Type type) { - return targetPropertyAccessor + ".charAt( 0 )"; + public String getFromConversionString(String targetReference, Context conversionContext) { + return targetReference + ".charAt( 0 )"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java b/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java new file mode 100644 index 000000000..a5735dc86 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/ConversionProvider.java @@ -0,0 +1,84 @@ +/** + * Copyright 2012-2013 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.conversion; + +import org.mapstruct.ap.model.Type; +import org.mapstruct.ap.model.TypeConversion; + +/** + * Implementations create inline {@link TypeConversion}s such as + * + * + * + * @author Gunnar Morling + */ +public interface ConversionProvider { + + /** + * Creates the conversion from source to target of a property mapping. + * + * @param sourceReference A reference to the source object, e.g. + * {@code beanName.getFoo()}. + * @param conversionContext Context providing optional information required for creating + * the conversion. + * + * @return A conversion from source to target. + */ + TypeConversion to(String sourceReference, Context conversionContext); + + /** + * Creates the conversion from target to source of a property mapping. + * + * @param targetReference A reference to the targetReference object, e.g. + * {@code beanName.getFoo()}. + * @param conversionContext Context providing optional information required for creating + * the conversion. + * + * @return A conversion from target to source. + */ + TypeConversion from(String targetReference, Context conversionContext); + + /** + * Context object passed to conversion providers. + * + * @author Gunnar Morling + */ + public interface Context { + + /** + * Returns the target type of this conversion. + * + * @return The target type of this conversion. + */ + Type getTargetType(); + + /** + * Returns the date format if this conversion is from String to + * {@link Date} or vice versa. Returns {@code null} for other types or + * if not given. + * + * @return The date format if this conversion. + */ + String getDateFormat(); + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java b/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java index e1f8eba24..f0868384b 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/Conversions.java @@ -19,6 +19,7 @@ package org.mapstruct.ap.conversion; import java.math.BigInteger; +import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.lang.model.type.DeclaredType; @@ -31,14 +32,14 @@ import org.mapstruct.ap.util.TypeUtil; import static org.mapstruct.ap.conversion.ReverseConversion.reverse; /** - * Holds built-in {@link Conversion}s such as from {@code int} to {@code String}. + * Holds built-in {@link ConversionProvider}s such as from {@code int} to {@code String}. * * @author Gunnar Morling */ public class Conversions { private TypeUtil typeUtil; - private final Map conversions = new HashMap(); + private final Map conversions = new HashMap(); private final DeclaredType enumType; private final DeclaredType stringType; @@ -164,6 +165,7 @@ public class Conversions { //misc. register( Enum.class, String.class, new EnumStringConversion() ); + register( Date.class, String.class, new DateToStringConversion() ); } private void registerNativeTypeConversion(Class sourceType, Class targetType) { @@ -199,12 +201,12 @@ public class Conversions { } } - private void register(Class sourceType, Class targetType, Conversion conversion) { + private void register(Class sourceType, Class targetType, ConversionProvider conversion) { conversions.put( Key.forClasses( sourceType, targetType ), conversion ); conversions.put( Key.forClasses( targetType, sourceType ), reverse( conversion ) ); } - public Conversion getConversion(Type sourceType, Type targetType) { + public ConversionProvider getConversion(Type sourceType, Type targetType) { if ( sourceType.isEnumType() && targetType.equals( typeUtil.getType( stringType ) ) ) { sourceType = typeUtil.getType( enumType ); } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/DateToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/DateToStringConversion.java new file mode 100644 index 000000000..fb98e6f35 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/DateToStringConversion.java @@ -0,0 +1,74 @@ +/** + * Copyright 2012-2013 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.conversion; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; + +import org.mapstruct.ap.model.Type; +import org.mapstruct.ap.model.TypeConversion; + +import static org.mapstruct.ap.util.Collections.asSet; + +/** + * Conversion between {@link String} and {@link Date}. + * + * @author Gunnar Morling + */ +public class DateToStringConversion implements ConversionProvider { + + @Override + public TypeConversion to(String sourceReference, Context conversionContext) { + return new TypeConversion( + asSet( Type.forClass( SimpleDateFormat.class ) ), + Collections.emptyList(), + getConversionString( sourceReference, conversionContext, "format" ) + ); + } + + @Override + public TypeConversion from(String targetReference, Context conversionContext) { + return new TypeConversion( + asSet( Type.forClass( SimpleDateFormat.class ) ), + Arrays.asList( Type.forClass( ParseException.class ) ), + getConversionString( targetReference, conversionContext, "parse" ) + ); + } + + private String getConversionString(String targetReference, Context conversionContext, String method) { + StringBuilder conversionString = new StringBuilder( "new SimpleDateFormat(" ); + + if ( conversionContext.getDateFormat() != null ) { + conversionString.append( " \"" ); + conversionString.append( conversionContext.getDateFormat() ); + conversionString.append( "\" " ); + } + + conversionString.append( ")." ); + conversionString.append( method ); + conversionString.append( "( " ); + conversionString.append( targetReference ); + conversionString.append( " )" ); + + return conversionString.toString(); + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/DefaultConversionContext.java b/processor/src/main/java/org/mapstruct/ap/conversion/DefaultConversionContext.java new file mode 100644 index 000000000..697b482d3 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/DefaultConversionContext.java @@ -0,0 +1,48 @@ +/** + * Copyright 2012-2013 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.conversion; + +import org.mapstruct.ap.conversion.ConversionProvider.Context; +import org.mapstruct.ap.model.Type; + +/** + * Default implementation of the {@link Context} passed to conversion providers. + * + * @author Gunnar Morling + */ +public class DefaultConversionContext implements ConversionProvider.Context { + + private final Type targetType; + private final String format; + + public DefaultConversionContext(Type targetType, String format) { + this.targetType = targetType; + this.format = format; + } + + @Override + public Type getTargetType() { + return targetType; + } + + @Override + public String getDateFormat() { + return format; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java index 34233cbdd..28bf4b7de 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/EnumStringConversion.java @@ -18,17 +18,20 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; - -public class EnumStringConversion implements Conversion { +/** + * Conversion between {@link String} and {@link Enum} types. + * + * @author Gunnar Morling + */ +public class EnumStringConversion extends SimpleConversion { @Override - public String to(String sourcePropertyAccessor, Type type) { - return sourcePropertyAccessor + ".toString()"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return sourceReference + ".toString()"; } @Override - public String from(String targetPropertyAccessor, Type type) { - return "Enum.valueOf( " + type.getName() + ".class, " + targetPropertyAccessor + " )"; + public String getFromConversionString(String targetReference, Context conversionContext) { + return "Enum.valueOf( " + conversionContext.getTargetType().getName() + ".class, " + targetReference + " )"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToPrimitiveConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToPrimitiveConversion.java index 76a600045..b0966bba0 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToPrimitiveConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToPrimitiveConversion.java @@ -18,9 +18,12 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; - -public class PrimitiveToPrimitiveConversion implements Conversion { +/** + * Conversion between primitive types such as {@code byte} or {@code long}. + * + * @author Gunnar Morling + */ +public class PrimitiveToPrimitiveConversion extends SimpleConversion { private final Class sourceType; @@ -33,12 +36,12 @@ public class PrimitiveToPrimitiveConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { - return sourcePropertyAccessor; + public String getToConversionString(String sourceReference, Context conversionContext) { + return sourceReference; } @Override - public String from(String targetPropertyAccessor, Type type) { - return "(" + sourceType + ") " + targetPropertyAccessor; + public String getFromConversionString(String targetReference, Context conversionContext) { + return "(" + sourceType + ") " + targetReference; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToStringConversion.java index b758975e0..45173f8f5 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToStringConversion.java @@ -18,11 +18,16 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; import org.mapstruct.ap.util.NativeTypes; import org.mapstruct.ap.util.Strings; -public class PrimitiveToStringConversion implements Conversion { +/** + * Conversion between primitive types such as {@code byte} or {@code long} and + * {@link String}. + * + * @author Gunnar Morling + */ +public class PrimitiveToStringConversion extends SimpleConversion { private final Class sourceType; private final Class wrapperType; @@ -37,13 +42,13 @@ public class PrimitiveToStringConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { - return "String.valueOf( " + sourcePropertyAccessor + " )"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return "String.valueOf( " + sourceReference + " )"; } @Override - public String from(String targetPropertyAccessor, Type type) { + public String getFromConversionString(String targetReference, Context conversionContext) { return wrapperType.getSimpleName() + ".parse" + Strings.capitalize( sourceType.getSimpleName() ) + "( " + - targetPropertyAccessor + " )"; + targetReference + " )"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToWrapperConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToWrapperConversion.java index dba75a816..9fe634f8d 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToWrapperConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/PrimitiveToWrapperConversion.java @@ -18,10 +18,15 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; import org.mapstruct.ap.util.NativeTypes; -public class PrimitiveToWrapperConversion implements Conversion { +/** + * Conversion between primitive types such as {@code byte} and wrapper types + * such as {@link Integer}. + * + * @author Gunnar Morling + */ +public class PrimitiveToWrapperConversion extends SimpleConversion { private final Class sourceType; private final Class targetType; @@ -39,17 +44,17 @@ public class PrimitiveToWrapperConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { + public String getToConversionString(String sourceReference, Context conversionContext) { if ( sourceType == targetType ) { - return sourcePropertyAccessor; + return sourceReference; } else { - return "(" + targetType.getName() + ") " + sourcePropertyAccessor; + return "(" + targetType.getName() + ") " + sourceReference; } } @Override - public String from(String targetPropertyAccessor, Type type) { - return targetPropertyAccessor + "." + sourceType.getName() + "Value()"; + public String getFromConversionString(String targetReference, Context conversionContext) { + return targetReference + "." + sourceType.getName() + "Value()"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/ReverseConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/ReverseConversion.java index 7fb33a477..93928a30c 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/ReverseConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/ReverseConversion.java @@ -18,27 +18,33 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; +import org.mapstruct.ap.model.TypeConversion; -public class ReverseConversion implements Conversion { +/** + * A {@link ConversionProvider} which creates the reversed conversions for a + * given conversion provider. + * + * @author Gunnar Morling + */ +public class ReverseConversion implements ConversionProvider { - private Conversion conversion; + private ConversionProvider conversionProvider; - public static ReverseConversion reverse(Conversion conversion) { - return new ReverseConversion( conversion ); + public static ReverseConversion reverse(ConversionProvider conversionProvider) { + return new ReverseConversion( conversionProvider ); } - private ReverseConversion(Conversion conversion) { - this.conversion = conversion; + private ReverseConversion(ConversionProvider conversionProvider) { + this.conversionProvider = conversionProvider; } @Override - public String to(String sourcePropertyAccessor, Type type) { - return conversion.from( sourcePropertyAccessor, type ); + public TypeConversion to(String sourceReference, Context conversionContext) { + return conversionProvider.from( sourceReference, conversionContext ); } @Override - public String from(String targetPropertyAccessor, Type type) { - return conversion.to( targetPropertyAccessor, type ); + public TypeConversion from(String targetReference, Context conversionContext) { + return conversionProvider.to( targetReference, conversionContext ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java new file mode 100644 index 000000000..cce0a73c4 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/conversion/SimpleConversion.java @@ -0,0 +1,68 @@ +/** + * Copyright 2012-2013 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.conversion; + +import org.mapstruct.ap.model.TypeConversion; + +/** + * Base class for {@link ConversionProvider}s creating {@link TypeConversion}s + * which don't require any type imports nor declare any exception types. + * + * @author Gunnar Morling + */ +public abstract class SimpleConversion implements ConversionProvider { + + @Override + public TypeConversion to(String sourceReference, Context conversionContext) { + return new TypeConversion( + getToConversionString( sourceReference, conversionContext ) + ); + } + + @Override + public TypeConversion from(String targetReference, Context conversionContext) { + return new TypeConversion( + getFromConversionString( targetReference, conversionContext ) + ); + } + + /** + * Returns the conversion string from source to target. + * + * @param sourceReference A reference to the source object, e.g. + * {@code beanName.getFoo()}. + * @param conversionContext Context providing optional information required for creating + * the conversion. + * + * @return The conversion string from source to target. + */ + protected abstract String getToConversionString(String sourceReference, Context conversionContext); + + /** + * Creates the conversion string from target to source. + * + * @param targetReference A reference to the targetReference object, e.g. + * {@code beanName.getFoo()}. + * @param conversionContext Context providing optional information required for creating + * the conversion. + * + * @return The conversion string from target to source. + */ + protected abstract String getFromConversionString(String targetReference, Context conversionContext); +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToStringConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToStringConversion.java index 33d372e35..7f192a416 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToStringConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToStringConversion.java @@ -18,11 +18,15 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; import org.mapstruct.ap.util.NativeTypes; import org.mapstruct.ap.util.Strings; -public class WrapperToStringConversion implements Conversion { +/** + * Conversion between wrapper types such as {@link Integer} and {@link String}. + * + * @author Gunnar Morling + */ +public class WrapperToStringConversion extends SimpleConversion { private final Class sourceType; private final Class primitiveType; @@ -37,13 +41,13 @@ public class WrapperToStringConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { - return "String.valueOf( " + sourcePropertyAccessor + " )"; + public String getToConversionString(String sourceReference, Context conversionContext) { + return "String.valueOf( " + sourceReference + " )"; } @Override - public String from(String targetPropertyAccessor, Type type) { + public String getFromConversionString(String targetReference, Context conversionContext) { return sourceType.getSimpleName() + ".parse" + Strings.capitalize( primitiveType.getSimpleName() ) + "( " + - targetPropertyAccessor + " )"; + targetReference + " )"; } } diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToWrapperConversion.java b/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToWrapperConversion.java index aa6a98c6d..e4059d4ea 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToWrapperConversion.java +++ b/processor/src/main/java/org/mapstruct/ap/conversion/WrapperToWrapperConversion.java @@ -18,10 +18,14 @@ */ package org.mapstruct.ap.conversion; -import org.mapstruct.ap.model.Type; import org.mapstruct.ap.util.NativeTypes; -public class WrapperToWrapperConversion implements Conversion { +/** + * Conversion between wrapper types such as {@link Integer} or {@link Long}. + * + * @author Gunnar Morling + */ +public class WrapperToWrapperConversion extends SimpleConversion { private final Class sourceType; private final Class targetType; @@ -39,22 +43,22 @@ public class WrapperToWrapperConversion implements Conversion { } @Override - public String to(String sourcePropertyAccessor, Type type) { + public String getToConversionString(String sourceReference, Context conversionContext) { if ( sourceType == targetType ) { - return sourcePropertyAccessor; + return sourceReference; } else { - return sourcePropertyAccessor + "." + targetType.getName() + "Value()"; + return sourceReference + "." + targetType.getName() + "Value()"; } } @Override - public String from(String targetPropertyAccessor, Type type) { + public String getFromConversionString(String targetReference, Context conversionContext) { if ( sourceType == targetType ) { - return targetPropertyAccessor; + return targetReference; } else { - return targetPropertyAccessor + "." + sourceType.getName() + "Value()"; + return targetReference + "." + sourceType.getName() + "Value()"; } } } diff --git a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java index f9dc6783d..938fc1ccb 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/model/IterableMappingMethod.java @@ -18,12 +18,10 @@ */ package org.mapstruct.ap.model; -import org.mapstruct.ap.conversion.Conversion; - /** * A {@link MappingMethod} implemented by a {@link Mapper} class which maps one * iterable type to another. The collection elements are mapped either by a - * {@link Conversion} or another mapping method. + * {@link TypeConversion} or another mapping method. * * @author Gunnar Morling */ 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 7844e7855..fdb3634b4 100644 --- a/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java +++ b/processor/src/main/java/org/mapstruct/ap/model/PropertyMapping.java @@ -18,10 +18,9 @@ */ package org.mapstruct.ap.model; +import java.util.HashSet; import java.util.Set; -import org.mapstruct.ap.util.Collections; - /** * Represents the mapping between a source and target property, e.g. from * {@code String Source#foo} to {@code int Target#bar}. Name and type of source @@ -44,11 +43,11 @@ public class PropertyMapping extends AbstractModelElement { private final Type targetType; private final MappingMethodReference mappingMethod; - private final String conversion; + private final TypeConversion conversion; public PropertyMapping(String sourceBeanName, String targetBeanName, String sourceName, String sourceAccessorName, Type sourceType, String targetName, String targetAccessorName, Type targetType, - MappingMethodReference mappingMethod, String conversion) { + MappingMethodReference mappingMethod, TypeConversion conversion) { this.sourceBeanName = sourceBeanName; this.targetBeanName = targetBeanName; @@ -100,13 +99,20 @@ public class PropertyMapping extends AbstractModelElement { return mappingMethod; } - public String getConversion() { + public TypeConversion getConversion() { return conversion; } @Override public Set getImportTypes() { - return Collections.asSet( sourceType, targetType ); + Set importTypes = new HashSet(); + importTypes.add( sourceType ); + importTypes.add( targetType ); + if ( conversion != null ) { + importTypes.addAll( conversion.getImportTypes() ); + } + + return importTypes; } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/model/TypeConversion.java b/processor/src/main/java/org/mapstruct/ap/model/TypeConversion.java new file mode 100644 index 000000000..8ac7cd590 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/model/TypeConversion.java @@ -0,0 +1,60 @@ +/** + * Copyright 2012-2013 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.model; + +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * An inline conversion between source and target type of a mapping. + * + * @author Gunnar Morling + */ +public class TypeConversion extends AbstractModelElement { + + private final Set importTypes; + private final List exceptionTypes; + private final String conversionString; + + public TypeConversion(String conversionString) { + this( Collections.emptySet(), Collections.emptyList(), conversionString ); + } + + public TypeConversion(Set importTypes, List exceptionTypes, String conversionString) { + this.importTypes = new HashSet( importTypes ); + this.importTypes.addAll( exceptionTypes ); + this.exceptionTypes = exceptionTypes; + this.conversionString = conversionString; + } + + @Override + public Set getImportTypes() { + return importTypes; + } + + public List getExceptionTypes() { + return exceptionTypes; + } + + public String getConversionString() { + return conversionString; + } +} 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 e46106456..d9762be2f 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 @@ -35,6 +35,7 @@ public class Mapping { private final String sourceName; private final String targetName; + private final String dateFormat; private final AnnotationMirror mirror; private final AnnotationValue sourceAnnotationValue; private final AnnotationValue targetAnnotationValue; @@ -53,17 +54,18 @@ public class Mapping { return new Mapping( mapping.source(), mapping.target(), + mapping.dateFormat(), mapping.mirror, mapping.values.source(), mapping.values.target() ); } - private Mapping(String sourceName, String targetName, AnnotationMirror mirror, - AnnotationValue sourceAnnotationValue, - AnnotationValue targetAnnotationValue) { + private Mapping(String sourceName, String targetName, String dateFormat, AnnotationMirror mirror, + AnnotationValue sourceAnnotationValue, AnnotationValue targetAnnotationValue) { this.sourceName = sourceName; - this.targetName = targetName; + this.targetName = targetName.equals( "" ) ? sourceName : targetName; + this.dateFormat = dateFormat; this.mirror = mirror; this.sourceAnnotationValue = sourceAnnotationValue; this.targetAnnotationValue = targetAnnotationValue; @@ -77,6 +79,10 @@ public class Mapping { return targetName; } + public String getDateFormat() { + return dateFormat; + } + public AnnotationMirror getMirror() { return mirror; } @@ -90,7 +96,7 @@ public class Mapping { } public Mapping reverse() { - return new Mapping( targetName, sourceName, mirror, sourceAnnotationValue, targetAnnotationValue ); + return new Mapping( targetName, sourceName, dateFormat, mirror, sourceAnnotationValue, targetAnnotationValue ); } @Override 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 70003e2f3..ed2ad78bf 100644 --- a/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/processor/MapperCreationProcessor.java @@ -36,8 +36,9 @@ import javax.lang.model.util.Types; import javax.tools.Diagnostic.Kind; import org.mapstruct.ap.MapperPrism; -import org.mapstruct.ap.conversion.Conversion; +import org.mapstruct.ap.conversion.ConversionProvider; import org.mapstruct.ap.conversion.Conversions; +import org.mapstruct.ap.conversion.DefaultConversionContext; import org.mapstruct.ap.model.BeanMappingMethod; import org.mapstruct.ap.model.DefaultMapperReference; import org.mapstruct.ap.model.IterableMappingMethod; @@ -213,6 +214,7 @@ public class MapperCreationProcessor implements ModelElementProcessor methods, Method method, ExecutableElement getterMethod, - ExecutableElement setterMethod) { + ExecutableElement setterMethod, String dateFormat) { Type sourceType = executables.retrieveReturnType( getterMethod ); Type targetType = executables.retrieveParameter( setterMethod ).getType(); MappingMethodReference propertyMappingMethod = getMappingMethodReference( methods, sourceType, targetType ); - Conversion conversion = conversions.getConversion( + ConversionProvider conversionProvider = conversions.getConversion( sourceType, targetType ); @@ -330,9 +333,9 @@ public class MapperCreationProcessor implements ModelElementProcessor methods, Type parameterType, diff --git a/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl index dec73c084..dafa5a6bf 100644 --- a/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl +++ b/processor/src/main/resources/org.mapstruct.ap.model.PropertyMapping.ftl @@ -25,10 +25,32 @@ <#elseif conversion??> <#if sourceType.primitive == false> if ( ${sourceBeanName}.${sourceAccessorName}() != null ) { - ${targetBeanName}.${targetAccessorName}( ${conversion} ); + <#if (conversion.exceptionTypes?size == 0) > + ${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> ); + <#else> + try { + ${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> ); + } + <#list conversion.exceptionTypes as exceptionType> + catch( ${exceptionType.name} e ) { + throw new RuntimeException( e ); + } + + } <#else> - ${targetBeanName}.${targetAccessorName}( ${conversion} ); + <#if (conversion.exceptionTypes?size == 0) > + ${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> ); + <#else> + try { + ${targetBeanName}.${targetAccessorName}( <@includeModel object=conversion/> ); + } + <#list conversion.exceptionTypes as exceptionType> + catch( ${exceptionType.name} e ) { + throw new RuntimeException( e ); + } + + <#-- c) simply set --> <#else> diff --git a/processor/src/main/resources/org.mapstruct.ap.model.TypeConversion.ftl b/processor/src/main/resources/org.mapstruct.ap.model.TypeConversion.ftl new file mode 100644 index 000000000..38b372856 --- /dev/null +++ b/processor/src/main/resources/org.mapstruct.ap.model.TypeConversion.ftl @@ -0,0 +1,21 @@ +<#-- + + Copyright 2012-2013 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. + +--> +${conversionString} \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java new file mode 100644 index 000000000..9b6d5caa6 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/DateConversionTest.java @@ -0,0 +1,75 @@ +/** + * Copyright 2012-2013 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.test.conversion.date; + +import java.util.GregorianCalendar; +import java.util.Locale; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.MapperTestBase; +import org.mapstruct.ap.testutil.WithClasses; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import static org.fest.assertions.Assertions.assertThat; + +/** + * Tests application of format strings for conversions between strings and dates. + * + * @author Gunnar Morling + */ +@WithClasses({ + Source.class, + Target.class, + SourceTargetMapper.class +}) +@IssueKey("43") +public class DateConversionTest extends MapperTestBase { + + @BeforeMethod + public void setDefaultLocale() { + Locale.setDefault( Locale.GERMAN ); + } + + @Test + public void shouldApplyDateFormatForConversions() { + Source source = new Source(); + source.setDate( new GregorianCalendar( 2013, 6, 6 ).getTime() ); + source.setAnotherDate( new GregorianCalendar( 2013, 1, 14 ).getTime() ); + + Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getDate() ).isEqualTo( "06.07.2013" ); + assertThat( target.getAnotherDate() ).isEqualTo( "14.02.13 00:00" ); + } + + @Test + public void shouldApplyDateFormatForConversionInReverseMapping() { + Target target = new Target(); + target.setDate( "06.07.2013" ); + target.setAnotherDate( "14.02.13 8:30" ); + + Source source = SourceTargetMapper.INSTANCE.targetToSource( target ); + + assertThat( source ).isNotNull(); + assertThat( source.getDate() ).isEqualTo( new GregorianCalendar( 2013, 6, 6 ).getTime() ); + assertThat( source.getAnotherDate() ).isEqualTo( new GregorianCalendar( 2013, 1, 14, 8, 30 ).getTime() ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java new file mode 100644 index 000000000..c56dddb7a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Source.java @@ -0,0 +1,43 @@ +/** + * Copyright 2012-2013 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.test.conversion.date; + +import java.util.Date; + +public class Source { + + private Date date; + private Date anotherDate; + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public Date getAnotherDate() { + return anotherDate; + } + + public void setAnotherDate(Date anotherDate) { + this.anotherDate = anotherDate; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/conversion/Conversion.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/SourceTargetMapper.java similarity index 66% rename from processor/src/main/java/org/mapstruct/ap/conversion/Conversion.java rename to processor/src/test/java/org/mapstruct/ap/test/conversion/date/SourceTargetMapper.java index 96f3fdf11..1e0bb1a8b 100644 --- a/processor/src/main/java/org/mapstruct/ap/conversion/Conversion.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/SourceTargetMapper.java @@ -16,13 +16,19 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.conversion; +package org.mapstruct.ap.test.conversion.date; -import org.mapstruct.ap.model.Type; +import org.mapstruct.Mapper; +import org.mapstruct.Mappers; +import org.mapstruct.Mapping; -public interface Conversion { +@Mapper +public interface SourceTargetMapper { - String to(String sourcePropertyAccessor, Type type); + SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); - String from(String targetPropertyAccessor, Type type); + @Mapping(source = "date", dateFormat = "dd.MM.yyyy") + Target sourceToTarget(Source source); + + Source targetToSource(Target target); } diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java new file mode 100644 index 000000000..96778c62b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/date/Target.java @@ -0,0 +1,41 @@ +/** + * Copyright 2012-2013 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.test.conversion.date; + +public class Target { + + private String date; + private String anotherDate; + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } + + public String getAnotherDate() { + return anotherDate; + } + + public void setAnotherDate(String anotherDate) { + this.anotherDate = anotherDate; + } +}