This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven.git
commit 49d1ec1668448528e0bc96bc3410144e0334002e Author: Guillaume Nodet <[email protected]> AuthorDate: Fri Oct 14 10:20:25 2022 +0200 Restore the serializable aspect of the Model --- .../maven/api/model/ImmutableCollections.java | 17 ++- .../maven/api/settings/ImmutableCollections.java | 13 ++- .../maven/api/toolchain/ImmutableCollections.java | 13 ++- .../java/org/apache/maven/model/BaseObject.java | 3 +- .../org/apache/maven/model/SerializationTest.java | 65 +++++++++++ maven-model/src/test/resources/xml/pom.xml | 123 +++++++++++++++++++++ .../plugin/lifecycle/ImmutableCollections.java | 13 ++- 7 files changed, 232 insertions(+), 15 deletions(-) diff --git a/api/maven-api-model/src/main/java/org/apache/maven/api/model/ImmutableCollections.java b/api/maven-api-model/src/main/java/org/apache/maven/api/model/ImmutableCollections.java index 35c98d6e7..e47fa1a8b 100644 --- a/api/maven-api-model/src/main/java/org/apache/maven/api/model/ImmutableCollections.java +++ b/api/maven-api-model/src/main/java/org/apache/maven/api/model/ImmutableCollections.java @@ -19,6 +19,7 @@ package org.apache.maven.api.model; * under the License. */ +import java.io.Serializable; import java.util.AbstractList; import java.util.AbstractMap; import java.util.AbstractSet; @@ -251,7 +252,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableList<E> extends AbstractList<E> implements RandomAccess + private abstract static class AbstractImmutableList<E> + extends AbstractList<E> + implements RandomAccess, Serializable { @Override public boolean add( E e ) @@ -541,7 +544,7 @@ class ImmutableCollections private static class Map1<K, V> extends AbstractImmutableMap<K, V> { - private final Map.Entry<K, V> entry; + private final Entry<K, V> entry; private Map1( K key, V value ) { @@ -618,7 +621,7 @@ class ImmutableCollections { if ( index < entries.length ) { - return ( Map.Entry<K, V> ) entries[index++]; + return ( Entry<K, V> ) entries[index++]; } throw new NoSuchElementException(); } @@ -634,7 +637,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableMap<K, V> extends AbstractMap<K, V> + private abstract static class AbstractImmutableMap<K, V> + extends AbstractMap<K, V> + implements Serializable { @Override public void replaceAll( BiFunction<? super K, ? super V, ? extends V> function ) @@ -691,7 +696,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableSet<E> extends AbstractSet<E> + private abstract static class AbstractImmutableSet<E> + extends AbstractSet<E> + implements Serializable { @Override public boolean removeAll( Collection<?> c ) diff --git a/api/maven-api-settings/src/main/java/org/apache/maven/api/settings/ImmutableCollections.java b/api/maven-api-settings/src/main/java/org/apache/maven/api/settings/ImmutableCollections.java index 850e0ef66..bdd5567b8 100644 --- a/api/maven-api-settings/src/main/java/org/apache/maven/api/settings/ImmutableCollections.java +++ b/api/maven-api-settings/src/main/java/org/apache/maven/api/settings/ImmutableCollections.java @@ -19,6 +19,7 @@ package org.apache.maven.api.settings; * under the License. */ +import java.io.Serializable; import java.util.AbstractList; import java.util.AbstractMap; import java.util.AbstractSet; @@ -251,7 +252,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableList<E> extends AbstractList<E> implements RandomAccess + private abstract static class AbstractImmutableList<E> + extends AbstractList<E> + implements RandomAccess, Serializable { @Override public boolean add( E e ) @@ -634,7 +637,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableMap<K, V> extends AbstractMap<K, V> + private abstract static class AbstractImmutableMap<K, V> + extends AbstractMap<K, V> + implements Serializable { @Override public void replaceAll( BiFunction<? super K, ? super V, ? extends V> function ) @@ -691,7 +696,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableSet<E> extends AbstractSet<E> + private abstract static class AbstractImmutableSet<E> + extends AbstractSet<E> + implements Serializable { @Override public boolean removeAll( Collection<?> c ) diff --git a/api/maven-api-toolchain/src/main/java/org/apache/maven/api/toolchain/ImmutableCollections.java b/api/maven-api-toolchain/src/main/java/org/apache/maven/api/toolchain/ImmutableCollections.java index 934a40331..8e95ea98c 100644 --- a/api/maven-api-toolchain/src/main/java/org/apache/maven/api/toolchain/ImmutableCollections.java +++ b/api/maven-api-toolchain/src/main/java/org/apache/maven/api/toolchain/ImmutableCollections.java @@ -19,6 +19,7 @@ package org.apache.maven.api.toolchain; * under the License. */ +import java.io.Serializable; import java.util.AbstractList; import java.util.AbstractMap; import java.util.AbstractSet; @@ -251,7 +252,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableList<E> extends AbstractList<E> implements RandomAccess + private abstract static class AbstractImmutableList<E> + extends AbstractList<E> + implements RandomAccess, Serializable { @Override public boolean add( E e ) @@ -634,7 +637,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableMap<K, V> extends AbstractMap<K, V> + private abstract static class AbstractImmutableMap<K, V> + extends AbstractMap<K, V> + implements Serializable { @Override public void replaceAll( BiFunction<? super K, ? super V, ? extends V> function ) @@ -691,7 +696,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableSet<E> extends AbstractSet<E> + private abstract static class AbstractImmutableSet<E> + extends AbstractSet<E> + implements Serializable { @Override public boolean removeAll( Collection<?> c ) diff --git a/maven-model/src/main/java/org/apache/maven/model/BaseObject.java b/maven-model/src/main/java/org/apache/maven/model/BaseObject.java index 69536fd4a..de1bd2df9 100644 --- a/maven-model/src/main/java/org/apache/maven/model/BaseObject.java +++ b/maven-model/src/main/java/org/apache/maven/model/BaseObject.java @@ -24,7 +24,8 @@ import java.io.Serializable; public abstract class BaseObject implements Serializable, Cloneable, InputLocationTracker { - protected ChildrenTracking childrenTracking; + protected transient ChildrenTracking childrenTracking; + protected Object delegate; public BaseObject() diff --git a/maven-model/src/test/java/org/apache/maven/model/SerializationTest.java b/maven-model/src/test/java/org/apache/maven/model/SerializationTest.java new file mode 100644 index 000000000..311b987e7 --- /dev/null +++ b/maven-model/src/test/java/org/apache/maven/model/SerializationTest.java @@ -0,0 +1,65 @@ +package org.apache.maven.model; + +/* + * 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. + */ + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class SerializationTest +{ + + @Test + public void testModelSerialization() + throws Exception + { + Model model; + try ( InputStream is = getClass().getResourceAsStream( "/xml/pom.xml" ) ) + { + model = new MavenXpp3Reader().read( is ); + } + + // Serialize an inner child here so that the BaseObject.childrenTracking is non null + Build build = model.getBuild(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try( ObjectOutputStream oos = new ObjectOutputStream( baos ) ) + { + oos.writeObject( build ); + } + + Build build2; + ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); + try( ObjectInputStream ois = new ObjectInputStream( bais ) ) + { + build2 = (Build) ois.readObject(); + } + + assertNotNull( build2 ); + } + +} diff --git a/maven-model/src/test/resources/xml/pom.xml b/maven-model/src/test/resources/xml/pom.xml new file mode 100644 index 000000000..01b679687 --- /dev/null +++ b/maven-model/src/test/resources/xml/pom.xml @@ -0,0 +1,123 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- +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. +--> + +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.maven</groupId> + <artifactId>maven</artifactId> + <version>4.0.0-alpha-1-SNAPSHOT</version> + </parent> + + <artifactId>maven-model</artifactId> + + <name>Maven Model</name> + <description>Model for Maven POM (Project Object Model)</description> + + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-api-model</artifactId> + <version>4.0.0-alpha-1-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-xml-impl</artifactId> + <version>4.0.0-alpha-1-SNAPSHOT</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <version>3.2.0</version> + <executions> + <execution> + <id>copy-model</id> + <phase>generate-sources</phase> + <goals> + <goal>copy</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>org.apache.maven</groupId> + <artifactId>maven-api-model</artifactId> + <version>4.0.0-alpha-1-SNAPSHOT</version> + <type>mdo</type> + <outputDirectory>target/mdo/</outputDirectory> + <destFileName>maven.mdo</destFileName> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven</groupId> + <artifactId>modello-plugin-velocity</artifactId> + <version>4.0.0-alpha-1-SNAPSHOT</version> + <executions> + <execution> + <id>velocity</id> + <phase>generate-sources</phase> + <goals> + <goal>velocity</goal> + </goals> + <configuration> + <version>4.0.0</version> + <models> + <model>target/mdo/maven.mdo</model> + </models> + <templates> + <template>src/main/mdo/model-v3.vm</template> + <template>src/main/mdo/merger.vm</template> + <template>src/main/mdo/transformer.vm</template> + <template>src/main/mdo/reader.vm</template> + <template>src/main/mdo/reader-ex.vm</template> + <template>src/main/mdo/writer.vm</template> + <template>src/main/mdo/writer-ex.vm</template> + </templates> + <params> + <param>packageModelV3=org.apache.maven.model</param> + <param>packageModelV4=org.apache.maven.api.model</param> + <param>packageToolV4=org.apache.maven.model.v4</param> + </params> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <configuration> + <excludes> + <exclude>**/package-info.java</exclude> + </excludes> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java b/maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java index 4479e4113..875b0b22f 100644 --- a/maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java +++ b/maven-plugin-api/src/main/java/org/apache/maven/plugin/lifecycle/ImmutableCollections.java @@ -19,6 +19,7 @@ package org.apache.maven.plugin.lifecycle; * under the License. */ +import java.io.Serializable; import java.util.AbstractList; import java.util.AbstractMap; import java.util.AbstractSet; @@ -251,7 +252,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableList<E> extends AbstractList<E> implements RandomAccess + private abstract static class AbstractImmutableList<E> + extends AbstractList<E> + implements RandomAccess, Serializable { @Override public boolean add( E e ) @@ -634,7 +637,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableMap<K, V> extends AbstractMap<K, V> + private abstract static class AbstractImmutableMap<K, V> + extends AbstractMap<K, V> + implements Serializable { @Override public void replaceAll( BiFunction<? super K, ? super V, ? extends V> function ) @@ -691,7 +696,9 @@ class ImmutableCollections } } - private abstract static class AbstractImmutableSet<E> extends AbstractSet<E> + private abstract static class AbstractImmutableSet<E> + extends AbstractSet<E> + implements Serializable { @Override public boolean removeAll( Collection<?> c )
