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 ed4ea7310 fix(java): fix codegen name conflict (#2565)
ed4ea7310 is described below

commit ed4ea73100aad085bf21142ca0983321aad7c457
Author: Shawn Yang <[email protected]>
AuthorDate: Wed Sep 3 00:28:19 2025 +0800

    fix(java): fix codegen name conflict (#2565)
    
    ## 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.
    -->
---
 .../fory/builder/BaseObjectCodecBuilder.java       | 12 ++++++------
 .../java/org/apache/fory/builder/CodecBuilder.java |  9 +++++++--
 .../fory/builder/MetaSharedCodecBuilder.java       |  2 +-
 .../apache/fory/builder/ObjectCodecBuilder.java    |  1 -
 .../fory/serializer/CodegenSerializerTest.java     | 22 ++++++++++++++++++++++
 5 files changed, 36 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/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