3535 * accessing system resources (ie, read/write files, opening ports, and etc).
3636 * <p/>
3737 * This class provides a consistent security model across Java versions by
38- * always using doPrivileged(), ensuring proper privilege elevation regardless
38+ * always using doPrivileged(), if it is available, ensuring proper privilege elevation regardless
3939 * of SecurityManager presence (which was deprecated in Java 17 and removed in Java 21).
4040 * <p/>
4141 * Note: This utility should be used properly, otherwise might introduce
5656 * }
5757 * </code>
5858 */
59-
60-
6159public class AccessController {
60+ private static final boolean SUPPORTS_SECURITY_MANAGER = Runtime .version ().feature () < 24 ;
6261
6362 /**
6463 * Performs the specified <code>PrivilegedAction</code> with privileges
6564 * enabled. This method always uses doPrivileged for security consistency
66- * across Java versions.
65+ * across Java versions, if it is available .
6766 * <p/>
6867 * If the action's <code>run</code> method throws an (unchecked) exception,
6968 * it will propagate through this method.
@@ -74,7 +73,11 @@ public class AccessController {
7473 * @see #doPrivileged(PrivilegedExceptionAction)
7574 */
7675 public static <T > T doPrivileged (PrivilegedAction <T > action ) {
77- return java .security .AccessController .doPrivileged (action );
76+ if (!SUPPORTS_SECURITY_MANAGER ) {
77+ return (action .run ());
78+ } else {
79+ return java .security .AccessController .doPrivileged (action );
80+ }
7881 }
7982
8083
@@ -97,7 +100,11 @@ public static <T> T doPrivileged(PrivilegedAction<T> action) {
97100 * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
98101 */
99102 public static <T > T doPrivileged (PrivilegedAction <T > action , AccessControlContext context ) {
100- return java .security .AccessController .doPrivileged (action , context );
103+ if (!SUPPORTS_SECURITY_MANAGER ) {
104+ return action .run ();
105+ } else {
106+ return java .security .AccessController .doPrivileged (action , context );
107+ }
101108 }
102109
103110 /**
@@ -117,7 +124,17 @@ public static <T> T doPrivileged(PrivilegedAction<T> action, AccessControlContex
117124 */
118125 public static <T > T doPrivileged (PrivilegedExceptionAction <T > action )
119126 throws PrivilegedActionException {
120- return java .security .AccessController .doPrivileged (action );
127+ if (!SUPPORTS_SECURITY_MANAGER ) {
128+ try {
129+ return action .run ();
130+ } catch (java .lang .RuntimeException e ) {
131+ throw e ;
132+ } catch (Exception e ) {
133+ throw new PrivilegedActionException (e );
134+ }
135+ } else {
136+ return java .security .AccessController .doPrivileged (action );
137+ }
121138 }
122139
123140
@@ -146,7 +163,18 @@ public static <T> T doPrivileged(PrivilegedExceptionAction<T> action)
146163 public static <T > T doPrivileged (PrivilegedExceptionAction <T > action ,
147164 AccessControlContext context )
148165 throws PrivilegedActionException {
149- return java .security .AccessController .doPrivileged (action , context );
166+
167+ if (!SUPPORTS_SECURITY_MANAGER ) {
168+ try {
169+ return action .run ();
170+ } catch (java .lang .RuntimeException e ) {
171+ throw e ;
172+ } catch (Exception e ) {
173+ throw new PrivilegedActionException (e );
174+ }
175+ } else {
176+ return java .security .AccessController .doPrivileged (action , context );
177+ }
150178 }
151179
152180 /**
@@ -174,7 +202,9 @@ public static AccessControlContext getContext() {
174202 * is not permitted, based on the current security policy.
175203 */
176204 public static void checkPermission (Permission perm ) throws AccessControlException {
177- java .security .AccessController .checkPermission (perm );
205+ if (SUPPORTS_SECURITY_MANAGER ) {
206+ java .security .AccessController .checkPermission (perm );
207+ }
178208 }
179209
180210 /**
0 commit comments