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

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


The following commit(s) were added to refs/heads/main by this push:
     new 80ffbebc3 fix(java): fix map/list element type same with collection 
type jit error (#2465)
80ffbebc3 is described below

commit 80ffbebc302ad48ac7dc513d5abcaa14eea3136a
Author: Shawn Yang <[email protected]>
AuthorDate: Thu Aug 14 16:12:53 2025 +0800

    fix(java): fix map/list element type same with collection type jit error 
(#2465)
    
    ## What does this PR do?
    
    <!-- Describe the purpose of this PR. -->
    
    ## Related issues
    
    Closes #2454
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/fory/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.
    -->
---
 .../fory/builder/BaseObjectCodecBuilder.java       | 37 +++++++++++++++++-----
 .../collection/ChildContainerSerializersTest.java  | 20 ++++++++++++
 2 files changed, 49 insertions(+), 8 deletions(-)

diff --git 
a/java/fory-core/src/main/java/org/apache/fory/builder/BaseObjectCodecBuilder.java
 
b/java/fory-core/src/main/java/org/apache/fory/builder/BaseObjectCodecBuilder.java
index 2f8efe1ec..0d965a5ff 100644
--- 
a/java/fory-core/src/main/java/org/apache/fory/builder/BaseObjectCodecBuilder.java
+++ 
b/java/fory-core/src/main/java/org/apache/fory/builder/BaseObjectCodecBuilder.java
@@ -64,7 +64,6 @@ import static 
org.apache.fory.type.TypeUtils.PRIMITIVE_INT_TYPE;
 import static org.apache.fory.type.TypeUtils.PRIMITIVE_LONG_TYPE;
 import static org.apache.fory.type.TypeUtils.PRIMITIVE_VOID_TYPE;
 import static org.apache.fory.type.TypeUtils.SET_TYPE;
-import static org.apache.fory.type.TypeUtils.getElementType;
 import static org.apache.fory.type.TypeUtils.getRawType;
 import static org.apache.fory.type.TypeUtils.isBoxed;
 import static org.apache.fory.type.TypeUtils.isPrimitive;
@@ -484,19 +483,19 @@ public abstract class BaseObjectCodecBuilder extends 
CodecBuilder {
   }
 
   protected boolean useCollectionSerialization(TypeRef<?> typeRef) {
-    return fory(f -> 
f.getClassResolver().isCollection(TypeUtils.getRawType(typeRef)));
+    return useCollectionSerialization(TypeUtils.getRawType(typeRef));
   }
 
   protected boolean useCollectionSerialization(Class<?> type) {
-    return fory(f -> 
f.getClassResolver().isCollection(TypeUtils.getRawType(type)));
+    return fory(f -> f.getClassResolver().isCollection(type));
   }
 
   protected boolean useMapSerialization(TypeRef<?> typeRef) {
-    return fory(f -> 
f.getClassResolver().isMap(TypeUtils.getRawType(typeRef)));
+    return useMapSerialization(TypeUtils.getRawType(typeRef));
   }
 
   protected boolean useMapSerialization(Class<?> type) {
-    return fory(f -> f.getClassResolver().isMap(TypeUtils.getRawType(type)));
+    return fory(f -> f.getClassResolver().isMap(type));
   }
 
   /**
@@ -834,12 +833,13 @@ public abstract class BaseObjectCodecBuilder extends 
CodecBuilder {
       serializer =
           cast(serializer, TypeRef.of(AbstractCollectionSerializer.class), 
"colSerializer");
     }
+    TypeRef<?> elementType = getElementType(typeRef);
     // write collection data.
     ListExpression actions = new ListExpression();
     Expression write =
         new If(
             inlineInvoke(serializer, "supportCodegenHook", 
PRIMITIVE_BOOLEAN_TYPE),
-            writeCollectionData(buffer, collection, serializer, 
getElementType(typeRef)),
+            writeCollectionData(buffer, collection, serializer, elementType),
             new Invoke(serializer, "write", buffer, collection));
     actions.add(write);
     if (generateNewMethod) {
@@ -849,6 +849,14 @@ public abstract class BaseObjectCodecBuilder extends 
CodecBuilder {
     return actions;
   }
 
+  private TypeRef<?> getElementType(TypeRef<?> typeRef) {
+    TypeRef<?> elementType = TypeUtils.getElementType(typeRef);
+    if (elementType.equals(typeRef)) {
+      elementType = OBJECT_TYPE;
+    }
+    return elementType;
+  }
+
   protected Expression writeCollectionData(
       Expression buffer, Expression collection, Expression serializer, 
TypeRef<?> elementType) {
     Invoke onCollectionWrite =
@@ -1146,9 +1154,22 @@ public abstract class BaseObjectCodecBuilder extends 
CodecBuilder {
     return write;
   }
 
+  private Tuple2<TypeRef<?>, TypeRef<?>> getMapKeyValueType(TypeRef<?> 
typeRef) {
+    Tuple2<TypeRef<?>, TypeRef<?>> keyValueType = 
TypeUtils.getMapKeyValueType(typeRef);
+    TypeRef<?> keyType = keyValueType.f0;
+    TypeRef<?> valueType = keyValueType.f1;
+    if (keyType.equals(typeRef)) {
+      keyType = OBJECT_TYPE;
+    }
+    if (valueType.equals(typeRef)) {
+      valueType = OBJECT_TYPE;
+    }
+    return Tuple2.of(keyType, valueType);
+  }
+
   private Expression jitWriteMap(
       Expression buffer, Expression map, Expression serializer, TypeRef<?> 
typeRef) {
-    Tuple2<TypeRef<?>, TypeRef<?>> keyValueType = 
TypeUtils.getMapKeyValueType(typeRef);
+    Tuple2<TypeRef<?>, TypeRef<?>> keyValueType = getMapKeyValueType(typeRef);
     TypeRef<?> keyType = keyValueType.f0;
     TypeRef<?> valueType = keyValueType.f1;
     map = new Invoke(serializer, "onMapWrite", TypeUtils.mapOf(keyType, 
valueType), buffer, map);
@@ -1867,7 +1888,7 @@ public abstract class BaseObjectCodecBuilder extends 
CodecBuilder {
    */
   protected Expression deserializeForMap(
       Expression buffer, TypeRef<?> typeRef, Expression serializer, InvokeHint 
invokeHint) {
-    Tuple2<TypeRef<?>, TypeRef<?>> keyValueType = 
TypeUtils.getMapKeyValueType(typeRef);
+    Tuple2<TypeRef<?>, TypeRef<?>> keyValueType = getMapKeyValueType(typeRef);
     TypeRef<?> keyType = keyValueType.f0;
     TypeRef<?> valueType = keyValueType.f1;
     if (serializer == null) {
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/serializer/collection/ChildContainerSerializersTest.java
 
b/java/fory-core/src/test/java/org/apache/fory/serializer/collection/ChildContainerSerializersTest.java
index a54d7e3ed..86e9891e6 100644
--- 
a/java/fory-core/src/test/java/org/apache/fory/serializer/collection/ChildContainerSerializersTest.java
+++ 
b/java/fory-core/src/test/java/org/apache/fory/serializer/collection/ChildContainerSerializersTest.java
@@ -259,4 +259,24 @@ public class ChildContainerSerializersTest extends 
ForyTestBase {
             .build();
     serDeMetaShared(fory, outerDO);
   }
+
+  public static class ChildLinkedListElemList extends 
LinkedList<ChildLinkedListElemList> {}
+
+  public static class ChildLinkedListElemListStruct {
+    public ChildLinkedListElemList list;
+  }
+
+  @Test
+  public void testElemTypeSameWithCollection() {
+    Fory fory = builder().withRefTracking(true).build();
+    ChildLinkedListElemList list = new ChildLinkedListElemList();
+    list.add(list);
+    ChildLinkedListElemList list1 = serDe(fory, list);
+    Assert.assertSame(list1.get(0), list1);
+
+    ChildLinkedListElemListStruct struct = new ChildLinkedListElemListStruct();
+    struct.list = list;
+    ChildLinkedListElemListStruct struct1 = serDe(fory, struct);
+    Assert.assertSame(struct1.list.get(0), struct1.list);
+  }
 }


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

Reply via email to