From 5540efc482f28b0a3c758702aa7bb4a6f0d14e42 Mon Sep 17 00:00:00 2001 From: Christian Bandowski Date: Sat, 21 Apr 2018 18:59:29 +0200 Subject: [PATCH] #1425 Added findType to VirtualMappingMethod and use it in all builtin templates Together with the includeModel directive this will ensure that the type will be written to the file as a FQN if required, otherwise as a simple name. --- .../tests/FullFeatureCompilationTest.java | 1 + .../internal/model/VirtualMappingMethod.java | 23 ++++++++ .../CalendarToXmlGregorianCalendar.ftl | 8 +-- .../builtin/CalendarToZonedDateTime.ftl | 4 +- .../builtin/DateToXmlGregorianCalendar.ftl | 8 +-- .../model/source/builtin/JaxbElemToValue.ftl | 2 +- .../JodaDateTimeToXmlGregorianCalendar.ftl | 6 +-- ...odaLocalDateTimeToXmlGregorianCalendar.ftl | 8 +-- .../JodaLocalDateToXmlGregorianCalendar.ftl | 8 +-- .../JodaLocalTimeToXmlGregorianCalendar.ftl | 8 +-- .../LocalDateToXmlGregorianCalendar.ftl | 8 +-- .../builtin/StringToXmlGregorianCalendar.ftl | 14 ++--- .../XmlGregorianCalendarToCalendar.ftl | 4 +- .../builtin/XmlGregorianCalendarToDate.ftl | 2 +- .../XmlGregorianCalendarToJodaDateTime.ftl | 48 ++++++++--------- .../XmlGregorianCalendarToJodaLocalDate.ftl | 10 ++-- ...mlGregorianCalendarToJodaLocalDateTime.ftl | 24 ++++----- .../XmlGregorianCalendarToJodaLocalTime.ftl | 18 +++---- .../XmlGregorianCalendarToLocalDate.ftl | 4 +- .../builtin/XmlGregorianCalendarToString.ftl | 6 +-- .../builtin/ZonedDateTimeToCalendar.ftl | 4 +- .../ap/test/bugs/_1425/Issue1425Mapper.java | 39 ++++++++++++++ .../ap/test/bugs/_1425/Issue1425Test.java | 52 +++++++++++++++++++ .../mapstruct/ap/test/bugs/_1425/Source.java | 37 +++++++++++++ .../mapstruct/ap/test/bugs/_1425/Target.java | 37 +++++++++++++ 25 files changed, 286 insertions(+), 97 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Test.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Target.java diff --git a/integrationtest/src/test/java/org/mapstruct/itest/tests/FullFeatureCompilationTest.java b/integrationtest/src/test/java/org/mapstruct/itest/tests/FullFeatureCompilationTest.java index f53b673d9..46dc4ce6b 100644 --- a/integrationtest/src/test/java/org/mapstruct/itest/tests/FullFeatureCompilationTest.java +++ b/integrationtest/src/test/java/org/mapstruct/itest/tests/FullFeatureCompilationTest.java @@ -73,6 +73,7 @@ public class FullFeatureCompilationTest { case ECLIPSE_JDT_JAVA_6: case ORACLE_JAVA_7: case ECLIPSE_JDT_JAVA_7: + additionalExcludes.add( "org/mapstruct/ap/test/bugs/_1425/*.java" ); additionalExcludes.add( "**/java8*/**/*.java" ); break; case ORACLE_JAVA_9: diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java index dda42376b..dbaec580e 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/VirtualMappingMethod.java @@ -56,6 +56,29 @@ public class VirtualMappingMethod extends MappingMethod { return importTypes; } + /** + * Finds a {@link Type} by a given name. The {@code name} will be compared to the fully-qualified and also simple + * names of the {@code importTypes}. + * + * @param name Fully-qualified or simple name of the type. + * + * @return Found type, never null. + * + * @throws IllegalArgumentException In case no {@link Type} was found for given name. + */ + public Type findType(String name) { + for ( Type type : importTypes ) { + if ( type.getFullyQualifiedName().contentEquals( name ) ) { + return type; + } + if ( type.getName().contentEquals( name ) ) { + return type; + } + } + + throw new IllegalArgumentException( "No type for given name '" + name + "' found in 'importTypes'." ); + } + @Override public int hashCode() { final int prime = 31; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl index b716b46f2..2e74ffa07 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToXmlGregorianCalendar.ftl @@ -19,17 +19,17 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( Calendar cal ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("Calendar")/> cal ) { if ( cal == null ) { return null; } try { - GregorianCalendar gcal = new GregorianCalendar(); + <@includeModel object=findType("GregorianCalendar")/> gcal = new <@includeModel object=findType("GregorianCalendar")/>(); gcal.setTimeInMillis( cal.getTimeInMillis() ); - return DatatypeFactory.newInstance().newXMLGregorianCalendar( gcal ); + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( gcal ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl index 8ca07603a..03815fac7 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/CalendarToZonedDateTime.ftl @@ -19,10 +19,10 @@ limitations under the License. --> -private ZonedDateTime ${name}(Calendar cal) { +private <@includeModel object=findType("ZonedDateTime")/> ${name}(<@includeModel object=findType("Calendar")/> cal) { if ( cal == null ) { return null; } - return ZonedDateTime.ofInstant( cal.toInstant(), cal.getTimeZone().toZoneId() ); + return <@includeModel object=findType("ZonedDateTime")/>.ofInstant( cal.toInstant(), cal.getTimeZone().toZoneId() ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl index 6442c12ec..3ed932884 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/DateToXmlGregorianCalendar.ftl @@ -19,17 +19,17 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( Date date ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("Date")/> date ) { if ( date == null ) { return null; } try { - GregorianCalendar c = new GregorianCalendar(); + <@includeModel object=findType("GregorianCalendar")/> c = new <@includeModel object=findType("GregorianCalendar")/>(); c.setTime( date ); - return DatatypeFactory.newInstance().newXMLGregorianCalendar( c ); + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( c ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl index af314d442..bc7da073b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JaxbElemToValue.ftl @@ -19,7 +19,7 @@ limitations under the License. --> -private T ${name}( JAXBElement element ) { +private T ${name}( <@includeModel object=findType("JAXBElement") raw=true/> element ) { if ( element == null ) { return null; } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl index 386ea8b8f..65677e731 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaDateTimeToXmlGregorianCalendar.ftl @@ -19,13 +19,13 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( DateTime dt ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("DateTime")/> dt ) { if ( dt == null ) { return null; } try { - return DatatypeFactory.newInstance().newXMLGregorianCalendar( + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), @@ -35,7 +35,7 @@ private XMLGregorianCalendar ${name}( DateTime dt ) { dt.getMillisOfSecond(), dt.getZone().getOffset( null ) / 60000 ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl index 04c5c9810..3e177214e 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateTimeToXmlGregorianCalendar.ftl @@ -19,13 +19,13 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( LocalDateTime dt ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("org.joda.time.LocalDateTime")/> dt ) { if ( dt == null ) { return null; } try { - return DatatypeFactory.newInstance().newXMLGregorianCalendar( + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), @@ -33,9 +33,9 @@ private XMLGregorianCalendar ${name}( LocalDateTime dt ) { dt.getMinuteOfHour(), dt.getSecondOfMinute(), dt.getMillisOfSecond(), - DatatypeConstants.FIELD_UNDEFINED ); + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl index f3244626e..5b77e2314 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalDateToXmlGregorianCalendar.ftl @@ -19,19 +19,19 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( LocalDate dt ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("org.joda.time.LocalDate")/> dt ) { if ( dt == null ) { return null; } try { - return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendarDate( dt.getYear(), dt.getMonthOfYear(), dt.getDayOfMonth(), - DatatypeConstants.FIELD_UNDEFINED ); + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl index c518fb852..2add1e79c 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/JodaLocalTimeToXmlGregorianCalendar.ftl @@ -19,20 +19,20 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( LocalTime dt ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("org.joda.time.LocalTime")/> dt ) { if ( dt == null ) { return null; } try { - return DatatypeFactory.newInstance().newXMLGregorianCalendarTime( + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendarTime( dt.getHourOfDay(), dt.getMinuteOfHour(), dt.getSecondOfMinute(), dt.getMillisOfSecond(), - DatatypeConstants.FIELD_UNDEFINED ); + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl index 6103838c8..e1139fed2 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl @@ -19,20 +19,20 @@ limitations under the License. --> -private static XMLGregorianCalendar ${name}( java.time.LocalDate localDate ) { +private static <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( <@includeModel object=findType("java.time.LocalDate")/> localDate ) { if ( localDate == null ) { return null; } try { - return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendarDate( localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth(), - DatatypeConstants.FIELD_UNDEFINED + <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ); } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl index 0cac9015c..0e8db79d1 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/StringToXmlGregorianCalendar.ftl @@ -19,26 +19,26 @@ limitations under the License. --> -private XMLGregorianCalendar ${name}( String date, String dateFormat ) { +private <@includeModel object=findType("XMLGregorianCalendar")/> ${name}( String date, String dateFormat ) { if ( date == null ) { return null; } try { if ( dateFormat != null ) { - DateFormat df = new SimpleDateFormat( dateFormat ); - GregorianCalendar c = new GregorianCalendar(); + <@includeModel object=findType("DateFormat")/> df = new <@includeModel object=findType("SimpleDateFormat")/>( dateFormat ); + <@includeModel object=findType("GregorianCalendar")/> c = new <@includeModel object=findType("GregorianCalendar")/>(); c.setTime( df.parse( date ) ); - return DatatypeFactory.newInstance().newXMLGregorianCalendar( c ); + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( c ); } else { - return DatatypeFactory.newInstance().newXMLGregorianCalendar( date ); + return <@includeModel object=findType("DatatypeFactory")/>.newInstance().newXMLGregorianCalendar( date ); } } - catch ( DatatypeConfigurationException ex ) { + catch ( <@includeModel object=findType("DatatypeConfigurationException")/> ex ) { throw new RuntimeException( ex ); } - catch ( ParseException ex ) { + catch ( <@includeModel object=findType("ParseException")/> ex ) { throw new RuntimeException( ex ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl index 782ba83d7..2021d776d 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToCalendar.ftl @@ -19,12 +19,12 @@ limitations under the License. --> -private Calendar ${name}( XMLGregorianCalendar xcal ) { +private <@includeModel object=findType("Calendar")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } - Calendar cal = Calendar.getInstance(); + <@includeModel object=findType("Calendar")/> cal = <@includeModel object=findType("Calendar")/>.getInstance(); cal.setTimeInMillis( xcal.toGregorianCalendar().getTimeInMillis() ); return cal; } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl index a62cf38cd..88fe71b48 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToDate.ftl @@ -19,7 +19,7 @@ limitations under the License. --> -private static Date ${name}( XMLGregorianCalendar xcal ) { +private static <@includeModel object=findType("java.util.Date")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl index 80cdf4661..1e4799dc8 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaDateTime.ftl @@ -19,33 +19,33 @@ limitations under the License. --> -private static DateTime ${name}( XMLGregorianCalendar xcal ) { +private static <@includeModel object=findType("DateTime")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } - if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getHour() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMinute() != DatatypeConstants.FIELD_UNDEFINED + if ( xcal.getYear() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMonth() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getDay() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getHour() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMinute() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { - if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED ) { - return new DateTime( xcal.getYear(), + if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMillisecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getTimezone() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("DateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), xcal.getMinute(), xcal.getSecond(), xcal.getMillisecond(), - DateTimeZone.forOffsetMillis( xcal.getTimezone() * 60000 ) + <@includeModel object=findType("DateTimeZone")/>.forOffsetMillis( xcal.getTimezone() * 60000 ) ); } - else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED ) { - return new DateTime( xcal.getYear(), + else if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMillisecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("DateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), @@ -54,19 +54,19 @@ private static DateTime ${name}( XMLGregorianCalendar xcal ) { xcal.getMillisecond() ); } - else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED ) { - return new DateTime( xcal.getYear(), + else if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getTimezone() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("DateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), xcal.getMinute(), xcal.getSecond(), - DateTimeZone.forOffsetMillis( xcal.getTimezone() * 60000 ) + <@includeModel object=findType("DateTimeZone")/>.forOffsetMillis( xcal.getTimezone() * 60000 ) ); } - else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED ) { - return new DateTime( xcal.getYear(), + else if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("DateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), @@ -74,17 +74,17 @@ private static DateTime ${name}( XMLGregorianCalendar xcal ) { xcal.getSecond() ); } - else if ( xcal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED ) { - return new DateTime( xcal.getYear(), + else if ( xcal.getTimezone() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("DateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), xcal.getMinute(), - DateTimeZone.forOffsetMillis( xcal.getTimezone() * 60000 ) + <@includeModel object=findType("DateTimeZone")/>.forOffsetMillis( xcal.getTimezone() * 60000 ) ); } else { - return new DateTime( xcal.getYear(), + return new <@includeModel object=findType("DateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl index 24ec1391d..e51d87a13 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDate.ftl @@ -19,15 +19,15 @@ limitations under the License. --> -private static LocalDate ${name}( XMLGregorianCalendar xcal ) { +private static <@includeModel object=findType("org.joda.time.LocalDate")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } - if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED ) { - return new LocalDate( xcal.getYear(), xcal.getMonth(), xcal.getDay() ); + if ( xcal.getYear() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMonth() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getDay() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("org.joda.time.LocalDate")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay() ); } return null; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl index a57e607ba..edcf3e3a3 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalDateTime.ftl @@ -19,20 +19,20 @@ limitations under the License. --> -private static LocalDateTime ${name}( XMLGregorianCalendar xcal ) { +private static <@includeModel object=findType("org.joda.time.LocalDateTime")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } - if ( xcal.getYear() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMonth() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getDay() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getHour() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMinute() != DatatypeConstants.FIELD_UNDEFINED + if ( xcal.getYear() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMonth() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getDay() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getHour() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMinute() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { - if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED ) { - return new LocalDateTime( xcal.getYear(), + if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMillisecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("org.joda.time.LocalDateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), @@ -41,8 +41,8 @@ private static LocalDateTime ${name}( XMLGregorianCalendar xcal ) { xcal.getMillisecond() ); } - else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED ) { - return new LocalDateTime( xcal.getYear(), + else if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("org.joda.time.LocalDateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), @@ -51,7 +51,7 @@ private static LocalDateTime ${name}( XMLGregorianCalendar xcal ) { ); } else { - return new LocalDateTime( xcal.getYear(), + return new <@includeModel object=findType("org.joda.time.LocalDateTime")/>( xcal.getYear(), xcal.getMonth(), xcal.getDay(), xcal.getHour(), diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl index 9d0c2af94..139632445 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToJodaLocalTime.ftl @@ -19,30 +19,30 @@ limitations under the License. --> -private static LocalTime ${name}( XMLGregorianCalendar xcal ) { +private static <@includeModel object=findType("org.joda.time.LocalTime")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } - if ( xcal.getHour() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMinute() != DatatypeConstants.FIELD_UNDEFINED ) { - if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED - && xcal.getMillisecond() != DatatypeConstants.FIELD_UNDEFINED ) { - return new LocalTime( xcal.getHour(), + if ( xcal.getHour() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMinute() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED + && xcal.getMillisecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("org.joda.time.LocalTime")/>( xcal.getHour(), xcal.getMinute(), xcal.getSecond(), xcal.getMillisecond() ); } - else if ( xcal.getSecond() != DatatypeConstants.FIELD_UNDEFINED ) { - return new LocalTime( + else if ( xcal.getSecond() != <@includeModel object=findType("DatatypeConstants")/>.FIELD_UNDEFINED ) { + return new <@includeModel object=findType("org.joda.time.LocalTime")/>( xcal.getHour(), xcal.getMinute(), xcal.getSecond() ); } else { - return new LocalTime( xcal.getHour(), + return new <@includeModel object=findType("org.joda.time.LocalTime")/>( xcal.getHour(), xcal.getMinute() ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl index 2a53fcd6e..fc69fdd5b 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl @@ -19,10 +19,10 @@ limitations under the License. --> -private static java.time.LocalDate ${name}( XMLGregorianCalendar xcal ) { +private static <@includeModel object=findType("java.time.LocalDate")/> ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal ) { if ( xcal == null ) { return null; } - return java.time.LocalDate.of( xcal.getYear(), xcal.getMonth(), xcal.getDay() ); + return <@includeModel object=findType("java.time.LocalDate")/>.of( xcal.getYear(), xcal.getMonth(), xcal.getDay() ); } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl index 63437c8b6..5a705aa74 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToString.ftl @@ -19,7 +19,7 @@ limitations under the License. --> -private String ${name}( XMLGregorianCalendar xcal, String dateFormat ) { +private String ${name}( <@includeModel object=findType("XMLGregorianCalendar")/> xcal, String dateFormat ) { if ( xcal == null ) { return null; } @@ -28,8 +28,8 @@ private String ${name}( XMLGregorianCalendar xcal, String dateFormat ) { return xcal.toString(); } else { - Date d = xcal.toGregorianCalendar().getTime(); - SimpleDateFormat sdf = new SimpleDateFormat( dateFormat ); + <@includeModel object=findType("java.util.Date")/> d = xcal.toGregorianCalendar().getTime(); + <@includeModel object=findType("SimpleDateFormat")/> sdf = new <@includeModel object=findType("SimpleDateFormat")/>( dateFormat ); return sdf.format( d ); } } diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl index 88c943e5f..47a951a29 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ZonedDateTimeToCalendar.ftl @@ -19,12 +19,12 @@ limitations under the License. --> -private Calendar ${name}(ZonedDateTime dateTime) { +private <@includeModel object=findType("Calendar")/> ${name}(<@includeModel object=findType("ZonedDateTime")/> dateTime) { if ( dateTime == null ) { return null; } - Calendar instance = Calendar.getInstance( TimeZone.getTimeZone( dateTime.getZone() ) ); + <@includeModel object=findType("Calendar")/> instance = <@includeModel object=findType("Calendar")/>.getInstance( TimeZone.getTimeZone( dateTime.getZone() ) ); instance.setTimeInMillis( dateTime.toInstant().toEpochMilli() ); return instance; } diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Mapper.java new file mode 100644 index 000000000..f61f8c4a7 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Mapper.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2017 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.bugs._1425; + +import java.time.LocalDate; + +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +/** + * @author Christian Bandowski + */ +@Mapper +public abstract class Issue1425Mapper { + + public static final Issue1425Mapper INSTANCE = Mappers.getMapper( Issue1425Mapper.class ); + + public abstract Target map(Source source); + + LocalDate now() { + return LocalDate.now(); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Test.java new file mode 100644 index 000000000..15ee02797 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Issue1425Test.java @@ -0,0 +1,52 @@ +/** + * Copyright 2012-2017 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.bugs._1425; + +import org.joda.time.LocalDate; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Christian Bandowski + */ +@WithClasses({ + Issue1425Mapper.class, + Source.class, + Target.class +}) +@RunWith(AnnotationProcessorTestRunner.class) +@IssueKey("1425") +public class Issue1425Test { + + @Test + public void shouldTestMappingLocalDates() { + Source source = new Source(); + source.setValue( LocalDate.parse( "2018-04-18" ) ); + + Target target = Issue1425Mapper.INSTANCE.map( source ); + + assertThat( target ).isNotNull(); + assertThat( target.getValue() ).isEqualTo( "2018-04-18" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Source.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Source.java new file mode 100644 index 000000000..2c97acc5f --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Source.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012-2017 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.bugs._1425; + +import org.joda.time.LocalDate; + +/** + * @author Christian Bandowski + */ +public class Source { + + private LocalDate value; + + public LocalDate getValue() { + return value; + } + + public void setValue(LocalDate value) { + this.value = value; + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Target.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Target.java new file mode 100644 index 000000000..da1d0394a --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1425/Target.java @@ -0,0 +1,37 @@ +/** + * Copyright 2012-2017 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.bugs._1425; + +import java.time.LocalDate; + +/** + * @author Christian Bandowski + */ +public class Target { + + private LocalDate value; + + public LocalDate getValue() { + return value; + } + + public void setValue(LocalDate value) { + this.value = value; + } +}