changeset 14518:e45ef8281247

Make use of Class::getPackageName in access checks
author alanb
date Mon, 30 Nov 2015 13:58:58 +0000
parents 02c750fa92d2
children ffe12af5613c
files src/java.base/share/classes/sun/invoke/util/VerifyAccess.java src/java.base/share/classes/sun/reflect/Reflection.java
diffstat 2 files changed, 26 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java	Mon Nov 30 11:38:57 2015 +0000
+++ b/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java	Mon Nov 30 13:58:58 2015 +0000
@@ -187,7 +187,7 @@
 
             // early VM startup case, java.base not defined
             if (lookupModule == null) {
-                if (refModule != null) throw new InternalError();
+                assert refModule == null;
                 return true;
             }
 
@@ -195,7 +195,11 @@
                 return false;
 
             // check the package is exported unconditionally
-            if (refModule.isExported(getPackageName(refc)))
+            Class<?> c = refc;
+            while (c.isArray()) {
+                c = c.getComponentType();
+            }
+            if (c.isPrimitive() || refModule.isExported(c.getPackageName()))
                 return true;
 
             // not exported but allow access during VM initialization
@@ -288,14 +292,11 @@
     /** Return the package name for this class.
      */
     public static String getPackageName(Class<?> cls) {
-        if (cls.isArray()) {
-            return getPackageName(cls.getComponentType());
-        } else {
-            String name = cls.getName();
-            int dot = name.lastIndexOf('.');
-            if (dot < 0) return "";
-            return name.substring(0, dot);
-        }
+        assert (!cls.isArray());
+        String name = cls.getName();
+        int dot = name.lastIndexOf('.');
+        if (dot < 0) return "";
+        return name.substring(0, dot);
     }
 
     /**
--- a/src/java.base/share/classes/sun/reflect/Reflection.java	Mon Nov 30 11:38:57 2015 +0000
+++ b/src/java.base/share/classes/sun/reflect/Reflection.java	Mon Nov 30 13:58:58 2015 +0000
@@ -104,7 +104,12 @@
             if (m2.isNamed())
                 memberSuffix = " (in " + m2 + ")";
 
-            String memberPackageName = packageName(memberClass);
+            Class<?> c = memberClass;
+            while (c.isArray()) {
+                c = c.getComponentType();
+            }
+            String memberPackageName = c.getPackageName();
+
             boolean canRead = m1.canRead(m2);
 
             String msg = currentClass + currentSuffix + " cannot access ";
@@ -234,17 +239,16 @@
         if (!currentModule.canRead(memberModule))
             return false;
 
+        // memberClass may be primitive or array class
+        Class<?> c = memberClass;
+        while (c.isArray()) {
+            c = c.getComponentType();
+        }
+        if (c.isPrimitive())
+            return true;
+
         // check that memberModule exports the package to currentModule
-        return memberModule.isExported(packageName(memberClass), currentModule);
-    }
-
-    private static String packageName(Class<?> c) {
-        String pn = c.getPackageName();
-        if (pn != null) {
-            return pn;
-        } else {
-            throw new InternalError("Should not get here: " + c);
-        }
+        return memberModule.isExported(c.getPackageName(), currentModule);
     }
 
     private static boolean isSameClassPackage(Class<?> c1, Class<?> c2) {