forked from spotbugs/spotbugs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not disable the security manager on Java 17 VMs and newer as it is…
… deprecated for removal. This fixes spotbugs#1579. If needed, disabling the security manager can still be requested on Java 17 and newer by setting the 'edu.umd.cs.findbugs.securityManagerDisabled' property to 'false'. Using this property also allows to disable this functionality on older VMs.
- Loading branch information
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
spotbugs/src/main/java/edu/umd/cs/findbugs/util/SecurityManagerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package edu.umd.cs.findbugs.util; | ||
|
||
/** | ||
* Since Java 17, the security manager is deprecated for removal and invoking related methods | ||
* causes a warning to be printed to the console. This intermediate disables use security | ||
* manager-related APIs on Java 17 or later, unless using the security manager is explicitly | ||
* configured by setting the <i>edu.umd.cs.findbugs.securityManagerDisabled</i> property. | ||
*/ | ||
public class SecurityManagerHandler { | ||
|
||
/** | ||
* Determines if the security manager is used by SpotBugs. | ||
*/ | ||
public static boolean SECURITY_MANAGER_DISABLED; | ||
|
||
static { | ||
boolean securityManagerDisabled; | ||
try { | ||
String property = System.getProperty("edu.umd.cs.findbugs.securityManagerDisabled"); | ||
if (property != null) { | ||
securityManagerDisabled = Boolean.parseBoolean(property); | ||
} else { | ||
String version = System.getProperty("java.version"); | ||
if (version.startsWith("1.")) { | ||
version = version.substring(2, 3); | ||
} else { | ||
int index = version.indexOf("."); | ||
if (index != -1) { | ||
version = version.substring(0, index); | ||
} | ||
} | ||
securityManagerDisabled = Integer.parseInt(version) > 16; | ||
} | ||
} catch (Throwable ignored) { | ||
securityManagerDisabled = false; | ||
} | ||
SECURITY_MANAGER_DISABLED = securityManagerDisabled; | ||
} | ||
|
||
/** | ||
* Disables the security manager by setting {@link System#setSecurityManager(SecurityManager)} | ||
* to {@code null}. | ||
*/ | ||
public static void disableSecurityManager() { | ||
if (SECURITY_MANAGER_DISABLED) { | ||
return; | ||
} | ||
doDisableSecurityManager(); | ||
} | ||
|
||
/** | ||
* This method is a safeguard for running this library on a JVM that might no longer include | ||
* the security manager API after removal. As the JVM verifies methods lazily, and since this | ||
* method will never be invoked, validation of this method with a missing type can never fail. | ||
*/ | ||
private static void doDisableSecurityManager() { | ||
System.setSecurityManager(null); | ||
} | ||
} |