fix: avoid NoClassDefFoundError in supportCodegenForJavaSerialization#3424
Merged
chaokunyang merged 2 commits intoapache:mainfrom Feb 26, 2026
Merged
fix: avoid NoClassDefFoundError in supportCodegenForJavaSerialization#3424chaokunyang merged 2 commits intoapache:mainfrom
chaokunyang merged 2 commits intoapache:mainfrom
Conversation
Check Modifier.isStatic() before calling getEnclosingClass() to avoid loading the enclosing class unnecessarily. In classloader-isolated environments (OSGi, custom module systems), the enclosing class may not be visible from the current classloader, causing NoClassDefFoundError. Also return false instead of re-throwing when the enclosing class cannot be loaded — if the enclosing class is not resolvable, the class cannot be a valid non-static inner class in this context.
…on() Swap evaluation order so that when codegen is disabled, supportCodegenForJavaSerialization() is never called. This avoids triggering getEnclosingClass() in classloader-isolated environments where withCodegen(false) should completely bypass codegen logic. Both call sites in getSerializerClass() and getObjectSerializerClass() had the same issue.
3 tasks
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.
Summary
CodegenSerializer.supportCodegenForJavaSerialization()callscls.getEnclosingClass()before checkingModifier.isStatic(). In classloader-isolated environments (OSGi, custom module systems with per-module classloaders), the enclosing class may not be visible from the class's classloader, causingNoClassDefFoundError.Two issues fixed:
Evaluation order: Check
Modifier.isStatic()first — static nested classes support codegen regardless of enclosing class visibility, so there is no need to load it.Error handling: When
getEnclosingClass()throws, returnfalseinstead of re-throwing asRuntimeException. If the enclosing class cannot be loaded, the class cannot function as a non-static inner class in this context, so codegen is simply not applicable.Reproduction scenario
Outer.InnerRecordis a static nested recordInnerRecordis loaded by ClassLoader AOuteris only visible to ClassLoader BgetEnclosingClass()→NoClassDefFoundError→RuntimeException(crash)isStatic()→true(correct result, no class loading)