Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8024] Make WrapperProperties and WrapperList serializable #1433

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}