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

chaokunyang 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 5b70810bd fix(java): fix xlang register type by id (#2591)
5b70810bd is described below

commit 5b70810bde017bdcf8ca36835ac32e18d4b23a02
Author: Shawn Yang <[email protected]>
AuthorDate: Tue Sep 9 02:29:46 2025 +0800

    fix(java): fix xlang register type by id (#2591)
    
    <!--
    **Thanks for contributing to Apache Fory™.**
    
    **If this is your first time opening a PR on fory, you can refer to
    
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
    
    Contribution Checklist
    
    - The **Apache Fory™** community has requirements on the naming of pr
    titles. You can also find instructions in
    [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
    
    - Apache Fory™ 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.
    -->
    
    ## Why?
    
    <!-- Describe the purpose of this PR. -->
    
    ## What does this PR do?
    
    <!-- Describe the details of this PR. -->
    
    ## Related issues
    
    <!--
    Is there any related issue? If this PR closes them you say say
    fix/closes:
    
    - #xxxx0
    - #xxxx1
    - Fixes #xxxx2
    -->
    
    ## 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.
    
    Delete section if not applicable.
    -->
    
    - [ ] 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.
    
    Delete section if not applicable.
    -->
---
 .../org/apache/fory/resolver/XtypeResolver.java    | 34 ++++++++++++++++++----
 .../java/org/apache/fory/CrossLanguageTest.java    | 15 ++++++++++
 .../fory/format/vectorized/ArrowSerializers.java   | 13 ++++++---
 python/pyfory/tests/test_cross_language.py         |  8 +++++
 4 files changed, 61 insertions(+), 9 deletions(-)

diff --git 
a/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java 
b/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java
index 3f70c4a01..ca7f83e2c 100644
--- a/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java
+++ b/java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java
@@ -49,6 +49,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 import org.apache.fory.Fory;
+import org.apache.fory.annotation.Internal;
 import org.apache.fory.collection.IdentityMap;
 import org.apache.fory.collection.IdentityObjectIntMap;
 import org.apache.fory.collection.LongMap;
@@ -139,11 +140,10 @@ public class XtypeResolver implements TypeResolver {
     register(type, xtypeIdGenerator++);
   }
 
-  public void register(Class<?> type, int typeId) {
+  public void register(Class<?> type, int userTypeId) {
     // ClassInfo[] has length of max type id. If the type id is too big, Fory 
will waste many
-    // memory.
-    // We can relax this limit in the future.
-    Preconditions.checkArgument(typeId < MAX_TYPE_ID, "Too big type id %s", 
typeId);
+    // memory. We can relax this limit in the future.
+    Preconditions.checkArgument(userTypeId < MAX_TYPE_ID, "Too big type id 
%s", userTypeId);
     ClassInfo classInfo = classInfoMap.get(type);
     if (type.isArray()) {
       buildClassInfo(type);
@@ -165,7 +165,7 @@ public class XtypeResolver implements TypeResolver {
                 type, prevNamespace, prevTypeName));
       }
     }
-    int xtypeId = typeId;
+    int xtypeId = userTypeId;
     if (type.isEnum()) {
       xtypeId = (xtypeId << 8) + Types.ENUM;
     } else {
@@ -175,6 +175,8 @@ public class XtypeResolver implements TypeResolver {
         } else {
           xtypeId = (xtypeId << 8) + Types.EXT;
         }
+      } else {
+        xtypeId = (xtypeId << 8) + Types.STRUCT;
       }
     }
     register(
@@ -242,6 +244,28 @@ public class XtypeResolver implements TypeResolver {
     xtypeIdToClassMap.put(xtypeId, classInfo);
   }
 
+  /**
+   * Register type with given type id and serializer for type in fory type 
system.
+   *
+   * <p>Do not use this method to register custom type in java type system. 
Use {@link
+   * #register(Class, String, String)} or {@link #register(Class, int)} 
instead.
+   *
+   * @param type type to register.
+   * @param serializer serializer to register.
+   * @param typeId type id to register.
+   * @throws IllegalArgumentException if type id is too big.
+   */
+  @Internal
+  public void registerForyType(Class<?> type, Serializer serializer, int 
typeId) {
+    Preconditions.checkArgument(typeId < MAX_TYPE_ID, "Too big type id %s", 
typeId);
+    register(
+        type,
+        serializer,
+        ReflectionUtils.getPackage(type),
+        ReflectionUtils.getClassNameWithoutPackage(type),
+        typeId);
+  }
+
   private boolean isStructType(Serializer serializer) {
     if (serializer instanceof ObjectSerializer || serializer instanceof 
GeneratedSerializer) {
       return true;
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java 
b/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java
index 1d887425e..375c60d8f 100644
--- a/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java
+++ b/java/fory-core/src/test/java/org/apache/fory/CrossLanguageTest.java
@@ -497,6 +497,21 @@ public class CrossLanguageTest extends ForyTestBase {
     structRoundBack(fory, obj2, "test_serialize_simple_struct");
   }
 
+  @Test
+  public void testRegisterById() throws Exception {
+    Fory fory =
+        Fory.builder()
+            .withLanguage(Language.XLANG)
+            .withRefTracking(true)
+            .requireClassRegistration(false)
+            .build();
+    fory.register(ComplexObject2.class, 100);
+    ComplexObject2 obj2 = new ComplexObject2();
+    obj2.f1 = true;
+    obj2.f2 = new HashMap<>(ImmutableMap.of((byte) -1, 2));
+    structRoundBack(fory, obj2, "test_register_by_id");
+  }
+
   public void testSerializeComplexStruct() throws Exception {
     Fory fory =
         Fory.builder()
diff --git 
a/java/fory-format/src/main/java/org/apache/fory/format/vectorized/ArrowSerializers.java
 
b/java/fory-format/src/main/java/org/apache/fory/format/vectorized/ArrowSerializers.java
index 7f5828f7b..ec482cbcc 100644
--- 
a/java/fory-format/src/main/java/org/apache/fory/format/vectorized/ArrowSerializers.java
+++ 
b/java/fory-format/src/main/java/org/apache/fory/format/vectorized/ArrowSerializers.java
@@ -37,6 +37,7 @@ import org.apache.fory.io.MockWritableChannel;
 import org.apache.fory.memory.MemoryBuffer;
 import org.apache.fory.memory.MemoryUtils;
 import org.apache.fory.memory.Platform;
+import org.apache.fory.resolver.XtypeResolver;
 import org.apache.fory.serializer.BufferObject;
 import 
org.apache.fory.serializer.Serializers.CrossLanguageCompatibleSerializer;
 import org.apache.fory.type.Types;
@@ -167,10 +168,14 @@ public class ArrowSerializers {
 
   public static void registerSerializers(Fory fory) {
     if (fory.isCrossLanguage()) {
-      fory.register(ArrowTable.class, Types.ARROW_TABLE);
-      fory.register(VectorSchemaRoot.class, Types.ARROW_RECORD_BATCH);
+      XtypeResolver resolver = fory.getXtypeResolver();
+      resolver.registerForyType(
+          ArrowTable.class, new ArrowTableSerializer(fory), Types.ARROW_TABLE);
+      resolver.registerForyType(
+          VectorSchemaRoot.class, new VectorSchemaRootSerializer(fory), 
Types.ARROW_RECORD_BATCH);
+    } else {
+      fory.registerSerializer(ArrowTable.class, new 
ArrowTableSerializer(fory));
+      fory.registerSerializer(VectorSchemaRoot.class, new 
VectorSchemaRootSerializer(fory));
     }
-    fory.registerSerializer(ArrowTable.class, new ArrowTableSerializer(fory));
-    fory.registerSerializer(VectorSchemaRoot.class, new 
VectorSchemaRootSerializer(fory));
   }
 }
diff --git a/python/pyfory/tests/test_cross_language.py 
b/python/pyfory/tests/test_cross_language.py
index 7b4615055..7b7e1b488 100644
--- a/python/pyfory/tests/test_cross_language.py
+++ b/python/pyfory/tests/test_cross_language.py
@@ -462,6 +462,14 @@ def test_serialize_simple_struct(data_file_path):
     struct_round_back(data_file_path, fory, obj)
 
 
+@cross_language_test
+def test_register_by_id(data_file_path):
+    fory = pyfory.Fory(language=pyfory.Language.XLANG, ref_tracking=True)
+    fory.register_type(ComplexObject2, type_id=100)
+    obj = ComplexObject2(f1=True, f2={-1: 2})
+    struct_round_back(data_file_path, fory, obj)
+
+
 class SomeClass:
     f1: "SomeClass"
     f2: Dict[str, str]


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

Reply via email to