changeset 14291:6c27e92c5b03

setAccessible(AccessibleObject[],boolean) doesn't throw SE for Class ctor
author alanb
date Mon, 19 Oct 2015 15:21:44 +0100
parents b9faea9a4ea2
children 7b6b932de667
files src/java.base/share/classes/java/lang/reflect/Constructor.java test/jdk/jigsaw/reflect/AccessibleObject/ModuleSetAccessibleTest.java
diffstat 2 files changed, 32 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/reflect/Constructor.java	Mon Oct 19 15:20:07 2015 +0100
+++ b/src/java.base/share/classes/java/lang/reflect/Constructor.java	Mon Oct 19 15:21:44 2015 +0100
@@ -165,11 +165,6 @@
         AccessibleObject.checkPermission();
         if (flag) {
             checkCanSetAccessible(Reflection.getCallerClass());
-            if (clazz == Class.class) {
-                // can we change this to InaccessibleObjectException?
-                throw new SecurityException("Cannot make a java.lang.Class"
-                                            + " constructor accessible");
-            }
         }
         setAccessible0(flag);
     }
@@ -177,6 +172,11 @@
     @Override
     void checkCanSetAccessible(Class<?> caller) {
         checkCanSetAccessible(caller, clazz);
+        if (clazz == Class.class) {
+            // can we change this to InaccessibleObjectException?
+            throw new SecurityException("Cannot make a java.lang.Class"
+                                        + " constructor accessible");
+        }
     }
 
     @Override
--- a/test/jdk/jigsaw/reflect/AccessibleObject/ModuleSetAccessibleTest.java	Mon Oct 19 15:20:07 2015 +0100
+++ b/test/jdk/jigsaw/reflect/AccessibleObject/ModuleSetAccessibleTest.java	Mon Oct 19 15:21:44 2015 +0100
@@ -215,4 +215,31 @@
 
     }
 
+
+    /**
+     * Test that the Class constructor cannot be make accessible.
+     */
+    public void testJavaLangClass() throws Exception {
+
+        // non-public constructor
+        Constructor<?> ctor
+            = Class.class.getDeclaredConstructor(ClassLoader.class, Class.class);
+        AccessibleObject[] ctors = { ctor };
+
+        try {
+            ctor.setAccessible(true);
+            assertTrue(false);
+        } catch (SecurityException expected) { }
+
+        try {
+            AccessibleObject.setAccessible(ctors, true);
+            assertTrue(false);
+        } catch (SecurityException expected) { }
+
+        // should succeed
+        ctor.setAccessible(false);
+        AccessibleObject.setAccessible(ctors, false);
+
+    }
+
 }