Skip to content

Commit 5821ccc

Browse files
committed
Delay disabling of unsafe by default to Java 26 and improve on error message.
1 parent 9d7d4fa commit 5821ccc

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/loading/ClassInjector.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,11 @@ protected UsingUnsafeInjection(Object accessor,
989989
protected static Initializable make() throws Exception {
990990
if (Boolean.parseBoolean(java.lang.System.getProperty(UsingUnsafe.SAFE_PROPERTY, Boolean.toString(ClassFileVersion
991991
.ofThisVm()
992-
.isAtLeast(ClassFileVersion.JAVA_V25) || GraalImageCode.getCurrent().isDefined())))) {
993-
return new Initializable.Unavailable("Use of Unsafe was disabled by system property");
992+
.isAtLeast(ClassFileVersion.JAVA_V26) || GraalImageCode.getCurrent().isDefined())))) {
993+
return new Initializable.Unavailable("As of Java 26, using Unsafe is disabled by default, set "
994+
+ UsingUnsafe.SAFE_PROPERTY + " to true if you want to use the JVM's internal unsafe API "
995+
+ "even though it will become unsupported in the future and should be replaced by injection "
996+
+ "using method handles: " + UsingLookup.class.getName());
994997
}
995998
Class<?> unsafe = Class.forName("sun.misc.Unsafe");
996999
Field theUnsafe = unsafe.getDeclaredField("theUnsafe");
@@ -1253,8 +1256,13 @@ protected UsingUnsafeOverride(Method findLoadedClass,
12531256
*/
12541257
@SuppressFBWarnings(value = "DP_DO_INSIDE_DO_PRIVILEGED", justification = "Assuring privilege is explicit user responsibility.")
12551258
protected static Initializable make() throws Exception {
1256-
if (Boolean.parseBoolean(java.lang.System.getProperty(UsingUnsafe.SAFE_PROPERTY, Boolean.toString(GraalImageCode.getCurrent().isDefined())))) {
1257-
return new Initializable.Unavailable("Use of Unsafe was disabled by system property");
1259+
if (Boolean.parseBoolean(java.lang.System.getProperty(UsingUnsafe.SAFE_PROPERTY, Boolean.toString(ClassFileVersion
1260+
.ofThisVm()
1261+
.isAtLeast(ClassFileVersion.JAVA_V26) || GraalImageCode.getCurrent().isDefined())))) {
1262+
return new Initializable.Unavailable("As of Java 26, using Unsafe is disabled by default, set "
1263+
+ UsingUnsafe.SAFE_PROPERTY + " to true if you want to use the JVM's internal unsafe API "
1264+
+ "even though it will become unsupported in the future and should be replaced by injection "
1265+
+ "using method handles: " + UsingLookup.class.getName());
12581266
}
12591267
Class<?> unsafeType = Class.forName("sun.misc.Unsafe");
12601268
Field theUnsafe = unsafeType.getDeclaredField("theUnsafe");
@@ -2021,6 +2029,14 @@ enum CreationAction implements PrivilegedAction<Initializable> {
20212029
*/
20222030
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION", justification = "Exception should not be rethrown but trigger a fallback.")
20232031
public Initializable run() {
2032+
if (Boolean.parseBoolean(java.lang.System.getProperty(UsingUnsafe.SAFE_PROPERTY, Boolean.toString(ClassFileVersion
2033+
.ofThisVm()
2034+
.isAtLeast(ClassFileVersion.JAVA_V26) || GraalImageCode.getCurrent().isDefined())))) {
2035+
return new Unavailable("As of Java 26, using Unsafe is disabled by default, set "
2036+
+ UsingUnsafe.SAFE_PROPERTY + " to true if you want to use the JVM's internal unsafe API "
2037+
+ "even though it will become unsupported in the future and should be replaced by injection "
2038+
+ "using method handles: " + UsingLookup.class.getName());
2039+
}
20242040
if (Boolean.parseBoolean(java.lang.System.getProperty(SAFE_PROPERTY, Boolean.toString(GraalImageCode.getCurrent().isDefined())))) {
20252041
return new Unavailable("Use of Unsafe was disabled by system property");
20262042
}

0 commit comments

Comments
 (0)