You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tr...@apache.org on 2018/07/04 09:59:27 UTC

[1/2] flink git commit: [FLINK-9654] Add InstantiationUtilTests for Scala types to flink-scala

Repository: flink
Updated Branches:
  refs/heads/master 579807d70 -> 82e6f7377


[FLINK-9654] Add InstantiationUtilTests for Scala types to flink-scala

This closes #6206.


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

Branch: refs/heads/master
Commit: 82e6f737787314aa02a08aa2f45bed2989d71c43
Parents: 7ca0fd7
Author: Till Rohrmann <tr...@apache.org>
Authored: Mon Jul 2 15:58:24 2018 +0200
Committer: Till Rohrmann <tr...@apache.org>
Committed: Wed Jul 4 11:57:01 2018 +0200

----------------------------------------------------------------------
 .../scala/typeutils/InstantiationUtilTest.scala | 94 ++++++++++++++++++++
 1 file changed, 94 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/82e6f737/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/InstantiationUtilTest.scala
----------------------------------------------------------------------
diff --git a/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/InstantiationUtilTest.scala b/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/InstantiationUtilTest.scala
new file mode 100644
index 0000000..b92abdc
--- /dev/null
+++ b/flink-scala/src/test/scala/org/apache/flink/api/scala/typeutils/InstantiationUtilTest.scala
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.api.scala.typeutils
+
+import java.io.ByteArrayOutputStream
+
+import org.apache.flink.util.{InstantiationUtil, TestLogger}
+import org.hamcrest.Matchers
+import org.junit.{Assert, Test}
+
+/**
+  * Serialization/Deserialization tests of Scala types using the
+  * [[org.apache.flink.util.InstantiationUtil]].
+  */
+class InstantiationUtilTest extends TestLogger {
+
+  @Test
+  def testNestedScalaTypeSerDe(): Unit = {
+    val instance = new Foo.Bar.Foobar(42)
+
+    val copy = serializeDeserializeInstance(instance)
+
+    Assert.assertThat(copy, Matchers.equalTo(instance))
+  }
+
+  @Test
+  def testAnonymousScalaTypeSerDe(): Unit = {
+    val instance = new Foo.FooTrait {
+      val value: Int = 41
+
+      override def hashCode(): Int = 37 * value
+
+      override def equals(obj: scala.Any): Boolean = {
+        obj match {
+          case x: Foo.FooTrait => value == x.value()
+          case _ => false
+        }
+      }
+    }
+
+    val copy = serializeDeserializeInstance(instance)
+
+    Assert.assertThat(copy, Matchers.equalTo(instance))
+  }
+
+  private def serializeDeserializeInstance[T](instance: T): T = {
+    val baos = new ByteArrayOutputStream()
+
+    InstantiationUtil.serializeObject(baos, instance)
+
+    InstantiationUtil.deserializeObject(
+      baos.toByteArray,
+      getClass.getClassLoader,
+      true)
+  }
+}
+
+object Foo extends Serializable {
+  trait FooTrait extends Serializable {
+    def value(): Int
+  }
+
+  object Bar extends Serializable {
+    class Foobar(val x: Int) extends Serializable {
+      override def hashCode(): Int = 37 * x
+
+      override def equals(obj: scala.Any): Boolean = {
+        obj match {
+          case other: Foobar =>
+            other.x == x
+          case _ =>
+            false
+        }
+      }
+    }
+
+  }
+}


[2/2] flink git commit: [FLINK-9654] Changed the way we check if a class is anonymous to avoid SI-2034.

Posted by tr...@apache.org.
[FLINK-9654] Changed the way we check if a class is anonymous to avoid SI-2034.


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

Branch: refs/heads/master
Commit: 7ca0fd783eb2ec01a0011eb891db75d998d635f4
Parents: 579807d
Author: Zsolt Donca <zs...@8x8.com>
Authored: Mon Jun 25 10:43:29 2018 +0300
Committer: Till Rohrmann <tr...@apache.org>
Committed: Wed Jul 4 11:57:01 2018 +0200

----------------------------------------------------------------------
 .../apache/flink/util/InstantiationUtil.java    | 22 ++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flink/blob/7ca0fd78/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
----------------------------------------------------------------------
diff --git a/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java b/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
index 3db0236..2370c7c 100644
--- a/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
+++ b/flink-core/src/main/java/org/apache/flink/util/InstantiationUtil.java
@@ -208,10 +208,7 @@ public final class InstantiationUtil {
 
 			final Class localClass = resolveClass(streamClassDescriptor);
 			final String name = localClass.getName();
-			if (scalaSerializerClassnames.contains(name) || scalaTypes.contains(name) || localClass.isAnonymousClass()
-				// isAnonymousClass does not work for anonymous Scala classes; additionally check by classname
-				|| name.contains("$anon$") || name.contains("$anonfun")) {
-
+			if (scalaSerializerClassnames.contains(name) || scalaTypes.contains(name) || isAnonymousClass(localClass)) {
 				final ObjectStreamClass localClassDescriptor = ObjectStreamClass.lookup(localClass);
 				if (localClassDescriptor != null
 					&& localClassDescriptor.getSerialVersionUID() != streamClassDescriptor.getSerialVersionUID()) {
@@ -226,6 +223,23 @@ public final class InstantiationUtil {
 		}
 	}
 
+	private static boolean isAnonymousClass(Class clazz) {
+		final String name = clazz.getName();
+
+		// isAnonymousClass does not work for anonymous Scala classes; additionally check by class name
+		if (name.contains("$anon$") || name.contains("$anonfun") || name.contains("$macro$")) {
+			return true;
+		}
+
+		// calling isAnonymousClass or getSimpleName can throw InternalError for certain Scala types, see https://issues.scala-lang.org/browse/SI-2034
+		// until we move to JDK 9, this try-catch is necessary
+		try {
+			return clazz.isAnonymousClass();
+		} catch (InternalError e) {
+			return false;
+		}
+	}
+
 	/**
 	 * A mapping between the full path of a deprecated serializer and its equivalent.
 	 * These mappings are hardcoded and fixed.