From 8255a29103b5533d00f9c2e134ae091d14d18cf8 Mon Sep 17 00:00:00 2001 From: Paul King Date: Sun, 1 Jul 2018 22:54:01 +1000 Subject: [PATCH] GROOVY-8671: Move JaxbGroovyMethods into their own optional groovy-jaxb "module" --- gradle/binarycompatibility.gradle | 5 +- gradle/docs.gradle | 1 + gradle/pomconfigurer.gradle | 3 + gradle/upload.gradle | 7 +- settings.gradle | 1 + subprojects/groovy-jaxb/build.gradle | 27 ++++++ .../jaxb/extensions/JaxbExtensions.java | 83 +++++++++++++++++++ .../extensions/JaxbExtensionsTest.groovy} | 8 +- .../groovy/jaxb/extensions}/Person.groovy | 8 +- subprojects/groovy-xml/build.gradle | 2 +- .../groovy/xml/jaxb/JaxbGroovyMethods.groovy | 7 +- 11 files changed, 138 insertions(+), 14 deletions(-) create mode 100644 subprojects/groovy-jaxb/build.gradle create mode 100644 subprojects/groovy-jaxb/src/main/java/org/apache/groovy/jaxb/extensions/JaxbExtensions.java rename subprojects/{groovy-xml/src/test/groovy/groovy/xml/jaxb/JaxbGroovyMethodsTest.groovy => groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/JaxbExtensionsTest.groovy} (90%) rename subprojects/{groovy-xml/src/test/groovy/groovy/xml/jaxb => groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions}/Person.groovy (90%) diff --git a/gradle/binarycompatibility.gradle b/gradle/binarycompatibility.gradle index 184a2e094cf..45b951dcfd5 100644 --- a/gradle/binarycompatibility.gradle +++ b/gradle/binarycompatibility.gradle @@ -35,7 +35,10 @@ task checkBinaryCompatibility { check.dependsOn(checkBinaryCompatibility) // for comparing between versions with different modules, set excludeModules to differing modules, e.g. -def excludeModules = ['groovy-cli-picocli', 'groovy-cli-commons', 'groovy-dateutil', 'groovy-datetime', 'performance', 'groovy-macro', 'tests-vm8', 'groovy-json-direct', 'groovy-test-junit5'] +def excludeModules = [ + 'groovy-cli-picocli', 'groovy-cli-commons', 'groovy-dateutil', 'groovy-datetime', 'groovy-jaxb', + 'groovy-macro', 'groovy-json-direct', 'groovy-test-junit5', 'performance', 'tests-vm8' +] //def excludeModules = [] Set projectsToCheck = allprojects.findAll{ !(it.name in excludeModules) } diff --git a/gradle/docs.gradle b/gradle/docs.gradle index c302216f7de..3472e3c7c17 100644 --- a/gradle/docs.gradle +++ b/gradle/docs.gradle @@ -156,6 +156,7 @@ task docGDK { arg(value: 'subprojects/groovy-dateutil/src/main/java/org/apache/groovy/dateutil/extensions/DateUtilExtensions.java') arg(value: 'subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeExtensions.java') arg(value: 'subprojects/groovy-datetime/src/main/java/org/apache/groovy/datetime/extensions/DateTimeStaticExtensions.java') + arg(value: 'subprojects/groovy-jaxb/src/main/groovy/org/apache/groovy/jaxb/extensions/JaxbExtensions.java') arg(value: 'subprojects/groovy-sql/src/main/java/org/apache/groovy/sql/extensions/SqlExtensions.java') arg(value: 'subprojects/groovy-swing/src/main/java/org/codehaus/groovy/runtime/SwingGroovyMethods.java') arg(value: 'subprojects/groovy-xml/src/main/java/org/codehaus/groovy/runtime/XmlGroovyMethods.java') diff --git a/gradle/pomconfigurer.gradle b/gradle/pomconfigurer.gradle index 33c62d31ed1..401f38dc967 100644 --- a/gradle/pomconfigurer.gradle +++ b/gradle/pomconfigurer.gradle @@ -633,6 +633,9 @@ project.ext.pomConfigureClosureWithoutTweaks = { contributor { name 'mgroovy' } + contributor { + name 'Dominik Przybysz' + } } mailingLists { mailingList { diff --git a/gradle/upload.gradle b/gradle/upload.gradle index 5c7e07b6867..ad984d96d97 100644 --- a/gradle/upload.gradle +++ b/gradle/upload.gradle @@ -201,7 +201,12 @@ allprojects { } } -def optionalModules = ['groovy-dateutil', 'groovy-cli-commons', 'groovy-json-direct'] +def optionalModules = [ + 'groovy-cli-commons', + 'groovy-dateutil', + 'groovy-jaxb', + 'groovy-json-direct' +] ext.pomAll = { addFilter('groovy') { artifact, file -> diff --git a/settings.gradle b/settings.gradle index f726532ebc1..4e4958f8795 100644 --- a/settings.gradle +++ b/settings.gradle @@ -26,6 +26,7 @@ def subprojects = ['groovy-ant', 'groovy-docgenerator', 'groovy-groovydoc', 'groovy-groovysh', + 'groovy-jaxb', 'groovy-jmx', 'groovy-json', 'groovy-json-direct', diff --git a/subprojects/groovy-jaxb/build.gradle b/subprojects/groovy-jaxb/build.gradle new file mode 100644 index 00000000000..4a271648008 --- /dev/null +++ b/subprojects/groovy-jaxb/build.gradle @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +dependencies { + compile rootProject + testCompile project(':groovy-test') +} + +task moduleDescriptor(type: org.codehaus.groovy.gradle.WriteExtensionDescriptorTask) { + extensionClasses = 'org.apache.groovy.jaxb.extensions.JaxbExtensions' +} +compileJava.dependsOn moduleDescriptor diff --git a/subprojects/groovy-jaxb/src/main/java/org/apache/groovy/jaxb/extensions/JaxbExtensions.java b/subprojects/groovy-jaxb/src/main/java/org/apache/groovy/jaxb/extensions/JaxbExtensions.java new file mode 100644 index 00000000000..76fe261bc7f --- /dev/null +++ b/subprojects/groovy-jaxb/src/main/java/org/apache/groovy/jaxb/extensions/JaxbExtensions.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.groovy.jaxb.extensions; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import java.io.StringReader; +import java.io.StringWriter; + +/** + * This class defines new groovy methods which appear on Jaxb-related JDK + * classes ({@code JAXBContext}, {@code Marshaller}) inside the Groovy environment. + * Static methods are used with the first parameter being the destination class. + */ +public class JaxbExtensions { + + /** + * Marshall an object to a xml {@code String}. + * + * @param self a Marshaller which can marshall the type of the given object + * @param jaxbElement object to marshall to a {@code String} + * @return {@code String} representing the object as xml + */ + public static String marshal(Marshaller self, T jaxbElement) throws JAXBException { + StringWriter sw = new StringWriter(); + self.marshal(jaxbElement, sw); + return sw.toString(); + } + + /** + * Marshall an object to a xml {@code String}. + * + * @param self a JaxbContext, which recognizes the type of the given object + * @param jaxbElement object to marshall to a {@code String} + * @return String representing the object as xml + */ + public static String marshal(JAXBContext self, T jaxbElement) throws JAXBException { + return marshal(self.createMarshaller(), jaxbElement); + } + + /** + * Unmarshal xml data from the given {@code String} to object of the given type. + * + * @param self Unmarshaller, a Unmarshaller which can unmarshall the type of the given object + * @param xml xml data as a {@link String} + * @param declaredType appropriate JAXB mapped class to hold node's xml data + * @return instance of destination class unmarshalled from xml + */ + public static T unmarshal(Unmarshaller self, String xml, Class declaredType) throws JAXBException { + StringReader sr = new StringReader(xml); + return declaredType.cast(self.unmarshal(sr)); + } + + /** + * Unmarshal xml data from the given {@code String} to object of the given type. + * + * @param self a JaxbContext, which recognizes the type of the given object + * @param xml xml data as a {@link String} + * @param declaredType appropriate JAXB mapped class to hold node's xml data + * @return instance of destination class unmarshalled from xml + */ + public static T unmarshal(JAXBContext self, String xml, Class declaredType) throws JAXBException { + return unmarshal(self.createUnmarshaller(), xml, declaredType); + } +} diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/xml/jaxb/JaxbGroovyMethodsTest.groovy b/subprojects/groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/JaxbExtensionsTest.groovy similarity index 90% rename from subprojects/groovy-xml/src/test/groovy/groovy/xml/jaxb/JaxbGroovyMethodsTest.groovy rename to subprojects/groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/JaxbExtensionsTest.groovy index 04bc500e29f..87ac31c7fa0 100644 --- a/subprojects/groovy-xml/src/test/groovy/groovy/xml/jaxb/JaxbGroovyMethodsTest.groovy +++ b/subprojects/groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/JaxbExtensionsTest.groovy @@ -16,16 +16,14 @@ * specific language governing permissions and limitations * under the License. */ -package groovy.xml.jaxb +package org.apache.groovy.jaxb.extensions import javax.xml.bind.JAXBContext /** - * Test cases for {@link JaxbGroovyMethods} - * - * @author Dominik Przybysz + * Test cases for {@link JaxbExtensions} */ -class JaxbGroovyMethodsTest extends GroovyTestCase { +class JaxbExtensionsTest extends GroovyTestCase { JAXBContext jaxbContext = JAXBContext.newInstance(Person) Person p = new Person(name: 'JT', age: 20) diff --git a/subprojects/groovy-xml/src/test/groovy/groovy/xml/jaxb/Person.groovy b/subprojects/groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/Person.groovy similarity index 90% rename from subprojects/groovy-xml/src/test/groovy/groovy/xml/jaxb/Person.groovy rename to subprojects/groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/Person.groovy index 862c071c767..a76b3a06fa3 100644 --- a/subprojects/groovy-xml/src/test/groovy/groovy/xml/jaxb/Person.groovy +++ b/subprojects/groovy-jaxb/src/test/groovy/org/apache/groovy/jaxb/extensions/Person.groovy @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package groovy.xml.jaxb +package org.apache.groovy.jaxb.extensions import groovy.transform.EqualsAndHashCode @@ -25,14 +25,12 @@ import javax.xml.bind.annotation.XmlAccessorType import javax.xml.bind.annotation.XmlRootElement /** - * DTO class for {@link JaxbGroovyMethodsTest} - * - * @author Dominik Przybysz + * DTO class for {@link JaxbExtensionsTest} */ @EqualsAndHashCode @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement -public class Person { +class Person { String name int age } diff --git a/subprojects/groovy-xml/build.gradle b/subprojects/groovy-xml/build.gradle index c5c284829f7..cf514dd1c41 100644 --- a/subprojects/groovy-xml/build.gradle +++ b/subprojects/groovy-xml/build.gradle @@ -24,6 +24,6 @@ dependencies { } task moduleDescriptor(type: org.codehaus.groovy.gradle.WriteExtensionDescriptorTask) { - extensionClasses = 'org.codehaus.groovy.runtime.XmlGroovyMethods,groovy.xml.jaxb.JaxbGroovyMethods' + extensionClasses = 'org.codehaus.groovy.runtime.XmlGroovyMethods' } compileJava.dependsOn moduleDescriptor \ No newline at end of file diff --git a/subprojects/groovy-xml/src/main/groovy/groovy/xml/jaxb/JaxbGroovyMethods.groovy b/subprojects/groovy-xml/src/main/groovy/groovy/xml/jaxb/JaxbGroovyMethods.groovy index 03233001917..a544b2e0f45 100644 --- a/subprojects/groovy-xml/src/main/groovy/groovy/xml/jaxb/JaxbGroovyMethods.groovy +++ b/subprojects/groovy-xml/src/main/groovy/groovy/xml/jaxb/JaxbGroovyMethods.groovy @@ -29,9 +29,10 @@ import javax.xml.bind.Unmarshaller * classes ({@code JAXBContext}, {@code Marshaller}) inside the Groovy environment. * Static methods are used with the first parameter being the destination class. * - * @author Dominik Przybysz + * @deprecated use org.apache.groovy.jaxb.extensions.JaxbExtensions */ @CompileStatic +@Deprecated class JaxbGroovyMethods { /** @@ -41,6 +42,7 @@ class JaxbGroovyMethods { * @param jaxbElement object to marshall to a {@code String} * @return {@code String} representing the object as xml */ + @Deprecated static String marshal(Marshaller self, T jaxbElement) { StringWriter sw = new StringWriter() self.marshal(jaxbElement, sw) @@ -54,6 +56,7 @@ class JaxbGroovyMethods { * @param jaxbElement object to marshall to a {@code String} * @return String representing the object as xml */ + @Deprecated static String marshal(JAXBContext self, T jaxbElement) { marshal(self.createMarshaller(), jaxbElement) } @@ -66,6 +69,7 @@ class JaxbGroovyMethods { * @param declaredType appropriate JAXB mapped class to hold node's xml data * @return instance of destination class unmarshalled from xml */ + @Deprecated static T unmarshal(Unmarshaller self, String xml, Class declaredType) { StringReader sr = new StringReader(xml) declaredType.cast(self.unmarshal(sr)) @@ -79,6 +83,7 @@ class JaxbGroovyMethods { * @param declaredType appropriate JAXB mapped class to hold node's xml data * @return instance of destination class unmarshalled from xml */ + @Deprecated static T unmarshal(JAXBContext self, String xml, Class declaredType) { unmarshal(self.createUnmarshaller(), xml, declaredType) }