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 94cc001af fix(java): fix xlang mode meta shared for java (#2592)
94cc001af is described below
commit 94cc001afaf3db661b35e81ffe553c3b2251716a
Author: Shawn Yang <[email protected]>
AuthorDate: Tue Sep 9 11:39:42 2025 +0800
fix(java): fix xlang mode meta shared for java (#2592)
<!--
**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.
-->
---
.../main/java/org/apache/fory/meta/TypeDefEncoder.java | 6 +++---
.../org/apache/fory/serializer/ObjectSerializer.java | 2 +-
.../src/main/java/org/apache/fory/type/Types.java | 3 +++
.../test/java/org/apache/fory/CrossLanguageTest.java | 18 ++++++++++++++++++
4 files changed, 25 insertions(+), 4 deletions(-)
diff --git
a/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
b/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
index 7981aba18..69b87bfa6 100644
--- a/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
+++ b/java/fory-core/src/main/java/org/apache/fory/meta/TypeDefEncoder.java
@@ -93,8 +93,8 @@ class TypeDefEncoder {
static MemoryBuffer encodeClassDef(
XtypeResolver resolver, Class<?> type, List<FieldInfo> fields) {
ClassInfo classInfo = resolver.getClassInfo(type);
- Preconditions.checkArgument(
- Types.isStructType(classInfo.getXtypeId()), "%s is not a struct",
type);
+ int xtypeId = (classInfo.getXtypeId()) & 0xff;
+ Preconditions.checkArgument(Types.isStructType(xtypeId), "%s is not a
struct", type);
MemoryBuffer buffer = MemoryBuffer.newHeapBuffer(128);
buffer.writeByte(-1); // placeholder for header, update later
int currentClassHeader = fields.size();
@@ -103,7 +103,7 @@ class TypeDefEncoder {
buffer.writeVarUint32(fields.size() - SMALL_NUM_FIELDS_THRESHOLD);
}
if (resolver.isRegisteredById(type)) {
- buffer.writeVarUint32(classInfo.getXtypeId());
+ buffer.writeVarUint32(xtypeId);
} else {
Preconditions.checkArgument(resolver.isRegisteredByName(type));
currentClassHeader |= REGISTER_BY_NAME_FLAG;
diff --git
a/java/fory-core/src/main/java/org/apache/fory/serializer/ObjectSerializer.java
b/java/fory-core/src/main/java/org/apache/fory/serializer/ObjectSerializer.java
index 881cedce0..13a7b5afe 100644
---
a/java/fory-core/src/main/java/org/apache/fory/serializer/ObjectSerializer.java
+++
b/java/fory-core/src/main/java/org/apache/fory/serializer/ObjectSerializer.java
@@ -370,7 +370,7 @@ public final class ObjectSerializer<T> extends
AbstractObjectSerializer<T> {
} else {
ClassInfo classInfo = resolver.getClassInfo(typeRef.getRawType());
int xtypeId = classInfo.getXtypeId();
- if (Types.isStructType((byte) xtypeId)) {
+ if (Types.isStructType(xtypeId & 0xff)) {
id =
TypeUtils.computeStringHash(
classInfo.decodeNamespace() + classInfo.decodeTypeName());
diff --git a/java/fory-core/src/main/java/org/apache/fory/type/Types.java
b/java/fory-core/src/main/java/org/apache/fory/type/Types.java
index 3a2a287d6..b7e937ba4 100644
--- a/java/fory-core/src/main/java/org/apache/fory/type/Types.java
+++ b/java/fory-core/src/main/java/org/apache/fory/type/Types.java
@@ -166,6 +166,7 @@ public class Types {
// Helper methods
public static boolean isStructType(int value) {
+ assert value < 0xff;
return value == STRUCT
|| value == COMPATIBLE_STRUCT
|| value == NAMED_STRUCT
@@ -173,10 +174,12 @@ public class Types {
}
public static boolean isExtType(int value) {
+ assert value < 0xff;
return value == EXT || value == NAMED_EXT;
}
public static boolean isEnumType(int value) {
+ assert value < 0xff;
return value == ENUM || value == NAMED_ENUM;
}
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 375c60d8f..21d49a386 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
@@ -55,6 +55,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.Data;
+import org.apache.fory.config.CompatibleMode;
import org.apache.fory.config.ForyBuilder;
import org.apache.fory.config.Language;
import org.apache.fory.logging.Logger;
@@ -512,6 +513,23 @@ public class CrossLanguageTest extends ForyTestBase {
structRoundBack(fory, obj2, "test_register_by_id");
}
+ @Test
+ public void testRegisterByIdMetaShare() throws Exception {
+ Fory fory =
+ Fory.builder()
+ .withLanguage(Language.XLANG)
+ .withRefTracking(true)
+ .withCompatibleMode(CompatibleMode.COMPATIBLE)
+ .requireClassRegistration(false)
+ .build();
+ fory.register(ComplexObject2.class, 100);
+ ComplexObject2 obj = new ComplexObject2();
+ obj.f1 = true;
+ obj.f2 = new HashMap<>(ImmutableMap.of((byte) -1, 2));
+ byte[] serialized = fory.serialize(obj);
+ Assert.assertEquals(fory.deserialize(serialized), obj);
+ }
+
public void testSerializeComplexStruct() throws Exception {
Fory fory =
Fory.builder()
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]