beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tg...@apache.org
Subject [2/2] beam git commit: Enable SerializableCoder to Serialize with Generic Types
Date Fri, 12 May 2017 00:56:08 GMT
Enable SerializableCoder to Serialize with Generic Types

A TypeToken that contains generics is not serializable. However, the
TypeDescriptor does not need to be transmitted via the serialized form,
so mark it as transient.


Project: http://git-wip-us.apache.org/repos/asf/beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/beam/commit/c39c9aec
Tree: http://git-wip-us.apache.org/repos/asf/beam/tree/c39c9aec
Diff: http://git-wip-us.apache.org/repos/asf/beam/diff/c39c9aec

Branch: refs/heads/master
Commit: c39c9aecf859095bfde87267e7eb9d9a4fd1682d
Parents: fc77ca7
Author: Thomas Groh <tgroh@google.com>
Authored: Thu May 11 17:16:53 2017 -0700
Committer: Thomas Groh <tgroh@google.com>
Committed: Thu May 11 17:55:50 2017 -0700

----------------------------------------------------------------------
 .../src/main/resources/beam/findbugs-filter.xml           | 10 ++++++++++
 .../org/apache/beam/sdk/coders/SerializableCoder.java     |  5 ++++-
 .../org/apache/beam/sdk/coders/SerializableCoderTest.java |  8 +++++++-
 3 files changed, 21 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/beam/blob/c39c9aec/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
----------------------------------------------------------------------
diff --git a/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml b/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
index 1db0e86..8ff0cb0 100644
--- a/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
+++ b/sdks/java/build-tools/src/main/resources/beam/findbugs-filter.xml
@@ -74,6 +74,16 @@
   </Match>
 
   <Match>
+    <Class name="org.apache.beam.sdk.coders.SerializableCoder"/>
+    <Field name="typeDescriptor"/>
+    <Bug pattern="SE_TRANSIENT_FIELD_NOT_RESTORED"/>
+    <!--
+    the field is used only in getEncodedTypeDescriptor, where it is restored if it is not
present due to
+    serialization
+    -->
+  </Match>
+
+  <Match>
     <Class name="org.apache.beam.sdk.io.jms.JmsRecord"/>
     <Field name="jmsDestination"/>
     <Bug pattern="SE_BAD_FIELD"/>

http://git-wip-us.apache.org/repos/asf/beam/blob/c39c9aec/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SerializableCoder.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SerializableCoder.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SerializableCoder.java
index 9aa8493..6691876 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SerializableCoder.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SerializableCoder.java
@@ -107,7 +107,7 @@ public class SerializableCoder<T extends Serializable> extends CustomCoder<T>
{
   }
 
   private final Class<T> type;
-  private final TypeDescriptor<T> typeDescriptor;
+  private transient TypeDescriptor<T> typeDescriptor;
 
   protected SerializableCoder(Class<T> type, TypeDescriptor<T> typeDescriptor)
{
     this.type = type;
@@ -166,6 +166,9 @@ public class SerializableCoder<T extends Serializable> extends CustomCoder<T>
{
 
   @Override
   public TypeDescriptor<T> getEncodedTypeDescriptor() {
+    if (typeDescriptor == null) {
+      typeDescriptor = TypeDescriptor.of(type);
+    }
     return typeDescriptor;
   }
 

http://git-wip-us.apache.org/repos/asf/beam/blob/c39c9aec/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/SerializableCoderTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/SerializableCoderTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/SerializableCoderTest.java
index adb6652..dd4f6ca 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/SerializableCoderTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/SerializableCoderTest.java
@@ -125,12 +125,18 @@ public class SerializableCoderTest implements Serializable {
     assertEquals(coder.getRecordType(), MyRecord.class);
     CoderProperties.coderSerializable(coder);
 
-
     SerializableCoder<?> decoded = SerializableUtils.clone(coder);
     assertThat(decoded.getRecordType(), Matchers.<Object>equalTo(MyRecord.class));
   }
 
   @Test
+  public <T extends Serializable> void testSerializableCoderIsSerializableWithGenericTypeToken()
+  throws Exception {
+    SerializableCoder<T> coder = SerializableCoder.of(new TypeDescriptor<T>()
{});
+    CoderProperties.coderSerializable(coder);
+  }
+
+  @Test
   public void testNullEquals() {
     SerializableCoder<MyRecord> coder = SerializableCoder.of(MyRecord.class);
     Assert.assertFalse(coder.equals(null));


Mime
View raw message