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

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


The following commit(s) were added to refs/heads/releases-0.12 by this push:
     new 12507ef0f fix(java): fix codegen name conflict (#2565) (#2571)
12507ef0f is described below

commit 12507ef0f97f2038ab209589c2cdd52ea1fa3ce0
Author: Shawn Yang <[email protected]>
AuthorDate: Wed Sep 3 14:17:27 2025 +0800

    fix(java): fix codegen name conflict (#2565) (#2571)
    
    ## Why?
    
    <!-- Describe the purpose of this PR. -->
    
    ## What does this PR do?
    
    <!-- Describe the details of this PR. -->
    
    ## Related issues
    
    Fix #2564
    
    ## 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.
    -->
    
    <!--
    **Thanks for contributing to 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).
    
    - 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.
    -->
---
 .../fory/builder/BaseObjectCodecBuilder.java       | 12 ++++++------
 .../java/org/apache/fory/builder/CodecBuilder.java |  9 +++++++--
 .../fory/builder/MetaSharedCodecBuilder.java       |  2 +-
 .../apache/fory/builder/ObjectCodecBuilder.java    |  1 -
 .../org/apache/fory/resolver/ClassResolver.java    |  3 +++
 .../fory/serializer/CodegenSerializerTest.java     | 22 ++++++++++++++++++++++
 6 files changed, 39 insertions(+), 10 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 859dd594f..7460664c2 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
@@ -131,11 +131,11 @@ import org.apache.fory.util.StringUtils;
  */
 @SuppressWarnings("unchecked")
 public abstract class BaseObjectCodecBuilder extends CodecBuilder {
-  public static final String BUFFER_NAME = "buffer";
-  public static final String REF_RESOLVER_NAME = "refResolver";
-  public static final String CLASS_RESOLVER_NAME = "classResolver";
-  public static final String POJO_CLASS_TYPE_NAME = "classType";
-  public static final String STRING_SERIALIZER_NAME = "strSerializer";
+  public static final String BUFFER_NAME = "_f_buffer";
+  public static final String REF_RESOLVER_NAME = "_f_refResolver";
+  public static final String CLASS_RESOLVER_NAME = "_f_classResolver";
+  public static final String POJO_CLASS_TYPE_NAME = "_f_classType";
+  public static final String STRING_SERIALIZER_NAME = "_f_strSerializer";
   private static final TypeRef<?> CLASS_RESOLVER_TYPE_TOKEN = 
TypeRef.of(ClassResolver.class);
   private static final TypeRef<?> STRING_SERIALIZER_TYPE_TOKEN = 
TypeRef.of(StringSerializer.class);
   private static final TypeRef<?> SERIALIZER_TYPE = 
TypeRef.of(Serializer.class);
@@ -266,7 +266,7 @@ public abstract class BaseObjectCodecBuilder extends 
CodecBuilder {
         ROOT_OBJECT_NAME);
     ctx.overrideMethod("read", decodeCode, Object.class, MemoryBuffer.class, 
BUFFER_NAME);
     registerJITNotifyCallback();
-    ctx.addConstructor(constructorCode, Fory.class, "fory", Class.class, 
POJO_CLASS_TYPE_NAME);
+    ctx.addConstructor(constructorCode, Fory.class, FORY_NAME, Class.class, 
POJO_CLASS_TYPE_NAME);
     return ctx.genCode();
   }
 
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/builder/CodecBuilder.java 
b/java/fory-core/src/main/java/org/apache/fory/builder/CodecBuilder.java
index eefb7212b..b9d0b44bb 100644
--- a/java/fory-core/src/main/java/org/apache/fory/builder/CodecBuilder.java
+++ b/java/fory-core/src/main/java/org/apache/fory/builder/CodecBuilder.java
@@ -42,6 +42,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
 import java.util.function.Supplier;
+import java.util.stream.Collectors;
 import org.apache.fory.Fory;
 import org.apache.fory.codegen.CodegenContext;
 import org.apache.fory.codegen.Expression;
@@ -80,9 +81,9 @@ import org.apache.fory.util.record.RecordUtils;
  */
 @SuppressWarnings("UnstableApiUsage")
 public abstract class CodecBuilder {
-  protected static final String ROOT_OBJECT_NAME = "obj";
+  protected static final String ROOT_OBJECT_NAME = "_f_obj";
   // avoid user class has field with name fory.
-  protected static final String FORY_NAME = "fory";
+  protected static final String FORY_NAME = "_f_fory";
   static TypeRef<Object[]> objectArrayTypeRef = TypeRef.of(Object[].class);
   static TypeRef<MemoryBuffer> bufferTypeRef = TypeRef.of(MemoryBuffer.class);
   static TypeRef<ClassInfo> classInfoTypeRef = TypeRef.of(ClassInfo.class);
@@ -115,6 +116,10 @@ public abstract class CodecBuilder {
     ctx.reserveName(ROOT_OBJECT_NAME);
     // Don't import other packages to avoid class conflicts.
     // For example user class named as `Date`/`List`/`MemoryBuffer`
+    ReflectionUtils.getFields(beanType.getRawType(), true).stream()
+        .map(Field::getName)
+        .collect(Collectors.toSet())
+        .forEach(ctx::reserveName);
   }
 
   /** Generate codec class code. */
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/builder/MetaSharedCodecBuilder.java
 
b/java/fory-core/src/main/java/org/apache/fory/builder/MetaSharedCodecBuilder.java
index c6da767a3..1c25a77a1 100644
--- 
a/java/fory-core/src/main/java/org/apache/fory/builder/MetaSharedCodecBuilder.java
+++ 
b/java/fory-core/src/main/java/org/apache/fory/builder/MetaSharedCodecBuilder.java
@@ -166,7 +166,7 @@ public class MetaSharedCodecBuilder extends 
ObjectCodecBuilder {
     decodeCode = ctx.optimizeMethodCode(decodeCode);
     ctx.overrideMethod("read", decodeCode, Object.class, MemoryBuffer.class, 
BUFFER_NAME);
     registerJITNotifyCallback();
-    ctx.addConstructor(constructorCode, Fory.class, "fory", Class.class, 
POJO_CLASS_TYPE_NAME);
+    ctx.addConstructor(constructorCode, Fory.class, FORY_NAME, Class.class, 
POJO_CLASS_TYPE_NAME);
     return ctx.genCode();
   }
 
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/builder/ObjectCodecBuilder.java 
b/java/fory-core/src/main/java/org/apache/fory/builder/ObjectCodecBuilder.java
index b4e525d7f..15c71cde0 100644
--- 
a/java/fory-core/src/main/java/org/apache/fory/builder/ObjectCodecBuilder.java
+++ 
b/java/fory-core/src/main/java/org/apache/fory/builder/ObjectCodecBuilder.java
@@ -79,7 +79,6 @@ import org.apache.fory.util.record.RecordUtils;
  * @see ObjectCodecOptimizer for code stats and split heuristics.
  */
 public class ObjectCodecBuilder extends BaseObjectCodecBuilder {
-  public static final String BUFFER_NAME = "buffer";
   private final Literal classVersionHash;
   protected ObjectCodecOptimizer objectCodecOptimizer;
   protected Map<String, Integer> recordReversedMapping;
diff --git 
a/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java 
b/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java
index 3f2f339cd..471a6069e 100644
--- a/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java
+++ b/java/fory-core/src/main/java/org/apache/fory/resolver/ClassResolver.java
@@ -1431,6 +1431,7 @@ public class ClassResolver implements TypeResolver {
         Tuple2<ClassDef, ClassInfo> classDefTuple = 
extRegistry.classIdToDef.get(classDef.getId());
         // empty serializer for graalvm build time
         classDefTuple.f1.serializer = null;
+        classInfoCache = NIL_CLASS_INFO;
         extRegistry.classIdToDef.put(classDef.getId(), 
Tuple2.of(classDefTuple.f0, null));
       }
     }
@@ -1440,6 +1441,7 @@ public class ClassResolver implements TypeResolver {
           .serializerClassMap
           .put(cls, getGraalvmSerializerClass(classInfo.serializer));
       classInfo.serializer = null;
+      classInfoCache = NIL_CLASS_INFO;
     }
   }
 
@@ -2245,6 +2247,7 @@ public class ClassResolver implements TypeResolver {
               }
             });
       }
+      classInfoCache = NIL_CLASS_INFO;
     } finally {
       fory.getJITContext().unlock();
     }
diff --git 
a/java/fory-core/src/test/java/org/apache/fory/serializer/CodegenSerializerTest.java
 
b/java/fory-core/src/test/java/org/apache/fory/serializer/CodegenSerializerTest.java
index 53af834c1..cc85de856 100644
--- 
a/java/fory-core/src/test/java/org/apache/fory/serializer/CodegenSerializerTest.java
+++ 
b/java/fory-core/src/test/java/org/apache/fory/serializer/CodegenSerializerTest.java
@@ -35,6 +35,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.ToString;
@@ -45,6 +46,7 @@ import org.apache.fory.config.Language;
 import org.apache.fory.memory.MemoryBuffer;
 import org.apache.fory.memory.MemoryUtils;
 import org.apache.fory.test.bean.Cyclic;
+import org.apache.fory.test.bean.Foo;
 import org.testng.Assert;
 import org.testng.annotations.Test;
 
@@ -401,4 +403,24 @@ public class CodegenSerializerTest extends ForyTestBase {
     A o = serDe(fory, a);
     assertEquals(a, o);
   }
+
+  @Data
+  @AllArgsConstructor
+  public static class ConflictName {
+    public Foo buffer;
+    public int fory;
+    public int classType;
+  }
+
+  @Test(dataProvider = "referenceTrackingConfig")
+  public void testNameConflict(boolean trackRef) {
+    Fory fory =
+        builder()
+            .requireClassRegistration(false)
+            .withCodegen(true)
+            .withRefTracking(trackRef)
+            .build();
+    ConflictName c = new ConflictName(Foo.create(), -100, 200);
+    serDeCheck(fory, c);
+  }
 }


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

Reply via email to