5757
5858
5959public class AccessController {
60+ private static final boolean SUPPORTS_SECURITY_MANAGER = Runtime .version ().feature () < 24 ;
6061
6162 /**
6263 * Performs the specified <code>PrivilegedAction</code> with privileges
@@ -71,8 +72,7 @@ public class AccessController {
7172 * @see #doPrivileged(PrivilegedExceptionAction)
7273 */
7374 public static <T > T doPrivileged (PrivilegedAction <T > action ) {
74- SecurityManager sm = System .getSecurityManager ();
75- if (sm == null ) {
75+ if (!SUPPORTS_SECURITY_MANAGER || System .getSecurityManager () == null ) {
7676 return (action .run ());
7777 } else {
7878 return java .security .AccessController .doPrivileged (action );
@@ -101,8 +101,7 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
101101 * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
102102 */
103103 public static <T > T doPrivileged (PrivilegedAction <T > action , AccessControlContext context ) {
104- SecurityManager sm = System .getSecurityManager ();
105- if (sm == null ) {
104+ if (!SUPPORTS_SECURITY_MANAGER || System .getSecurityManager () == null ) {
106105 return action .run ();
107106 } else {
108107 return java .security .AccessController .doPrivileged (action , context );
@@ -126,8 +125,7 @@ public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContex
126125 */
127126 public static <T > T doPrivileged (PrivilegedExceptionAction <T > action )
128127 throws PrivilegedActionException {
129- SecurityManager sm = System .getSecurityManager ();
130- if (sm == null ) {
128+ if (!SUPPORTS_SECURITY_MANAGER || System .getSecurityManager () == null ) {
131129 try {
132130 return action .run ();
133131 } catch (java .lang .RuntimeException e ) {
@@ -167,8 +165,7 @@ public static <T> T doPrivileged(PrivilegedExceptionAction<T> action,
167165 AccessControlContext context )
168166 throws PrivilegedActionException {
169167
170- SecurityManager sm = System .getSecurityManager ();
171- if (sm == null ) {
168+ if (!SUPPORTS_SECURITY_MANAGER || System .getSecurityManager () == null ) {
172169 try {
173170 return action .run ();
174171 } catch (java .lang .RuntimeException e ) {
@@ -206,7 +203,9 @@ public static AccessControlContext getContext() {
206203 * is not permitted, based on the current security policy.
207204 */
208205 public static void checkPermission (Permission perm ) throws AccessControlException {
209- java .security .AccessController .checkPermission (perm );
206+ if (SUPPORTS_SECURITY_MANAGER ) {
207+ java .security .AccessController .checkPermission (perm );
208+ }
210209 }
211210
212211 /**
0 commit comments