Skip to content

Commit

Permalink
[MNG-8024] Make WrapperProperties and WrapperList serializable (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored Mar 1, 2024
1 parent a3e8da8 commit a278736
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
8 changes: 7 additions & 1 deletion src/mdo/java/WrapperList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +28,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

class WrapperList<T, U> extends AbstractList<T> {
class WrapperList<T, U> extends AbstractList<T> implements Serializable {
private final Supplier<List<U>> getter;
private final Consumer<List<U>> setter;
private final Function<U, T> mapper;
Expand Down Expand Up @@ -102,4 +104,8 @@ public T remove(int index) {
return mapper.apply(getter.get().remove(index));
}
}

private Object writeReplace() throws ObjectStreamException {
return new ArrayList<T>(this);
}
}
7 changes: 7 additions & 0 deletions src/mdo/java/WrapperProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit a278736

Please sign in to comment.