@@ -65,13 +65,10 @@ public final class LoaderUtil {
6565 } catch (final SecurityException ignored ) {
6666 try {
6767 // let's see if we can obtain that permission
68- AccessController .doPrivileged (
69- (PrivilegedAction <Void >) () -> {
70- AccessController .checkPermission (GET_CLASS_LOADER );
71- return null ;
72- },
73- null ,
74- GET_CLASS_LOADER );
68+ runPrivilegedActionWithGetClassLoaderPermission ((PrivilegedAction <Void >) () -> {
69+ AccessController .checkPermission (GET_CLASS_LOADER );
70+ return null ;
71+ });
7572 getClassLoaderDisabled = false ;
7673 } catch (final SecurityException ignore ) {
7774 // no chance
@@ -108,7 +105,7 @@ public static ClassLoader getClassLoader(final Class<?> class1, final Class<?> c
108105 }
109106 return isChild (loader1 , loader2 ) ? loader1 : loader2 ;
110107 };
111- return AccessController . doPrivileged (action , null , GET_CLASS_LOADER );
108+ return runActionInvolvingGetClassLoaderPermission (action );
112109 }
113110
114111 /**
@@ -143,22 +140,29 @@ private static boolean isChild(final ClassLoader loader1, final ClassLoader load
143140 * @return the current thread's ClassLoader, a fallback loader, or null if no fallback can be determined
144141 */
145142 public static ClassLoader getThreadContextClassLoader () {
146- if (GET_CLASS_LOADER_DISABLED ) {
147- // we can at least get this class's ClassLoader regardless of security context
148- // however, if this is null, there's really no option left at this point
149- try {
150- return getThisClassLoader ();
151- } catch (final SecurityException ignored ) {
152- return null ;
153- }
143+ try {
144+ return GET_CLASS_LOADER_DISABLED
145+ ? getThisClassLoader ()
146+ : runActionInvolvingGetClassLoaderPermission (TCCL_GETTER );
147+ } catch (final SecurityException ignored ) {
148+ return null ;
154149 }
155- return AccessController .doPrivileged (TCCL_GETTER , null , GET_CLASS_LOADER );
156150 }
157151
158152 private static ClassLoader getThisClassLoader () {
159153 return LoaderUtil .class .getClassLoader ();
160154 }
161155
156+ private static <T > T runActionInvolvingGetClassLoaderPermission (final PrivilegedAction <T > action ) {
157+ return System .getSecurityManager () != null
158+ ? runPrivilegedActionWithGetClassLoaderPermission (action )
159+ : action .run ();
160+ }
161+
162+ private static <T > T runPrivilegedActionWithGetClassLoaderPermission (final PrivilegedAction <T > action ) {
163+ return AccessController .doPrivileged (action , null , GET_CLASS_LOADER );
164+ }
165+
162166 private static class ThreadContextClassLoaderGetter implements PrivilegedAction <ClassLoader > {
163167 @ Override
164168 public ClassLoader run () {
0 commit comments