Repository navigation
fix(java): allocate non-static inner classes without a constructor in generated codecs - #4204
Merged
chaokunyang merged 2 commits intoNov 4, 2026
Merged
Conversation
… generated codecs
chaokunyang
approved these changes
Nov 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why?
On JDK 8-24, a generated serializer cannot be created for a non-static inner class. In the default native configuration, serialization succeeds and deserialization fails:
CodecBuilder.newBean()returnsnew Expression.NewInstance(beanType)for a source-public class on JDK < 25, and theNewInstanceconstructor rejects a non-static member class that has no outer pointer. An inner class has no no-arg constructor, soNewInstancewould allocate it without calling a constructor anyway; the outer pointer is never used on this path. On JDK 25+newBean()already takes theObjectInstantiatorbranch for classes without a public no-arg constructor, so the same objects round-trip there.Which paths are affected on JDK < 25:
CodegenSerializer.supportCodegenForJavaSerializationkeeps inner classes on the interpreterObjectSerializer.PriorityBlockingQueue,LinkedBlockingDequeand otherSerializableclasses that hold aCondition: their JDK serialization form contains anAbstractQueuedSynchronizer.ConditionObject, which is a public inner class. (Without reference tracking these still fail later withIllegalMonitorStateException, because the lock and itsCondition's outer instance are restored as two objects; that is unchanged by this PR and matches the interpreter path and JDK 25.)ensureSerializersCompiled()).Reproducer (fails on 1.7.7 with JDK 21, works with this change):
What does this PR do?
CodecBuilder.newBean()no longer builds aNewInstancefor a non-static member class. Such classes take the existing no-constructor allocation branch (Unsafe.allocateInstanceon JDK < 25,ObjectInstantiatoron JDK 25+) that non-public classes already use. Every class that could be generated before still takes the same branch, and the wire format is unchanged; nothing changes on JDK 25+.Routing inner classes to the interpreter
CompatibleSerializerinTypeResolver(as schema-consistent mode does) would fix only the native compatible read path, leave xlang broken, and move JDK 25+ off the generated serializer that already handles these classes, so I kept the fix innewBean().CodegenCompatibleSerializerTest#testNonStaticInnerClassround-trips a user inner class with and without reference tracking (checking the restored outer instance) and aPriorityBlockingQueuewith a comparator.Verification:
mainwith only the test added: fails on JDK 8, 11, 17 and 21 with the exception above; passes on JDK 25 and 26, which are not affected.fory-coretests with the change on JDK 8, 11, 17, 21, 25 and 26 (JDK 25+ with the CIJDK_JAVA_OPTIONS): 2352 tests, 0 failures each.fory-json(1285 tests) andfory-format(146 tests), which also inheritCodecBuilder.newBean(): 0 failures on JDK 21. The xlang peer suites were skipped locally (no peer runtimes).ensureSerializersCompiled()with-Dorg.graalvm.nativeimage.imagecode=buildtime, checked with standalone programs on JDK 21: both fail onmainand work with the change. I did not build a native image.mvn -T16 spotless:checkandmvn -T16 checkstyle:check: pass.Related issues
None found.
Does this PR introduce any user-facing change?
Benchmark
Not applicable: only non-static inner classes, which previously failed to generate, get different generated code.