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 6ba816e41582f7c42ff633c2129c2f75f3e0ee88
Author: Shawn Yang <shawn.ck.y...@gmail.com>
AuthorDate: Sat May 10 06:45:30 2025 +0800

    fix(java): fix field super class missing in compatible mode (#2214)
    
    <!--
    **Thanks for contributing to Fury.**
    
    **If this is your first time opening a PR on fury, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fury (incubating)** community has restrictions on the
    naming of pr titles. You can also find instructions in
    [CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).
    
    - Fury has a strong focus on performance. If the PR you submit will have
    an impact on performance, please benchmark it first and provide the
    benchmark result here.
    -->
    
    <!-- Describe the purpose of this PR. -->
    
    Closes #2210
    
    <!--
    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?
    
    <!--
    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.
    -->
---
 .../java/org/apache/fury/meta/ClassDefEncoder.java |  6 +--
 .../fury/serializer/MetaSharedCompatibleTest.java  | 61 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 3 deletions(-)

diff --git 
a/java/fury-core/src/main/java/org/apache/fury/meta/ClassDefEncoder.java 
b/java/fury-core/src/main/java/org/apache/fury/meta/ClassDefEncoder.java
index 9a592cd2..f2907515 100644
--- a/java/fury-core/src/main/java/org/apache/fury/meta/ClassDefEncoder.java
+++ b/java/fury-core/src/main/java/org/apache/fury/meta/ClassDefEncoder.java
@@ -137,17 +137,17 @@ class ClassDefEncoder {
     MemoryBuffer classDefBuf = MemoryBuffer.newHeapBuffer(128);
     for (Map.Entry<String, List<FieldInfo>> entry : classLayers.entrySet()) {
       String className = entry.getKey();
+      Class<?> currentType = getType(type, className);
       List<FieldInfo> fields = entry.getValue();
       // | num fields + register flag | header + package name | header + class 
name
       // | header + type id + field name | next field info | ... |
       int currentClassHeader = (fields.size() << 1);
-      if (classResolver.isRegistered(type)) {
+      if (classResolver.isRegistered(currentType)) {
         currentClassHeader |= 1;
         classDefBuf.writeVarUint32Small7(currentClassHeader);
-        
classDefBuf.writeVarUint32Small7(classResolver.getRegisteredClassId(type));
+        
classDefBuf.writeVarUint32Small7(classResolver.getRegisteredClassId(currentType));
       } else {
         classDefBuf.writeVarUint32Small7(currentClassHeader);
-        Class<?> currentType = getType(type, className);
         Tuple2<String, String> encoded = 
Encoders.encodePkgAndClass(currentType);
         writePkgName(classDefBuf, encoded.f0);
         writeTypeName(classDefBuf, encoded.f1);
diff --git 
a/java/fury-core/src/test/java/org/apache/fury/serializer/MetaSharedCompatibleTest.java
 
b/java/fury-core/src/test/java/org/apache/fury/serializer/MetaSharedCompatibleTest.java
index 8a4eb683..57e0edc0 100644
--- 
a/java/fury-core/src/test/java/org/apache/fury/serializer/MetaSharedCompatibleTest.java
+++ 
b/java/fury-core/src/test/java/org/apache/fury/serializer/MetaSharedCompatibleTest.java
@@ -31,6 +31,8 @@ import java.util.stream.Collectors;
 import org.apache.fury.Fury;
 import org.apache.fury.FuryTestBase;
 import org.apache.fury.builder.MetaSharedCodecBuilder;
+import org.apache.fury.codegen.CompileUnit;
+import org.apache.fury.codegen.JaninoUtils;
 import org.apache.fury.config.CompatibleMode;
 import org.apache.fury.config.FuryBuilder;
 import org.apache.fury.config.Language;
@@ -673,4 +675,63 @@ public class MetaSharedCompatibleTest extends FuryTestBase 
{
             .InnerClassTestLengthInnerClassTestLengthInnerClassTestLength();
     serDeCheck(fury, o);
   }
+
+  @Test
+  public void testInheritance() throws Exception {
+    // See issue https://github.com/apache/fury/issues/2210
+    CompileUnit u1 =
+        new CompileUnit(
+            "example.test",
+            "Parent",
+            (""
+                + "package example.test;\n"
+                + "public class Parent {\n"
+                + "  public Integer f1;\n"
+                + "  public Integer f2;\n"
+                + "}"));
+    CompileUnit u2 =
+        new CompileUnit(
+            "example.test",
+            "Child",
+            (""
+                + "package example.test;\n"
+                + "public class Child extends Parent {\n"
+                + "  public Integer f3;\n"
+                + "}"));
+    ClassLoader classLoader1 =
+        JaninoUtils.compile(Thread.currentThread().getContextClassLoader(), 
u1, u2);
+    Object o = classLoader1.loadClass("example.test.Child").newInstance();
+    ReflectionUtils.setObjectFieldValue(o, "f1", 10);
+    ReflectionUtils.setObjectFieldValue(o, "f2", 20);
+    ReflectionUtils.setObjectFieldValue(o, "f3", 30);
+    Fury fury =
+        builder()
+            .withCompatibleMode(CompatibleMode.COMPATIBLE)
+            .withClassLoader(classLoader1)
+            .build();
+    fury.register(classLoader1.loadClass("example.test.Child"));
+    fury.register(classLoader1.loadClass("example.test.Parent"));
+    byte[] serialized = fury.serialize(o);
+    CompileUnit u11 =
+        new CompileUnit(
+            "example.test",
+            "Parent",
+            (""
+                + "package example.test;\n"
+                + "public class Parent {\n"
+                + "  public Integer f2;\n"
+                + "}"));
+    ClassLoader classLoader2 = 
JaninoUtils.compile(getClass().getClassLoader(), u11, u2);
+    Fury fury2 =
+        builder()
+            .withCompatibleMode(CompatibleMode.COMPATIBLE)
+            .withClassLoader(classLoader2)
+            .build();
+    fury2.register(classLoader2.loadClass("example.test.Child"));
+    fury2.register(classLoader2.loadClass("example.test.Parent"));
+    Object o1 = fury2.deserialize(serialized);
+    Assert.assertNull(ReflectionUtils.getObjectFieldValue(o1, "f1"));
+    Assert.assertEquals(ReflectionUtils.getObjectFieldValue(o1, "f2"), 20);
+    Assert.assertEquals(ReflectionUtils.getObjectFieldValue(o1, "f3"), 30);
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@fury.apache.org
For additional commands, e-mail: commits-h...@fury.apache.org

Reply via email to