Make `ArrayCodec` generic.
A codec for `Object[]` is still automatically registered. Custom array types have to be registered manually.
I am working on a change to store outputs as `Artifact[]` instead of `ImmutableSet<Artifact>` and this will be necessary.
PiperOrigin-RevId: 523487752
Change-Id: I3ac1b7d88e7d52719381f210380605f3f61f219d
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodec.java b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodec.java
index 3668435..abc7637 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodec.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodec.java
@@ -17,16 +17,40 @@
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import java.io.IOException;
+import java.lang.reflect.Array;
-/** {@link ObjectCodec} for {@link Object[]}. */
-class ArrayCodec implements ObjectCodec<Object[]> {
- @Override
- public Class<Object[]> getEncodedClass() {
- return Object[].class;
+/** {@link ObjectCodec} for arrays of an arbitrary component type. */
+public class ArrayCodec<T> implements ObjectCodec<T[]> {
+
+ /** Creates a codec for arrays of the given component type. */
+ public static <T> ArrayCodec<T> forComponentType(Class<T> componentType) {
+ @SuppressWarnings("unchecked")
+ Class<T[]> arrayType = (Class<T[]>) Array.newInstance(componentType, 0).getClass();
+ return new ArrayCodec<>(componentType, arrayType);
+ }
+
+ /** Codec for {@code Object[]}. */
+ static final class ObjectArrayCodec extends ArrayCodec<Object> {
+ ObjectArrayCodec() {
+ super(Object.class, Object[].class);
+ }
+ }
+
+ private final Class<T> componentType;
+ private final Class<T[]> arrayType;
+
+ private ArrayCodec(Class<T> componentType, Class<T[]> arrayType) {
+ this.componentType = componentType;
+ this.arrayType = arrayType;
}
@Override
- public void serialize(SerializationContext context, Object[] obj, CodedOutputStream codedOut)
+ public final Class<T[]> getEncodedClass() {
+ return arrayType;
+ }
+
+ @Override
+ public final void serialize(SerializationContext context, T[] obj, CodedOutputStream codedOut)
throws SerializationException, IOException {
codedOut.writeInt32NoTag(obj.length);
try {
@@ -40,9 +64,10 @@
}
@Override
- public Object[] deserialize(DeserializationContext context, CodedInputStream codedIn)
+ public final T[] deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
- Object[] result = new Object[codedIn.readInt32()];
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(componentType, codedIn.readInt32());
try {
for (int i = 0; i < result.length; i++) {
result[i] = context.deserialize(codedIn);
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodecTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodecTest.java
index 37b699a..a68b049 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodecTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/ArrayCodecTest.java
@@ -22,15 +22,17 @@
import com.google.devtools.build.lib.skyframe.serialization.testutils.TestUtils;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
+import java.math.BigInteger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Tests for {@link ArrayCodec}. */
@RunWith(JUnit4.class)
-public class ArrayCodecTest {
+public final class ArrayCodecTest {
+
@Test
- public void smoke() throws Exception {
+ public void objectArray() throws Exception {
Object[] instance = new Object[2];
instance[0] = "hi";
Object[] inner = new Object[2];
@@ -43,6 +45,16 @@
}
@Test
+ public void typedArray() throws Exception {
+ new SerializationTester(
+ new BigInteger[] {},
+ new BigInteger[] {BigInteger.ZERO},
+ new BigInteger[] {BigInteger.ZERO, BigInteger.ONE, BigInteger.TWO})
+ .addCodec(ArrayCodec.forComponentType(BigInteger.class))
+ .runTests();
+ }
+
+ @Test
public void stackOverflowTransformedIntoSerializationException() {
class Foo {}