alibaba / alibaba/fastjson2

[BUG] JDK 25 下 fastjson2 最新版仍触发 sun.misc.Unsafe::arrayBaseOffset 废弃警告

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

Description

## 环境信息
- **fastjson2 版本**:2.0.61(当前最新稳定版)
- **JDK 版本**:JDK 25(Eclipse Adoptium 25.0.2)
- **操作系统**:Windows 11 / Linux(可按实际填写)
- **构建工具**:Maven / Gradle(可按实际填写)

## 问题描述
在 **JDK 25** 环境下,即使将 fastjson2 升级到 **最新稳定版 2.0.61**,运行时依然会持续打印 `sun.misc.Unsafe` 相关的废弃警告,具体日志如下:
```
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 (file:/xxx/fastjson2-2.0.61.jar)
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
```
该警告在 JDK 23+ 开始强制输出,JDK 25 下依然存在,且**升级 fastjson2 到最新版无法解决**。

## 重现步骤
1. 配置项目依赖为 fastjson2 2.0.61:
```xml

com.alibaba
fastjson2
2.0.61

```
2. 使用 JDK 25 编译并运行项目
3. 执行任意 JSON 序列化/反序列化操作(如 `JSON.toJSONString`、`JSON.parseObject`)
4. 控制台输出上述 `Unsafe` 废弃警告

## 预期行为
- fastjson2 应在 JDK 23+ 环境下**完全移除对 `sun.misc.Unsafe` 内存访问方法的依赖**
- 替换为 JDK 官方推荐的 `VarHandle` / `MethodHandle` 等标准 API,消除废弃警告
- 兼容 JDK 8 ~ JDK 25 全版本,不影响性能与功能

## 实际行为
- 最新版 fastjson2 仍在 `com.alibaba.fastjson2.util.JDKUtils` 中直接调用 `Unsafe.arrayBaseOffset`
- JDK 25 运行时持续输出废弃警告,影响日志整洁度,且未来 JDK 版本可能直接移除该方法导致报错

## 补充信息
1. 该问题在 fastjson2 2.0.40 ~ 2.0.61 所有版本均存在,与版本无关
2. JDK 官方已明确:`sun.misc.Unsafe` 内存访问方法在 JDK 23 标记为 **终态废弃**,未来版本将彻底移除(JEP 8342077)
3. fastjson2 官方 roadmap 提到 2.1 版本计划用 `VarHandle` 替代 `Unsafe`,但当前 2.0.x 维护版未修复此兼容性问题

## 建议解决方案
- 在 `JDKUtils` 中增加 JDK 版本判断:
- JDK 8 ~ 22:保留 `Unsafe` 调用以兼容
- JDK 23+:使用 `java.lang.invoke.VarHandle` 或 `java.lang.foreign` API 替代 `Unsafe.arrayBaseOffset`
- 发布 2.0.x 维护版修复该警告,无需等待 2.1 大版本

Contributor guide

Open the contributing guide

Research direction

Start by reading com.alibaba.fastjson2.util.JDKUtils and reproduce the warning with the JSON.toJSONString or JSON.parseObject steps under JDK 25. Check how the existing implementation behaves across JDK 8 through 25; done means the deprecated Unsafe warning is eliminated on JDK 23+ while serialization and deserialization remain compatible and functional.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.