Skip to content

feat(java): Support codegen for xlang serialization in java for strict mode #2312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

Hen1ng
Copy link
Contributor

@Hen1ng Hen1ng commented Jun 8, 2025

What does this PR do?

Related issues

#2286

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

@Hen1ng Hen1ng requested a review from chaokunyang as a code owner June 8, 2025 09:19
@@ -240,6 +240,9 @@ public String genCode() {
ctx.addField(ctx.type(Fory.class), FORY_NAME);
Expression encodeExpr = buildEncodeExpression();
Expression decodeExpr = buildDecodeExpression();

Expression xlangEncodeExpr = buildXlangEncodeExpression();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should do this only fury is configured to use xlang mode, and when using xlang mode, do not generate code for java native serialization, i.e. don't generate code for read/write

@chaokunyang
Copy link
Collaborator

Could we add some unit tests for xlang jit mode?

@@ -123,6 +123,8 @@ public CodecBuilder(CodegenContext ctx, TypeRef<?> beanType) {
/** Returns an expression that serialize java bean of type {@link CodecBuilder#beanClass}. */
public abstract Expression buildEncodeExpression();

public abstract Expression buildXlangEncodeExpression();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep buildXlangEncodeExpression and buildXlangDecodeExpression in BaseObjectCodecBuilder.java?

@@ -83,7 +84,7 @@ public class CrossLanguageTest extends ForyTestBase {

@BeforeClass
public void isPyforyInstalled() {
TestUtils.verifyPyforyInstalled();
// TestUtils.verifyPyforyInstalled();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this is unrelated to this PR, could we revert this change?

@@ -798,4 +799,55 @@ public void testEnumField() throws java.io.IOException {
Assert.assertEquals(xserDe(fory, a), a);
structRoundBack(fory, a, "test_enum_field");
}

@Test
public void testCodeGen() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we split it into two tests, one for SCHEMA_CONSISTENT, and another for COMPATIBLE.

And perhaps we could create a new test file CodegenXlangTest.java under org.apache.fory.xlang package, and move this test into that file. CrossLanguageTest is a little big currently

import org.apache.fory.serializer.SerializationUtils;
import org.apache.fory.serializer.Serializer;
import org.apache.fory.serializer.Serializers;
import org.apache.fory.serializer.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fory doesn't use * imports, please expand all imports

addSerializer(cls, serializer, namespace, typeName, xtypeId);
}

/** Ass serializer for specified class. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does ass means here?

case SCHEMA_CONSISTENT:
return ObjectSerializer.class;
case COMPATIBLE:
return shareMeta ? ObjectSerializer.class : CompatibleSerializer.class;
Copy link
Collaborator

@chaokunyang chaokunyang Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CompatibleSerializer.class is not used for xlang serialization, we used meta shade mode for xlang forward/backward compatibility, CompatibleSerializer use kv format instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shareMeta ? MetaSharedSerializer.class : ObjectSerializer.class
is this correctly?

sc =
fory.getJITContext()
.registerSerializerJITCallback(
() -> ObjectSerializer.class,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer creating serializer lazily, since the hash compute for type meta will read names of all registered classes. And when class registration is invoked, if the serializer are created egaerly, some classes don't have a change to let the registered name/tag being used for hash computing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants