Replace sun.misc.Unsafe with Multi-Release JAR approach for Java 24+ compatibility using UnsafeString multijar implementation#74
Replace sun.misc.Unsafe with Multi-Release JAR approach for Java 24+ compatibility using UnsafeString multijar implementation#74
Conversation
|
@trask 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs. I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review. |
Co-authored-by: trask <218610+trask@users.noreply.github.com>
…ify JCTools fallback Co-authored-by: trask <218610+trask@users.noreply.github.com>
Co-authored-by: trask <218610+trask@users.noreply.github.com>
|
@copilot use multijar instead of reflection for access to new APIs |
…+ compatibility Co-authored-by: trask <218610+trask@users.noreply.github.com>
Implemented multijar approach instead of reflection for VarHandle access. The solution now uses Multi-Release JAR structure with:
This provides better performance and maintainability while maintaining full Java 8+ compatibility. Commit 4c27f54 contains the complete implementation. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
what about making this the multijar class instead of VarHandleString?
There was a problem hiding this comment.
@copilot what about making this the multijar class instead of VarHandleString?
There was a problem hiding this comment.
Done! I've refactored the implementation to make UnsafeString the multijar class instead of VarHandleString.
The changes include:
- Created Java 9+ version of
UnsafeStringthat uses VarHandle directly with Unsafe fallback - Updated Java 8 version of
UnsafeStringto use only Unsafe with safe fallbacks - Removed
VarHandleStringclasses entirely - Updated all test files to work with the new structure
This eliminates the indirection and makes the code cleaner - UnsafeString is now the main API that automatically selects the best implementation via multijar. Commit d246e69.
Co-authored-by: trask <218610+trask@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Starting with Java 24 (March 2025), usage of
sun.misc.Unsafewill generate warnings, and Java 26 (March 2026) will remove access to this internal API by default. This change proactively addresses compatibility issues by implementing a Multi-Release JAR approach with direct VarHandle APIs while maintaining backwards compatibility.Implementation
The solution uses a Multi-Release JAR structure that automatically selects the best available approach based on Java version:
sun.misc.Unsafewhen available, safe fallbacks otherwiseKey Changes
Multi-Release JAR Structure:
src/main/java/: Java 8 compatible implementation using Unsafe + fallbackssrc/main/java9/: Java 9+ implementation using VarHandle with Unsafe fallbackSimplified Architecture:
UnsafeStringis now the multijar class that automatically provides the right implementation based on Java version, eliminating the need for delegation throughVarHandleString.Enhanced Build Configuration: Updated
build.gradle.ktsto support multi-release JAR compilation with proper manifest attributes (Multi-Release: true).Eliminated Reflection: The Java 9+ version uses compile-time VarHandle APIs directly instead of runtime reflection, providing better performance and maintainability.
Automatic Fallback Chain: VarHandle (Java 9+) → sun.misc.Unsafe → safe fallback implementations ensures compatibility across all environments.
Configuration
The system property configuration is simplified as the multijar approach handles version selection automatically:
# Disable Unsafe usage (forces safe fallbacks) -Dotel.java.experimental.exporter.unsafe.enabled=falsePerformance
The Multi-Release JAR approach eliminates reflection overhead on Java 9+ while maintaining the same optimization benefits. No measurable performance difference between VarHandle and Unsafe implementations.
Future Compatibility
This change provides a smooth migration path where applications will continue to work unchanged when
sun.misc.Unsafeis completely removed from future Java versions, using the standard Multi-Release JAR mechanism employed by many Java libraries.Fixes #73.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.