mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#852 Adding explicit conversion for LocalDate <> XMLGregorianCalendar
This commit is contained in:
parent
e13696172e
commit
c64919829a
@ -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 ) );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Type> 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<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
@ -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<Type> 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<Type> getImportTypes() {
|
||||
return importTypes;
|
||||
}
|
||||
}
|
@ -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 );
|
||||
}
|
||||
}
|
@ -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() );
|
||||
}
|
@ -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 })
|
||||
|
@ -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() {
|
@ -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;
|
||||
|
@ -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;
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user