Skip to content

Add built-in dispatch creator mode (fastjson2.creator=dispatch) - #7983

Open
zhengjiezxxy wants to merge 1 commit into
alibaba:mainfrom
zhengjiezxxy:feature/creator-dispatch
Open

zhengjiezxxy wants to merge 1 commit into
alibaba:mainfrom
zhengjiezxxy:feature/creator-dispatch

Conversation

@zhengjiezxxy

Copy link
Copy Markdown

Add ObjectWriterCreatorAsmDispatch and ObjectReaderCreatorAsmDispatch, which create the ASM writer/reader with the ClassLoader of the target class.

  • If the ClassLoader of the default ObjectWriterCreatorASM / ObjectReaderCreatorASM can load the target class, the default creator is used, so the common case is unchanged.
  • Otherwise a creator is created per ClassLoader and cached, so generated classes always live in a DynamicClassLoader that can resolve the target class. This fixes ASM creation for classes from other ClassLoaders (OSGi, multiple web apps, hot deployment).
  • cleanup(ClassLoader) / cleanupAll() release cached creators, so a ClassLoader can be garbage collected after undeployment.

Enable with -Dfastjson2.creator=dispatch or fastjson2.creator=dispatch in fastjson2.properties. Tests cover creator resolution, caching, cleanup and serialization/deserialization of a class loaded by an external ClassLoader.

中文说明

新增内置 dispatch 模式(fastjson2.creator=dispatch),新增
ObjectWriterCreatorAsmDispatch 与 ObjectReaderCreatorAsmDispatch,按照目标类的 ClassLoader 分派读写器的创建。

  • 如果默认 ObjectWriterCreatorASM / ObjectReaderCreatorASM 的 ClassLoader 能找到目标类, 直接使用默认 creator,常规场景行为不变。
  • 否则按 ClassLoader 创建并缓存独立的 creator,保证生成的类位于能解析目标类的 DynamicClassLoader 中,修复其他 ClassLoader 加载的类(OSGi、同 JVM 多 Web 应用、 热部署等)无法使用 ASM 的问题。
  • 提供 cleanup(ClassLoader) / cleanupAll() 外部清理接口,卸载后 ClassLoader 可被回收。

通过 -Dfastjson2.creator=dispatch 或 fastjson2.properties 中的 fastjson2.creator=dispatch 启用。测试覆盖 creator 解析、缓存、清理以及外部 ClassLoader 加载类的序列化/反序列化。

What this PR does / why we need it?

Summary of your change

Please indicate you've done the following:

  • Made sure tests are passing and test coverage is added if needed.
  • Made sure commit message follow the rule of Conventional Commits specification.
  • Considered the docs impact and opened a new docs issue or PR with docs changes if needed.

Add ObjectWriterCreatorAsmDispatch and ObjectReaderCreatorAsmDispatch, which
create the ASM writer/reader with the ClassLoader of the target class.

- If the ClassLoader of the default ObjectWriterCreatorASM / ObjectReaderCreatorASM
  can load the target class, the default creator is used, so the common case is
  unchanged.
- Otherwise a creator is created per ClassLoader and cached, so generated classes
  always live in a DynamicClassLoader that can resolve the target class. This fixes
  ASM creation for classes from other ClassLoaders (OSGi, multiple web apps, hot
  deployment).
- cleanup(ClassLoader) / cleanupAll() release cached creators, so a ClassLoader can
  be garbage collected after undeployment.

Enable with -Dfastjson2.creator=dispatch or fastjson2.creator=dispatch in
fastjson2.properties. Tests cover creator resolution, caching, cleanup and
serialization/deserialization of a class loaded by an external ClassLoader.

<details>
<summary>中文说明</summary>

新增内置 dispatch 模式(fastjson2.creator=dispatch),新增
ObjectWriterCreatorAsmDispatch 与 ObjectReaderCreatorAsmDispatch,按照目标类的
ClassLoader 分派读写器的创建。

- 如果默认 ObjectWriterCreatorASM / ObjectReaderCreatorASM 的 ClassLoader 能找到目标类,
  直接使用默认 creator,常规场景行为不变。
- 否则按 ClassLoader 创建并缓存独立的 creator,保证生成的类位于能解析目标类的
  DynamicClassLoader 中,修复其他 ClassLoader 加载的类(OSGi、同 JVM 多 Web 应用、
  热部署等)无法使用 ASM 的问题。
- 提供 cleanup(ClassLoader) / cleanupAll() 外部清理接口,卸载后 ClassLoader 可被回收。

通过 -Dfastjson2.creator=dispatch 或 fastjson2.properties 中的
fastjson2.creator=dispatch 启用。测试覆盖 creator 解析、缓存、清理以及外部
ClassLoader 加载类的序列化/反序列化。
</details>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

This branch has not been deployed

No deployments
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.

3 participants