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 8e1517dbc fix(java): fix get nested genericType for codegen (#2632)
8e1517dbc is described below
commit 8e1517dbcdfeef66795d43b00123871e6c915ad1
Author: Shawn Yang <[email protected]>
AuthorDate: Sat Sep 20 00:55:27 2025 +0800
fix(java): fix get nested genericType for codegen (#2632)
<!--
**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
Fixes #2628
## 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/ClassResolver.java | 15 ++----
.../org/apache/fory/resolver/TypeResolver.java | 54 ++++++++++++++++++++++
.../java/org/apache/fory/type/GenericType.java | 4 ++
.../main/java/org/apache/fory/type/TypeUtils.java | 8 ++++
.../serializer/collection/MapSerializersTest.java | 26 +++++++++++
5 files changed, 96 insertions(+), 11 deletions(-)
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 ff41a8b99..6d421f374 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
@@ -1983,20 +1983,13 @@ public class ClassResolver extends TypeResolver {
public void resetWrite() {}
+ private static final GenericType OBJECT_GENERIC_TYPE =
GenericType.build(Object.class);
+
@CodegenInvoke
public GenericType getGenericTypeInStruct(Class<?> cls, String
genericTypeStr) {
Map<String, GenericType> map =
- extRegistry.classGenericTypes.computeIfAbsent(cls, k -> new
HashMap<>());
- GenericType genericType = map.get(genericTypeStr);
- if (genericType == null) {
- for (Field field : ReflectionUtils.getFields(cls, true)) {
- Type type = field.getGenericType();
- TypeRef<Object> typeRef = TypeRef.of(type);
- genericType = buildGenericType(typeRef);
- map.put(type.getTypeName(), genericType);
- }
- }
- return genericType;
+ extRegistry.classGenericTypes.computeIfAbsent(cls,
this::buildGenericMap);
+ return map.getOrDefault(genericTypeStr, OBJECT_GENERIC_TYPE);
}
@Override
diff --git
a/java/fory-core/src/main/java/org/apache/fory/resolver/TypeResolver.java
b/java/fory-core/src/main/java/org/apache/fory/resolver/TypeResolver.java
index 4367b3a0e..59bef735f 100644
--- a/java/fory-core/src/main/java/org/apache/fory/resolver/TypeResolver.java
+++ b/java/fory-core/src/main/java/org/apache/fory/resolver/TypeResolver.java
@@ -19,8 +19,10 @@
package org.apache.fory.resolver;
+import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
@@ -28,6 +30,7 @@ import org.apache.fory.Fory;
import org.apache.fory.annotation.Internal;
import org.apache.fory.codegen.Expression;
import org.apache.fory.codegen.Expression.Invoke;
+import org.apache.fory.collection.Tuple2;
import org.apache.fory.memory.MemoryBuffer;
import org.apache.fory.meta.ClassDef;
import org.apache.fory.reflect.ReflectionUtils;
@@ -38,6 +41,7 @@ import org.apache.fory.type.Descriptor;
import org.apache.fory.type.DescriptorGrouper;
import org.apache.fory.type.GenericType;
import org.apache.fory.type.ScalaTypes;
+import org.apache.fory.type.TypeUtils;
// Internal type dispatcher.
// Do not use this interface outside of fory package
@@ -170,6 +174,56 @@ public abstract class TypeResolver {
public abstract Collection<Descriptor> getFieldDescriptors(Class<?>
beanClass, boolean b);
+ /**
+ * Build a map of nested generic type name to generic type for all fields in
the class.
+ *
+ * @param cls the class to build the map of nested generic type name to
generic type for all
+ * fields in the class
+ * @return a map of nested generic type name to generic type for all fields
in the class
+ */
+ protected Map<String, GenericType> buildGenericMap(Class<?> cls) {
+ Map<String, GenericType> map = new HashMap<>();
+ Map<String, GenericType> map2 = new HashMap<>();
+ for (Field field : ReflectionUtils.getFields(cls, true)) {
+ Type type = field.getGenericType();
+ GenericType genericType = buildGenericType(type);
+ buildGenericMap(map, genericType);
+ TypeRef<?> typeRef = TypeRef.of(type);
+ buildGenericMap(map2, typeRef);
+ }
+ map.putAll(map2);
+ return map;
+ }
+
+ private void buildGenericMap(Map<String, GenericType> map, TypeRef<?>
typeRef) {
+ if (map.containsKey(typeRef.getType().getTypeName())) {
+ return;
+ }
+ map.put(typeRef.getType().getTypeName(), buildGenericType(typeRef));
+ Class<?> rawType = typeRef.getRawType();
+ if (TypeUtils.isMap(rawType)) {
+ Tuple2<TypeRef<?>, TypeRef<?>> kvTypes =
TypeUtils.getMapKeyValueType(typeRef);
+ buildGenericMap(map, kvTypes.f0);
+ buildGenericMap(map, kvTypes.f1);
+ } else if (TypeUtils.isCollection(rawType)) {
+ TypeRef<?> elementType = TypeUtils.getElementType(typeRef);
+ buildGenericMap(map, elementType);
+ } else if (rawType.isArray()) {
+ TypeRef<?> arrayComponent = TypeUtils.getArrayComponent(typeRef);
+ buildGenericMap(map, arrayComponent);
+ }
+ }
+
+ private void buildGenericMap(Map<String, GenericType> map, GenericType
genericType) {
+ if (map.containsKey(genericType.getType().getTypeName())) {
+ return;
+ }
+ map.put(genericType.getType().getTypeName(), genericType);
+ for (GenericType t : genericType.getTypeParameters()) {
+ buildGenericMap(map, t);
+ }
+ }
+
public final Fory getFory() {
return fory;
}
diff --git a/java/fory-core/src/main/java/org/apache/fory/type/GenericType.java
b/java/fory-core/src/main/java/org/apache/fory/type/GenericType.java
index f5e7333f4..36c9a3eb7 100644
--- a/java/fory-core/src/main/java/org/apache/fory/type/GenericType.java
+++ b/java/fory-core/src/main/java/org/apache/fory/type/GenericType.java
@@ -166,6 +166,10 @@ public class GenericType {
return typeRef;
}
+ public Type getType() {
+ return typeRef.getType();
+ }
+
public Class<?> getCls() {
return cls;
}
diff --git a/java/fory-core/src/main/java/org/apache/fory/type/TypeUtils.java
b/java/fory-core/src/main/java/org/apache/fory/type/TypeUtils.java
index 2095d2000..03fc675d7 100644
--- a/java/fory-core/src/main/java/org/apache/fory/type/TypeUtils.java
+++ b/java/fory-core/src/main/java/org/apache/fory/type/TypeUtils.java
@@ -414,6 +414,14 @@ public class TypeUtils {
return type;
}
+ public static TypeRef<?> getArrayComponent(TypeRef<?> type) {
+ if (type.getType() instanceof GenericArrayType) {
+ Type componentType = ((GenericArrayType)
(type.getType())).getGenericComponentType();
+ return TypeRef.of(componentType);
+ }
+ return TypeRef.of(getArrayComponentInfo(type.getRawType()).f0);
+ }
+
public static Class<?> getArrayComponent(Class<?> type) {
return getArrayComponentInfo(type).f0;
}
diff --git
a/java/fory-core/src/test/java/org/apache/fory/serializer/collection/MapSerializersTest.java
b/java/fory-core/src/test/java/org/apache/fory/serializer/collection/MapSerializersTest.java
index b19b76e26..4e00a7953 100644
---
a/java/fory-core/src/test/java/org/apache/fory/serializer/collection/MapSerializersTest.java
+++
b/java/fory-core/src/test/java/org/apache/fory/serializer/collection/MapSerializersTest.java
@@ -1048,4 +1048,30 @@ public class MapSerializersTest extends ForyTestBase {
State state2 = (State) fory2.deserialize(bytes);
Assert.assertEquals(state2.map.get("bar"), new String[] {"bar"});
}
+
+ @Data
+ public static class OuterClass {
+ private Map<String, InnerClass> f1 = new HashMap<>();
+ private TestEnum f2;
+ }
+
+ @Data
+ public static class InnerClass {
+ int f1;
+ }
+
+ @Test(dataProvider = "compatible")
+ public void testNestedMapGenericCodegen(boolean compatible) {
+ Fory fory =
+ builder()
+ .withCodegen(true)
+ .withCompatibleMode(
+ compatible ? CompatibleMode.COMPATIBLE :
CompatibleMode.SCHEMA_CONSISTENT)
+ .requireClassRegistration(false)
+ .build();
+
+ OuterClass value = new OuterClass();
+ value.f1.put("aaa", null);
+ serDeCheck(fory, value);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]