8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations
authorjfranck
Mon, 15 Jul 2013 14:44:50 +0200
changeset 20816 b44a353664b8
parent 20815 31d867b2565d
child 20817 a842c616ae88
child 20818 da638a97c27a
8014349: (cl) Class.getDeclaredClass problematic in some class loader configurations Reviewed-by: mchung, ahgross, darcy
jdk/src/share/classes/java/lang/Class.java
jdk/src/share/native/java/lang/Class.c
--- 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},