public class LegacyJava extends Object
For pre-17 this class provides access to the expected SecurityManager and AccessController. For 17+ the getSecurityManager() will always return 'null' and the doPrivileged() methods will simply execute the lambda argument without any security checks.
Solution for AccessControl: This class uses reflection to detect whether AccessControl is deprecated. If it is deprecated we provide dummy implementation of 'doPrivileged'. If it is not deprecated we use the original implementation. Implementations are used by finding the desired methods and store them in a static field in LegacyJava. Whenever we need to call a method we can use reflection again to call the stored method in LegacyJava.
Please note that the call to the stored method needs to happen from the original calling class. We cannot provide such a generic doPrivileged() in LegacyJava because it would be accessible from other classes and thus allow *any* caller to execute *any* code with doPrivileged() with the security context of LegacyJava. For the same reason the doPrivileged() implementations in the calling classes must be *private*.
Modifier and Type | Class and Description |
---|---|
static class |
LegacyJava.SecurityManager
The SecurityManager is only instantiated if it is not deprecated/removed in the Java API.
|
Modifier and Type | Field and Description |
---|---|
static Method |
doPrivilegedAction |
static Method |
doPrivilegedExceptionAction |
Constructor and Description |
---|
LegacyJava() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
doPrivileged(PrivilegedAction<T> privilegedAction)
This is a replacement for the old Java AccessControl.doPrivileged().
|
static <T> T |
doPrivileged(PrivilegedExceptionAction<T> privilegedAction)
This is a replacement for the old Java AccessControl.doPrivileged().
|
static LegacyJava.SecurityManager |
getSecurityManager() |
static boolean |
isSecurityManagerDeprecated() |
public static final Method doPrivilegedAction
public static final Method doPrivilegedExceptionAction
public static LegacyJava.SecurityManager getSecurityManager()
public static boolean isSecurityManagerDeprecated()
public static <T> T doPrivileged(PrivilegedAction<T> privilegedAction)
T
- Return type.privilegedAction
- The action to execute.public static <T> T doPrivileged(PrivilegedExceptionAction<T> privilegedAction) throws PrivilegedActionException
T
- Return type.privilegedAction
- The action to execute.PrivilegedActionException
- Never thrown.Copyright © 2005–2022 The Apache Software Foundation. All rights reserved.