This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new fc237e180 [fix] bugfix: use the same class load of object need to be 
copy in `CodeGeneratorContext` (#1511)
fc237e180 is described below

commit fc237e18078c0765872d6561bf704433ca38549d
Author: YeJunHao <[email protected]>
AuthorDate: Tue Jul 11 16:05:28 2023 +0800

    [fix] bugfix: use the same class load of object need to be copy in 
`CodeGeneratorContext` (#1511)
---
 .../paimon/codegen/CodeGeneratorContext.scala      | 11 ++----
 .../paimon/codegen/CodeGeneratorContextTest.java   | 46 ++++++++++++++++++++++
 2 files changed, 50 insertions(+), 7 deletions(-)

diff --git 
a/paimon-codegen/src/main/scala/org/apache/paimon/codegen/CodeGeneratorContext.scala
 
b/paimon-codegen/src/main/scala/org/apache/paimon/codegen/CodeGeneratorContext.scala
index 6e25a4cb6..549e5c725 100644
--- 
a/paimon-codegen/src/main/scala/org/apache/paimon/codegen/CodeGeneratorContext.scala
+++ 
b/paimon-codegen/src/main/scala/org/apache/paimon/codegen/CodeGeneratorContext.scala
@@ -17,6 +17,7 @@
  */
 package org.apache.paimon.codegen
 
+import org.apache.paimon.annotation.VisibleForTesting
 import org.apache.paimon.codegen.GenerateUtils.{newName, newNames}
 import org.apache.paimon.data.serializer.InternalSerializers
 import org.apache.paimon.types.DataType
@@ -30,8 +31,6 @@ import scala.collection.mutable
  */
 class CodeGeneratorContext {
 
-  val classLoader: ClassLoader = Thread.currentThread().getContextClassLoader
-
   // holding a list of objects that could be used passed into generated class
   val references: mutable.ArrayBuffer[AnyRef] = new 
mutable.ArrayBuffer[AnyRef]()
 
@@ -148,15 +147,13 @@ class CodeGeneratorContext {
     fieldTerm
   }
 
-  private def addReusableObjectInternal(
-      obj: AnyRef,
-      fieldTerm: String,
-      fieldTypeTerm: String): Unit = {
+  @VisibleForTesting
+  def addReusableObjectInternal(obj: AnyRef, fieldTerm: String, fieldTypeTerm: 
String): Unit = {
     val idx = references.length
     // make a deep copy of the object
     val byteArray = InstantiationUtil.serializeObject(obj)
     val objCopy: AnyRef =
-      InstantiationUtil.deserializeObject(byteArray, classLoader)
+      InstantiationUtil.deserializeObject(byteArray, 
obj.getClass.getClassLoader)
     references += objCopy
 
     reusableMemberStatements.add(s"private transient $fieldTypeTerm 
$fieldTerm;")
diff --git 
a/paimon-codegen/src/test/java/org/apache/paimon/codegen/CodeGeneratorContextTest.java
 
b/paimon-codegen/src/test/java/org/apache/paimon/codegen/CodeGeneratorContextTest.java
new file mode 100644
index 000000000..529df923b
--- /dev/null
+++ 
b/paimon-codegen/src/test/java/org/apache/paimon/codegen/CodeGeneratorContextTest.java
@@ -0,0 +1,46 @@
+/*
+ * 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.paimon.codegen;
+
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.Serializable;
+
+/** Tests for {@link CodeGeneratorContext}. */
+public class CodeGeneratorContextTest {
+
+    @Test
+    public void testAddReusableObjectInternal() {
+        Object o = new TestClass();
+        Thread.currentThread()
+                
.setContextClassLoader(ClassLoader.getSystemClassLoader().getParent());
+
+        Assertions.assertThatCode(
+                        () ->
+                                new CodeGeneratorContext()
+                                        .addReusableObjectInternal(o, 
"careless", "careless"))
+                .doesNotThrowAnyException();
+    }
+
+    /**
+     * This class is only for test, loaded by app classloader, and will not be 
found by its parent.
+     */
+    public static class TestClass implements Serializable {}
+}

Reply via email to