Skip to content

Allow object serialization on DefaultSdkAutoConstructList and Default… #976

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

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-bd31809.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java v2",
"type": "bugfix",
"description": "Allow object serialization on DefaultSdkAutoConstructList and DefaultSdkAutoConstructMap"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

package software.amazon.awssdk.core.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import software.amazon.awssdk.annotations.SdkProtectedApi;

/**
Expand All @@ -30,8 +32,10 @@
* @param <T> The element type.
*/
@SdkProtectedApi
public final class DefaultSdkAutoConstructList<T> implements SdkAutoConstructList<T> {
public final class DefaultSdkAutoConstructList<T> implements SdkAutoConstructList<T>, Serializable {

private static final DefaultSdkAutoConstructList INSTANCE = new DefaultSdkAutoConstructList();
private static final long serialVersionUID = -7448720100757704309L;

private final List impl = Collections.unmodifiableList(Collections.emptyList());

Expand Down Expand Up @@ -158,4 +162,8 @@ public List<T> subList(int fromIndex, int toIndex) {
return impl.subList(fromIndex, toIndex);
}

// For deserialization
private Object readResolve() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give us more context of why this is needed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a serialization trick to ensure that we use the singleton instance (INSTANCE on line 37) when we deserialize this class's object. (Usually, deserialization constructs a new object without readResolve())

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zoewangg ping

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay. Hmm, I'm not sure if we want to add this. This seems a bit hacky to me.

return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.awssdk.core.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand All @@ -31,8 +32,11 @@
* @param <V> The value type.
*/
@SdkProtectedApi
public final class DefaultSdkAutoConstructMap<K, V> implements SdkAutoConstructMap<K, V> {
public final class DefaultSdkAutoConstructMap<K, V>
implements SdkAutoConstructMap<K, V>, Serializable {

private static final DefaultSdkAutoConstructMap INSTANCE = new DefaultSdkAutoConstructMap();
private static final long serialVersionUID = 5085388072643164984L;

private final Map<K, V> impl = Collections.unmodifiableMap(Collections.emptyMap());

Expand Down Expand Up @@ -103,4 +107,9 @@ public Collection<V> values() {
public Set<Entry<K, V>> entrySet() {
return impl.entrySet();
}

// For deserialization
private Object readResolve() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package software.amazon.awssdk.core.util;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test files are missing copyright header. You can copy paste the copyright header from other file in this repo


import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class DefaultSdkAutoConstructListTest {

@Test
public void testSerialization() throws Exception {
DefaultSdkAutoConstructList<Object> original = DefaultSdkAutoConstructList.getInstance();
byte[] originalBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)){
oos.writeObject(original);
originalBytes = baos.toByteArray();
}
DefaultSdkAutoConstructList<Object> deserializedObject;
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(originalBytes))) {
deserializedObject = (DefaultSdkAutoConstructList<Object>) ois.readObject();
}
assertEquals(original, deserializedObject);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package software.amazon.awssdk.core.util;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;

public class DefaultSdkAutoConstructMapTest {

@Test
public void testSerialization() throws Exception {
DefaultSdkAutoConstructMap<Object, Object> original =
DefaultSdkAutoConstructMap.getInstance();
byte[] originalBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos)){
oos.writeObject(original);
originalBytes = baos.toByteArray();
}
DefaultSdkAutoConstructMap<Object, Object> deserializedObject;
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(originalBytes))) {
deserializedObject = (DefaultSdkAutoConstructMap<Object, Object>) ois.readObject();
}
assertEquals(original, deserializedObject);
}
}