Skip to content

Commit 619e3a9

Browse files
committed
ClassUtils.isCacheSafe defensively catches SecurityException (for Google App Engine compatibility)
Issue: SPR-12002 (cherry picked from commit 48fea0b)
1 parent a3ebf13 commit 619e3a9

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

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

Lines changed: 19 additions & 14 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;
@@ -215,7 +214,7 @@ public static Class<?> forName(String name) throws ClassNotFoundException, Linka
215214

216215
/**
217216
* Replacement for {@code Class.forName()} that also returns Class instances
218-
* for primitives (e.g."int") and array class names (e.g. "String[]").
217+
* for primitives (e.g. "int") and array class names (e.g. "String[]").
219218
* Furthermore, it is also capable of resolving inner class names in Java source
220219
* style (e.g. "java.lang.Thread.State" instead of "java.lang.Thread$State").
221220
* @param name the name of the Class
@@ -397,21 +396,27 @@ public static Class<?> getUserClass(Class<?> clazz) {
397396
*/
398397
public static boolean isCacheSafe(Class<?> clazz, ClassLoader classLoader) {
399398
Assert.notNull(clazz, "Class must not be null");
400-
ClassLoader target = clazz.getClassLoader();
401-
if (target == null) {
402-
return true;
403-
}
404-
ClassLoader cur = classLoader;
405-
if (cur == target) {
406-
return true;
407-
}
408-
while (cur != null) {
409-
cur = cur.getParent();
399+
try {
400+
ClassLoader target = clazz.getClassLoader();
401+
if (target == null) {
402+
return true;
403+
}
404+
ClassLoader cur = classLoader;
410405
if (cur == target) {
411406
return true;
412407
}
408+
while (cur != null) {
409+
cur = cur.getParent();
410+
if (cur == target) {
411+
return true;
412+
}
413+
}
414+
return false;
415+
}
416+
catch (SecurityException ex) {
417+
// Probably from the system ClassLoader - let's consider it safe.
418+
return true;
413419
}
414-
return false;
415420
}
416421

417422

@@ -800,7 +805,7 @@ public static Method getMostSpecificMethod(Method method, Class<?> targetClass)
800805
return (specificMethod != null ? specificMethod : method);
801806
}
802807
}
803-
catch (AccessControlException ex) {
808+
catch (SecurityException ex) {
804809
// Security settings are disallowing reflective access; fall back to 'method' below.
805810
}
806811
}

0 commit comments

Comments
 (0)