From a27873629f94174bbcaa345c81abb204e4357ce5 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 1 Mar 2024 17:30:18 +0100 Subject: [PATCH] [MNG-8024] Make WrapperProperties and WrapperList serializable (#1433) --- .../org/apache/maven/model/SerializationTest.java | 14 ++++++++++++++ src/mdo/java/WrapperList.java | 8 +++++++- src/mdo/java/WrapperProperties.java | 7 +++++++ 3 files changed, 28 insertions(+), 1 deletion(-) 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 index 91daad714580..b2cfd60acc56 100644 --- a/maven-model/src/test/java/org/apache/maven/model/SerializationTest.java +++ b/maven-model/src/test/java/org/apache/maven/model/SerializationTest.java @@ -54,4 +54,18 @@ void testModelSerialization() throws Exception { assertNotNull(build2); } + + @Test + void testModelPropertiesAndListSerialization() throws Exception { + Model model; + try (InputStream is = getClass().getResourceAsStream("/xml/pom.xml")) { + model = new MavenXpp3Reader().read(is); + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { + oos.writeObject(model.getProperties()); + oos.writeObject(model.getBuild().getPlugins()); + } + } } diff --git a/src/mdo/java/WrapperList.java b/src/mdo/java/WrapperList.java index 0799336aeff5..8de3ef32bdcc 100644 --- a/src/mdo/java/WrapperList.java +++ b/src/mdo/java/WrapperList.java @@ -18,6 +18,8 @@ */ package ${package}; +import java.io.ObjectStreamException; +import java.io.Serializable; import java.util.AbstractList; import java.util.ArrayList; import java.util.List; @@ -26,7 +28,7 @@ import java.util.function.Function; import java.util.function.Supplier; -class WrapperList extends AbstractList { +class WrapperList extends AbstractList implements Serializable { private final Supplier> getter; private final Consumer> setter; private final Function mapper; @@ -102,4 +104,8 @@ public T remove(int index) { return mapper.apply(getter.get().remove(index)); } } + + private Object writeReplace() throws ObjectStreamException { + return new ArrayList(this); + } } diff --git a/src/mdo/java/WrapperProperties.java b/src/mdo/java/WrapperProperties.java index 46d7658365a4..851459c4a6b5 100644 --- a/src/mdo/java/WrapperProperties.java +++ b/src/mdo/java/WrapperProperties.java @@ -330,4 +330,11 @@ public void storeToXML(OutputStream os, String comment, String encoding) throws props.putAll(getter.get()); props.storeToXML(os, comment, encoding); } + + + private Object writeReplace() throws java.io.ObjectStreamException { + Properties props = new Properties(); + props.putAll(getter.get()); + return props; + } }