8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
Reviewed-by: mchung, ahgross, darcy
--- a/jdk/src/share/classes/java/lang/Class.java Thu Jul 11 13:32:36 2013 -0700
+++ b/jdk/src/share/classes/java/lang/Class.java Mon Jul 15 14:44:50 2013 +0200
@@ -1221,9 +1221,25 @@
* type, or void,then this method returns null.
*
* @return the declaring class for this class
+ * @throws SecurityException
+ * If a security manager, <i>s</i>, is present and the caller's
+ * class loader is not the same as or an ancestor of the class
+ * loader for the declaring class and invocation of {@link
+ * SecurityManager#checkPackageAccess s.checkPackageAccess()}
+ * denies access to the package of the declaring class
* @since JDK1.1
*/
- public native Class<?> getDeclaringClass();
+ @CallerSensitive
+ public Class<?> getDeclaringClass() throws SecurityException {
+ final Class<?> candidate = getDeclaringClass0();
+
+ if (candidate != null)
+ candidate.checkPackageAccess(
+ ClassLoader.getClassLoader(Reflection.getCallerClass()), true);
+ return candidate;
+ }
+
+ private native Class<?> getDeclaringClass0();
/**
--- a/jdk/src/share/native/java/lang/Class.c Thu Jul 11 13:32:36 2013 -0700
+++ b/jdk/src/share/native/java/lang/Class.c Mon Jul 15 14:44:50 2013 +0200
@@ -69,7 +69,7 @@
{"getDeclaredConstructors0","(Z)[" CTR, (void *)&JVM_GetClassDeclaredConstructors},
{"getProtectionDomain0", "()" PD, (void *)&JVM_GetProtectionDomain},
{"getDeclaredClasses0", "()[" CLS, (void *)&JVM_GetDeclaredClasses},
- {"getDeclaringClass", "()" CLS, (void *)&JVM_GetDeclaringClass},
+ {"getDeclaringClass0", "()" CLS, (void *)&JVM_GetDeclaringClass},
{"getGenericSignature0", "()" STR, (void *)&JVM_GetClassSignature},
{"getRawAnnotations", "()" BA, (void *)&JVM_GetClassAnnotations},
{"getConstantPool", "()" CPL, (void *)&JVM_GetClassConstantPool},