fix: ClassFormatError when generating writers for array-typed fields (#4009) - #4014
Merged
Conversation
Member
Author
|
No issues found. LGTM! ✅ — gpt-5.5 via Qwen Code /review |
wenshao
commented
Jul 31, 2026
wenshao
left a comment
Member
Author
There was a problem hiding this comment.
Reviewed. Suggestions are inline.
— qwen3.8-max-preview via Qwen Code /review
…s, for issue #4009 When calling JSON.toJSON() on an object with a Class[] (or any array-typed) field, the ASM writer generated class names like "OWG_5_0_Class[]", which are illegal Java class names, so defineClass threw ClassFormatError. Sanitize the simple name embedded in generated class names: array notation becomes a suffix ("Class[]" -> "ClassArray") and any other character invalid in a class name becomes an underscore. sanitizeClassName lives in ASMUtils, which both creators already static-import, so the writer and reader cannot drift apart.
wenshao
force-pushed
the
fix/issue-4009-classformaterror
branch
from
August 2, 2026 08:29
64e769a to
272d1ca
Compare
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.
Problem
JSON.toJSON(bean)throwsClassFormatErrorwhen the bean has an array-typed field:Root cause
ObjectWriterCreatorASMembedsobjectClass.getSimpleName()in the name of the class it generates:For an array type the simple name ends in
[]—Class[]— and[is not legal in a class name, sodefineClassrejects the generated bytes.Fix
Sanitize the simple name before embedding it: array notation becomes a suffix (
Class[]→ClassArray), and any other character that is not valid in a class name becomes an underscore.The helper lives in
com.alibaba.fastjson2.internal.asm.ASMUtils, which bothObjectWriterCreatorASMandObjectReaderCreatorASMalready static-import, so the two cannot drift apart:Scope
The writer is the only path that could produce an illegal name. The reader call sites are defensive symmetry, verified rather than assumed:
ObjectReaderCreatorASMnever generates a class for an array type — it returnsObjectReaderAdapter, so theORG_path is not reached. Its other call site (VBACG_/VCACG_increateValueConsumer0) bails out earlier still, since it requires a default constructor and a public class, and an array type has neither.Tests
Issue4009Test— serialization and round-trip forClass[]andint[][]fields.ASMUtilsTest— unit coverage forsanitizeClassName: null/empty, unchanged names, arrays includingint[][], special characters, and the combinedMy$Class[].Verification
Revert-probe — removing
sanitizeClassNamefromObjectWriterCreatorASMmakes the integration test fail with the exact error from #4009:Full suites green locally on JDK 26 — core 7965, extension 56, fastjson1-compatible 1355, kotlin 36, safemode-test 2 — and core again with
-Dfastjson2.creator=reflect.Closes #4009
中文说明
问题
Bean 中含有数组类型字段时,
JSON.toJSON(bean)抛ClassFormatError:根因
ObjectWriterCreatorASM会把objectClass.getSimpleName()拼进它生成的类名:数组类型的 simple name 以
[]结尾(Class[]),而[在类名中非法,defineClass因此拒绝生成的字节码。修复
拼接前先对 simple name 做净化:数组记法转成后缀(
Class[]→ClassArray),其余在类名中非法的字符替换为下划线。方法放在
com.alibaba.fastjson2.internal.asm.ASMUtils——ObjectWriterCreatorASM和ObjectReaderCreatorASM本来就 static import 了它,两边不会各自演化:影响范围
只有 writer 会产生非法类名。reader 那两处是防御性对称,这一点是实测确认的,不是推断:
ObjectReaderCreatorASM对数组类型根本不生成类,直接返回ObjectReaderAdapter,走不到ORG_路径。另一处(createValueConsumer0里的VBACG_/VCACG_)退出得更早:它要求有默认构造器且类是 public,数组类型两条都不满足。测试
Issue4009Test——Class[]与int[][]字段的序列化及 round-trip。ASMUtilsTest——sanitizeClassName的单测:null/空、无需改写的名字、数组(含int[][])、特殊字符,以及组合场景My$Class[]。验证
Revert-probe:把
sanitizeClassName从ObjectWriterCreatorASM移除后,集成测试以 #4009 中一模一样的错误失败:本地 JDK 26 全量通过 —— core 7965、extension 56、fastjson1-compatible 1355、kotlin 36、safemode-test 2;
-Dfastjson2.creator=reflect下 core 同样通过。