From 619e3a995d30b7c8b54323c91f59c9acd69ac886 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 24 Jul 2014 17:46:42 +0200 Subject: [PATCH] ClassUtils.isCacheSafe defensively catches SecurityException (for Google App Engine compatibility) Issue: SPR-12002 (cherry picked from commit 48fea0b) --- .../org/springframework/util/ClassUtils.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index e3d55f358f..4bac34884e 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -22,7 +22,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.Proxy; -import java.security.AccessControlException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -215,7 +214,7 @@ public abstract class ClassUtils { /** * Replacement for {@code Class.forName()} that also returns Class instances - * for primitives (e.g."int") and array class names (e.g. "String[]"). + * for primitives (e.g. "int") and array class names (e.g. "String[]"). * Furthermore, it is also capable of resolving inner class names in Java source * style (e.g. "java.lang.Thread.State" instead of "java.lang.Thread$State"). * @param name the name of the Class @@ -397,21 +396,27 @@ public abstract class ClassUtils { */ public static boolean isCacheSafe(Class clazz, ClassLoader classLoader) { Assert.notNull(clazz, "Class must not be null"); - ClassLoader target = clazz.getClassLoader(); - if (target == null) { - return true; - } - ClassLoader cur = classLoader; - if (cur == target) { - return true; - } - while (cur != null) { - cur = cur.getParent(); + try { + ClassLoader target = clazz.getClassLoader(); + if (target == null) { + return true; + } + ClassLoader cur = classLoader; if (cur == target) { return true; } + while (cur != null) { + cur = cur.getParent(); + if (cur == target) { + return true; + } + } + return false; + } + catch (SecurityException ex) { + // Probably from the system ClassLoader - let's consider it safe. + return true; } - return false; } @@ -800,7 +805,7 @@ public abstract class ClassUtils { return (specificMethod != null ? specificMethod : method); } } - catch (AccessControlException ex) { + catch (SecurityException ex) { // Security settings are disallowing reflective access; fall back to 'method' below. } }