diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java index f5d888d63..4652a1532 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/BuiltInMappingMethods.java @@ -51,6 +51,8 @@ public class BuiltInMappingMethods { if ( isJava8TimeAvailable( typeFactory ) ) { builtInMethods.add( new ZonedDateTimeToCalendar( typeFactory ) ); builtInMethods.add( new CalendarToZonedDateTime( typeFactory ) ); + builtInMethods.add( new XmlGregorianCalendarToLocalDate( typeFactory ) ); + builtInMethods.add( new LocalDateToXmlGregorianCalendar( typeFactory ) ); } } diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java new file mode 100644 index 000000000..d39fcdb70 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.java @@ -0,0 +1,70 @@ +/** + * Copyright 2012-2016 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.internal.model.source.builtin; + +import static org.mapstruct.ap.internal.util.Collections.asSet; + +import java.time.LocalDate; +import java.util.Set; + +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeConstants; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.ap.internal.model.common.Parameter; +import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.common.TypeFactory; + +/** + * @author Gunnar Morling + */ +public class LocalDateToXmlGregorianCalendar extends BuiltInMethod { + + private final Parameter parameter; + private final Type returnType; + private final Set importTypes; + + public LocalDateToXmlGregorianCalendar(TypeFactory typeFactory) { + this.parameter = new Parameter( "localDate", typeFactory.getType( LocalDate.class ) ); + this.returnType = typeFactory.getType( XMLGregorianCalendar.class ); + this.importTypes = asSet( + returnType, + parameter.getType(), + typeFactory.getType( DatatypeFactory.class ), + typeFactory.getType( DatatypeConfigurationException.class ), + typeFactory.getType( DatatypeConstants.class ) + ); + } + + @Override + public Parameter getParameter() { + return parameter; + } + + @Override + public Type getReturnType() { + return returnType; + } + + @Override + public Set getImportTypes() { + return importTypes; + } +} diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java new file mode 100644 index 000000000..4c6c30965 --- /dev/null +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.java @@ -0,0 +1,61 @@ +/** + * Copyright 2012-2016 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.internal.model.source.builtin; + +import static org.mapstruct.ap.internal.util.Collections.asSet; + +import java.time.LocalDate; +import java.util.Set; + +import javax.xml.datatype.XMLGregorianCalendar; + +import org.mapstruct.ap.internal.model.common.Parameter; +import org.mapstruct.ap.internal.model.common.Type; +import org.mapstruct.ap.internal.model.common.TypeFactory; + +/** + * @author Gunnar Morling + */ +public class XmlGregorianCalendarToLocalDate extends BuiltInMethod { + + private final Parameter parameter; + private final Type returnType; + private final Set importTypes; + + public XmlGregorianCalendarToLocalDate(TypeFactory typeFactory) { + this.parameter = new Parameter( "xcal", typeFactory.getType( XMLGregorianCalendar.class ) ); + this.returnType = typeFactory.getType( LocalDate.class ); + this.importTypes = asSet( returnType, parameter.getType() ); + } + + @Override + public Parameter getParameter() { + return parameter; + } + + @Override + public Type getReturnType() { + return returnType; + } + + @Override + public Set getImportTypes() { + return importTypes; + } +} 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 new file mode 100644 index 000000000..d184fff7f --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/LocalDateToXmlGregorianCalendar.ftl @@ -0,0 +1,37 @@ +<#-- + + Copyright 2012-2016 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. + +--> +private static XMLGregorianCalendar ${name}( java.time.LocalDate localDate ) { + if ( localDate == null ) { + return null; + } + + try { + return DatatypeFactory.newInstance().newXMLGregorianCalendarDate( + localDate.getYear(), + localDate.getMonthValue(), + localDate.getDayOfMonth(), + DatatypeConstants.FIELD_UNDEFINED + ); + } + catch ( DatatypeConfigurationException ex ) { + throw new RuntimeException( ex ); + } +} \ No newline at end of file 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 new file mode 100644 index 000000000..ff0a99dd8 --- /dev/null +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/XmlGregorianCalendarToLocalDate.ftl @@ -0,0 +1,27 @@ +<#-- + + Copyright 2012-2016 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. + +--> +private static java.time.LocalDate ${name}( XMLGregorianCalendar xcal ) { + if ( xcal == null ) { + return null; + } + + return java.time.LocalDate.of( xcal.getYear(), xcal.getMonth(), xcal.getDay() ); +} \ No newline at end of file diff --git a/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/Java8TimeConversionTest.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/Java8TimeConversionTest.java index ba36e8234..e30638e97 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/Java8TimeConversionTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/Java8TimeConversionTest.java @@ -35,7 +35,7 @@ import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; /** - * + * Tests for conversions to/from Java 8 date and time types. */ @RunWith(AnnotationProcessorTestRunner.class) @WithClasses({ Source.class, Target.class, SourceTargetMapper.class }) diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/Issue580Test.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/LocalDateToXMLGregorianCalendarConversionTest.java similarity index 86% rename from processor/src/test/java/org/mapstruct/ap/test/bugs/_580/Issue580Test.java rename to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/LocalDateToXMLGregorianCalendarConversionTest.java index 74773a637..cd4bf7c9a 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/Issue580Test.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/LocalDateToXMLGregorianCalendarConversionTest.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.bugs._580; +package org.mapstruct.ap.test.conversion.java8time; import static org.assertj.core.api.Assertions.assertThat; @@ -28,9 +28,9 @@ import javax.xml.datatype.XMLGregorianCalendar; import org.junit.Test; import org.junit.runner.RunWith; -import org.mapstruct.ap.test.bugs._580.java8.Source; -import org.mapstruct.ap.test.bugs._580.java8.SourceTargetMapper; -import org.mapstruct.ap.test.bugs._580.java8.Target; +import org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion.Source; +import org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion.SourceTargetMapper; +import org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion.Target; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @@ -41,7 +41,7 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; @IssueKey("580") @WithClasses({ Source.class, Target.class, SourceTargetMapper.class }) @RunWith(AnnotationProcessorTestRunner.class) -public class Issue580Test { +public class LocalDateToXMLGregorianCalendarConversionTest { @Test public void shouldNullCheckOnBuiltinAndConversion() { diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Source.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Source.java similarity index 92% rename from processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Source.java rename to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Source.java index 848ef3b6c..f45cdf23d 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Source.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Source.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.bugs._580.java8; +package org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion; import javax.xml.datatype.XMLGregorianCalendar; diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/SourceTargetMapper.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/SourceTargetMapper.java similarity index 92% rename from processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/SourceTargetMapper.java rename to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/SourceTargetMapper.java index 1340b7ea0..b815afb82 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/SourceTargetMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/SourceTargetMapper.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.bugs._580.java8; +package org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Target.java b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Target.java similarity index 91% rename from processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Target.java rename to processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Target.java index b2a9662ad..296f7384f 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bugs/_580/java8/Target.java +++ b/processor/src/test/java/org/mapstruct/ap/test/conversion/java8time/localdatetoxmlgregoriancalendarconversion/Target.java @@ -16,7 +16,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.mapstruct.ap.test.bugs._580.java8; +package org.mapstruct.ap.test.conversion.java8time.localdatetoxmlgregoriancalendarconversion; import java.time.LocalDate;