Files
javademos/unsafe-migration/README.md
T
2026-11-06 21:43:48 +05:30

25 lines
2.1 KiB
Markdown

# unsafe-migration — leaving sun.misc.Unsafe (JEP 471 / JEP 498)
Companion code for the ankurm.com article **sun.misc.Unsafe Memory Access Is Going Away: Find and Fix It in Your Dependencies**
(<https://ankurm.com/sun-misc-unsafe-memory-access-deprecation-migration/>). The explanation lives in the article; this folder holds the sources and the transcripts they produced.
```bash
JDK25=/path/to/jdk-25 JDK21=/path/to/jdk-21 [JDK26=/path JDK27=/path] ./run.sh # regenerates output/*.txt (a few minutes; steps 06-08 need Maven Central)
```
Tested on Temurin 25.0.5+8 (the baseline), Ubuntu OpenJDK 21.0.12.1 (the "before"), Temurin 26.0.2.1+1 and 27.0.1.0.2+1 (default-behaviour check only). Plain `javac`/`java`, no Maven.
| Path | What it is |
|---|---|
| `src/UnsafeFieldCounter.java`, `VarHandleFieldCounter.java`, `FieldCounter.java` | a CAS field: Unsafe before, VarHandle after |
| `src/UnsafeOffHeapLongs.java`, `FfmOffHeapLongs.java`, `OffHeapLongs.java` | an off-heap `long` array: `allocateMemory` before, `Arena`/`MemorySegment` after |
| `src/UnsafeBytes.java`, `ViewBytes.java` | long access inside a `byte[]`: bimodal `getLong(Object,long)` before, byte-array-view VarHandle after |
| `src/Equivalence.java` | the before/after check (170 checks; 67 with `--new-only` under `deny`) |
| `broken/Probe.java` | the smallest Unsafe memory access; what every flag value does to it |
| `tools/ScanJar.java` | bytecode scan for removal-deprecated Unsafe calls (java.lang.classfile, JDK 24+) |
| `tools/NettyProbe.java`, `GuavaProbe.java`, `ListUnsafe.java`, `Prop.java` | one real Netty buffer; three Guava features; the JDK's own method list; the flag as a system property |
| `scripts/scan-libs.sh`, `scripts/run-all.sh` | download and scan 17 jars (sha1-checked; retries because the proxy here truncated transfers now and then); regenerate everything |
| `output/01`..`12` | captured transcripts: methods per JDK, javac warnings, flag values, other JDKs, jdeps, library scan, Netty, equivalence, JFR, scan callers, Guava |
Timing is not measured anywhere in this module; there is no benchmark.