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

chaokunyang pushed a commit to branch releases-0.10
in repository https://gitbox.apache.org/repos/asf/fury.git

commit 6d928190062518c782f650b04b2cc42958c83d40
Author: zhaommmmomo <[email protected]>
AuthorDate: Tue Nov 5 00:25:50 2024 +0800

    feat(java): ReplaceResolveSerializer deep copy (#1925)
    
    
    
    ## What does this PR do?
    Adjusting the deep copy of ReplaceResolveSerializer.
    
    obj -> writeReplace -> copy -> readResolve -> newObj
    
    <!-- Describe the purpose of this PR. -->
    
    ## Related issues
    https://github.com/apache/fury/issues/1849
    <!--
    Is there any related issue? Please attach here.
    
    - #xxxx0
    - #xxxx1
    - #xxxx2
    -->
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fury/issues/new/choose) describing the
    need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../fury/serializer/ReplaceResolveSerializer.java  | 62 ++++++++++++++++------
 .../serializer/ReplaceResolveSerializerTest.java   |  2 -
 2 files changed, 45 insertions(+), 19 deletions(-)

diff --git 
a/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java
 
b/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java
index e958669e..48667e7d 100644
--- 
a/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java
+++ 
b/java/fury-core/src/main/java/org/apache/fury/serializer/ReplaceResolveSerializer.java
@@ -44,7 +44,7 @@ import org.apache.fury.util.unsafe._JDKAccess;
  * will skip classname writing if object returned by `writeReplace` is 
different from current class.
  */
 @SuppressWarnings({"unchecked", "rawtypes"})
-public class ReplaceResolveSerializer extends AbstractObjectSerializer {
+public class ReplaceResolveSerializer extends Serializer {
   private static final Logger LOG = 
LoggerFactory.getLogger(ReplaceResolveSerializer.class);
 
   /**
@@ -139,6 +139,19 @@ public class ReplaceResolveSerializer extends 
AbstractObjectSerializer {
         }
       }
     }
+
+    Object readResolve(Object o) {
+      if (readResolveFunc != null) {
+        return readResolveFunc.apply(o);
+      } else {
+        try {
+          return readResolveMethod.invoke(o);
+        } catch (Exception e) {
+          Platform.throwException(e);
+          throw new IllegalStateException(e);
+        }
+      }
+    }
   }
 
   private static final ClassValue<ReplaceResolveInfo> 
REPLACE_RESOLVE_INFO_CACHE =
@@ -308,26 +321,41 @@ public class ReplaceResolveSerializer extends 
AbstractObjectSerializer {
 
   private Object readObject(MemoryBuffer buffer) {
     Class cls = classResolver.readClassInternal(buffer);
+    MethodInfoCache jdkMethodInfoCache = getMethodInfoCache(cls);
+    Object o = jdkMethodInfoCache.objectSerializer.read(buffer);
+    ReplaceResolveInfo replaceResolveInfo = jdkMethodInfoCache.info;
+    if (replaceResolveInfo.readResolveMethod == null) {
+      return o;
+    }
+    return replaceResolveInfo.readResolve(o);
+  }
+
+  @Override
+  public Object copy(Object originObj) {
+    ReplaceResolveInfo replaceResolveInfo = jdkMethodInfoWriteCache.info;
+    if (replaceResolveInfo.writeReplaceMethod == null) {
+      return jdkMethodInfoWriteCache.objectSerializer.copy(originObj);
+    }
+    Object newObj = originObj;
+    newObj = replaceResolveInfo.writeReplace(newObj);
+    if (needToCopyRef) {
+      fury.reference(originObj, newObj);
+    }
+    MethodInfoCache jdkMethodInfoCache = getMethodInfoCache(newObj.getClass());
+    newObj = jdkMethodInfoCache.objectSerializer.copy(newObj);
+    replaceResolveInfo = jdkMethodInfoCache.info;
+    if (replaceResolveInfo.readResolveMethod != null) {
+      newObj = replaceResolveInfo.readResolve(newObj);
+    }
+    return newObj;
+  }
+
+  private MethodInfoCache getMethodInfoCache(Class<?> cls) {
     MethodInfoCache jdkMethodInfoCache = classClassInfoHolderMap.get(cls);
     if (jdkMethodInfoCache == null) {
       jdkMethodInfoCache = newJDKMethodInfoCache(cls, fury);
       classClassInfoHolderMap.put(cls, jdkMethodInfoCache);
     }
-    Object o = jdkMethodInfoCache.objectSerializer.read(buffer);
-    ReplaceResolveInfo replaceResolveInfo = jdkMethodInfoCache.info;
-    Method readResolveMethod = replaceResolveInfo.readResolveMethod;
-    if (readResolveMethod != null) {
-      if (replaceResolveInfo.readResolveFunc != null) {
-        return replaceResolveInfo.readResolveFunc.apply(o);
-      }
-      try {
-        return readResolveMethod.invoke(o);
-      } catch (Exception e) {
-        Platform.throwException(e);
-        throw new IllegalStateException("unreachable");
-      }
-    } else {
-      return o;
-    }
+    return jdkMethodInfoCache;
   }
 }
diff --git 
a/java/fury-core/src/test/java/org/apache/fury/serializer/ReplaceResolveSerializerTest.java
 
b/java/fury-core/src/test/java/org/apache/fury/serializer/ReplaceResolveSerializerTest.java
index c0d2f782..5e1af25c 100644
--- 
a/java/fury-core/src/test/java/org/apache/fury/serializer/ReplaceResolveSerializerTest.java
+++ 
b/java/fury-core/src/test/java/org/apache/fury/serializer/ReplaceResolveSerializerTest.java
@@ -183,8 +183,6 @@ public class ReplaceResolveSerializerTest extends 
FuryTestBase {
       fury.registerSerializer(o.getClass(), ReplaceResolveSerializer.class);
       copyCheck(fury, o);
     }
-    CustomReplaceClass2 o = new CustomReplaceClass2(false, 6);
-    copyCheck(fury, o);
   }
 
   public static class CustomReplaceClass3 implements Serializable {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to