8190733: Use Class::getPackageName in java.base implementation
authormchung
Wed, 08 Nov 2017 13:38:00 -0800
changeset 47722 ce6ff74192fc
parent 47721 d2b306a70bf1
child 47723 e6278add9ff2
child 47824 cf127be65014
8190733: Use Class::getPackageName in java.base implementation Reviewed-by: mchung, rriggs Contributed-by: Christoph Dreis <christoph.dreis@freenet.de>
src/java.base/share/classes/java/io/ObjectInputFilter.java
src/java.base/share/classes/java/io/ObjectStreamClass.java
src/java.base/share/classes/java/lang/ClassLoader.java
src/java.base/share/classes/java/lang/reflect/Proxy.java
src/java.base/share/classes/sun/invoke/util/VerifyAccess.java
--- a/src/java.base/share/classes/java/io/ObjectInputFilter.java	Wed Nov 08 13:32:40 2017 -0800
+++ b/src/java.base/share/classes/java/io/ObjectInputFilter.java	Wed Nov 08 13:38:00 2017 -0800
@@ -495,8 +495,8 @@
                         // Wildcard cases
                         if (p.endsWith(".*")) {
                             // Pattern is a package name with a wildcard
-                            final String pkg = p.substring(poffset, nameLen - 1);
-                            if (pkg.length() < 2) {
+                            final String pkg = p.substring(poffset, nameLen - 2);
+                            if (pkg.isEmpty()) {
                                 throw new IllegalArgumentException("package missing in: \"" + pattern + "\"");
                             }
                             if (negate) {
@@ -651,13 +651,12 @@
              * Returns {@code true} if the class is in the package.
              *
              * @param c   a class
-             * @param pkg a package name (including the trailing ".")
+             * @param pkg a package name
              * @return {@code true} if the class is in the package,
              * otherwise {@code false}
              */
             private static boolean matchesPackage(Class<?> c, String pkg) {
-                String n = c.getName();
-                return n.startsWith(pkg) && n.lastIndexOf('.') == pkg.length() - 1;
+                return pkg.equals(c.getPackageName());
             }
 
             /**
--- a/src/java.base/share/classes/java/io/ObjectStreamClass.java	Wed Nov 08 13:32:40 2017 -0800
+++ b/src/java.base/share/classes/java/io/ObjectStreamClass.java	Wed Nov 08 13:38:00 2017 -0800
@@ -1580,18 +1580,7 @@
      */
     private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
         return (cl1.getClassLoader() == cl2.getClassLoader() &&
-                getPackageName(cl1).equals(getPackageName(cl2)));
-    }
-
-    /**
-     * Returns package name of given class.
-     */
-    private static String getPackageName(Class<?> cl) {
-        String s = cl.getName();
-        int i = s.lastIndexOf('[');
-        i = (i < 0) ? 0 : i + 2;
-        int j = s.lastIndexOf('.');
-        return (i < j) ? s.substring(i, j) : "";
+                cl1.getPackageName().equals(cl2.getPackageName()));
     }
 
     /**
--- a/src/java.base/share/classes/java/lang/ClassLoader.java	Wed Nov 08 13:32:40 2017 -0800
+++ b/src/java.base/share/classes/java/lang/ClassLoader.java	Wed Nov 08 13:38:00 2017 -0800
@@ -675,12 +675,11 @@
                 return;
             }
 
-            final String name = cls.getName();
-            final int i = name.lastIndexOf('.');
-            if (i != -1) {
+            final String packageName = cls.getPackageName();
+            if (!packageName.isEmpty()) {
                 AccessController.doPrivileged(new PrivilegedAction<>() {
                     public Void run() {
-                        sm.checkPackageAccess(name.substring(0, i));
+                        sm.checkPackageAccess(packageName);
                         return null;
                     }
                 }, new AccessControlContext(new ProtectionDomain[] {pd}));
--- a/src/java.base/share/classes/java/lang/reflect/Proxy.java	Wed Nov 08 13:32:40 2017 -0800
+++ b/src/java.base/share/classes/java/lang/reflect/Proxy.java	Wed Nov 08 13:38:00 2017 -0800
@@ -1034,11 +1034,8 @@
 
                 // do permission check if the caller is in a different runtime package
                 // of the proxy class
-                int n = proxyClass.getName().lastIndexOf('.');
-                String pkg = (n == -1) ? "" : proxyClass.getName().substring(0, n);
-
-                n = caller.getName().lastIndexOf('.');
-                String callerPkg = (n == -1) ? "" : caller.getName().substring(0, n);
+                String pkg = proxyClass.getPackageName();
+                String callerPkg = caller.getPackageName();
 
                 if (pcl != ccl || !pkg.equals(callerPkg)) {
                     sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
--- a/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java	Wed Nov 08 13:32:40 2017 -0800
+++ b/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java	Wed Nov 08 13:38:00 2017 -0800
@@ -332,16 +332,6 @@
         return Objects.equals(class1.getPackageName(), class2.getPackageName());
     }
 
-    /** Return the package name for this class.
-     */
-    public static String getPackageName(Class<?> cls) {
-        assert (!cls.isArray());
-        String name = cls.getName();
-        int dot = name.lastIndexOf('.');
-        if (dot < 0) return "";
-        return name.substring(0, dot);
-    }
-
     /**
      * Test if two classes are defined as part of the same package member (top-level class).
      * If this is true, they can share private access with each other.