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

pandalee pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 541f11e3 fix(java): fix field super class missing in compatible mode 
(#2214)
541f11e3 is described below

commit 541f11e3afa58dd956fc679a6ef266d6aa57fd30
Author: Shawn Yang <[email protected]>
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.
    -->
    
    ## What does this PR do?
    
    <!-- Describe the purpose of this PR. -->
    
    ## Related issues
    
    Closes #2210
    
    ## 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.
    -->
---
 .../java/org/apache/fury/meta/ClassDefEncoder.java | 10 ++--
 .../fury/serializer/MetaSharedCompatibleTest.java  | 59 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 5 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 a6b2fa91..9478adc3 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
@@ -131,20 +131,20 @@ 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.isRegisteredById(type)) {
+      if (classResolver.isRegisteredById(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);
         String ns, typename;
-        if (classResolver.isRegisteredByName(type)) {
-          Tuple2<String, String> nameTuple = 
classResolver.getRegisteredNameTuple(type);
+        if (classResolver.isRegisteredByName(currentType)) {
+          Tuple2<String, String> nameTuple = 
classResolver.getRegisteredNameTuple(currentType);
           ns = nameTuple.f0;
           typename = nameTuple.f1;
         } else {
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 ecd19d78..3d5b64eb 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
@@ -880,4 +880,63 @@ public class MetaSharedCompatibleTest extends FuryTestBase 
{
       Assert.assertEquals(f3.toString(), "C{f2=1000}");
     }
   }
+
+  @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(getObjectFieldValue(o1, "f1"));
+    Assert.assertEquals(ReflectionUtils.getObjectFieldValue(o1, "f2"), 20);
+    Assert.assertEquals(ReflectionUtils.getObjectFieldValue(o1, "f3"), 30);
+  }
 }


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

Reply via email to