Skip to content

Commit 48fea0b

Browse files
committed
ClassUtils.isCacheSafe defensively catches SecurityException (for Google App Engine compatibility)
Issue: SPR-12002
1 parent a53987a commit 48fea0b

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

spring-core/src/main/java/org/springframework/util/ClassUtils.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
2424
import java.lang.reflect.Proxy;
25-
import java.security.AccessControlException;
2625
import java.util.Arrays;
2726
import java.util.Collection;
2827
import java.util.Collections;
@@ -365,21 +364,27 @@ public static Class<?> getUserClass(Class<?> clazz) {
365364
*/
366365
public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
367366
Assert.notNull(clazz, "Class must not be null");
368-
ClassLoader target = clazz.getClassLoader();
369-
if (target == null) {
370-
return true;
371-
}
372-
ClassLoader cur = classLoader;
373-
if (cur == target) {
374-
return true;
375-
}
376-
while (cur != null) {
377-
cur = cur.getParent();
367+
try {
368+
ClassLoader target = clazz.getClassLoader();
369+
if (target == null) {
370+
return true;
371+
}
372+
ClassLoader cur = classLoader;
378373
if (cur == target) {
379374
return true;
380375
}
376+
while (cur != null) {
377+
cur = cur.getParent();
378+
if (cur == target) {
379+
return true;
380+
}
381+
}
382+
return false;
383+
}
384+
catch (SecurityException ex) {
385+
// Probably from the system ClassLoader - let's consider it safe.
386+
return true;
381387
}
382-
return false;
383388
}
384389

385390

@@ -768,7 +773,7 @@ public static Method getMostSpecificMethod(Method method, Class<?> targetClass)
768773
return (specificMethod != null ? specificMethod : method);
769774
}
770775
}
771-
catch (AccessControlException ex) {
776+
catch (SecurityException ex) {
772777
// Security settings are disallowing reflective access; fall back to 'method' below.
773778
}
774779
}

0 commit comments

Comments
 (0)