alibaba / alibaba/fastjson2

[BUG] JDK 25 使用 fastjson2 出现 Unsafe 警告:sun.misc.Unsafe::arrayBaseOffset 已被废弃

Open
#3,790 4 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

描述(Description)

在 JDK 25 环境下使用 fastjson2 时,会出现以下警告:

WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::arrayBaseOffset has been called by com.alibaba.fastjson2.util.JDKUtils
WARNING: Please consider reporting this to the maintainers of class com.alibaba.fastjson2.util.JDKUtils
WARNING: sun.misc.Unsafe::arrayBaseOffset will be removed in a future release

该警告来自 com.alibaba.fastjson2.util.JDKUtils,提示 sun.misc.Unsafe::arrayBaseOffset 已经废弃,并将在未来的 JDK 版本中彻底移除。

复现步骤(Steps to Reproduce)

使用 JDK 25(OpenJDK 25-ea 或 Oracle JDK 25)。

引入 fastjson2 依赖,例如 2.0.58 版本:

com.alibaba.fastjson2
fastjson2
2.0.58

执行如下简单代码:

import com.alibaba.fastjson2.JSON;

public class TestFastjson2 {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"test\"}";
JSON.parseObject(json);
}
}

运行时即可在控制台看到警告信息。

期望结果(Expected Result)

在 JDK 25 及未来版本中,不再依赖 sun.misc.Unsafe 的已废弃方法,避免出现运行时警告,确保兼容性。

实际结果(Actual Result)

运行时出现如下警告信息:

WARNING: sun.misc.Unsafe::arrayBaseOffset will be removed in a future release

环境信息(Environment)

fastjson2 版本: 2.0.58

JDK 版本: JDK 25 (early-access)

操作系统: Windows 11 x64

建议(Suggestion)

在 JDK 25+ 中替换 sun.misc.Unsafe::arrayBaseOffset 的使用方式,例如使用 VarHandle 或 JDK 官方推荐的 API。

或者在运行时检测 JDK 版本,对 JDK 25+ 提供兼容性处理。

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the warning with JDK 25 and fastjson2 2.0.58, then inspect com.alibaba.fastjson2.util.JDKUtils for the sun.misc.Unsafe::arrayBaseOffset call. Compare the available JDK-supported alternatives and verify that JSON.parseObject runs without the deprecation warning on JDK 25 while preserving compatibility with supported JDK versions.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.