diff --git a/src/java.base/share/classes/java/io/ObjectInputFilter.java b/src/java.base/share/classes/java/io/ObjectInputFilter.java index a45b655f8e74b21a7c552a8e67e6e5104000db9e..69044b199a031d4ff248ba21169245f862573c29 100644 --- a/src/java.base/share/classes/java/io/ObjectInputFilter.java +++ b/src/java.base/share/classes/java/io/ObjectInputFilter.java @@ -495,8 +495,8 @@ public interface ObjectInputFilter { // 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 @@ public interface ObjectInputFilter { * 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()); } /** diff --git a/src/java.base/share/classes/java/io/ObjectStreamClass.java b/src/java.base/share/classes/java/io/ObjectStreamClass.java index ebca26e27616e2ba9eeb4ee5a7b39f34ccd24b3e..954e4a0e30a051232c0fb82a90316a10a38918f8 100644 --- a/src/java.base/share/classes/java/io/ObjectStreamClass.java +++ b/src/java.base/share/classes/java/io/ObjectStreamClass.java @@ -1580,18 +1580,7 @@ public class ObjectStreamClass implements Serializable { */ 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())); } /** diff --git a/src/java.base/share/classes/java/lang/ClassLoader.java b/src/java.base/share/classes/java/lang/ClassLoader.java index b91b4619be60b735909b96ff86c1ebaa47ed3196..844c3181b4ce94a7830001bd7977c2b39f5e18a0 100644 --- a/src/java.base/share/classes/java/lang/ClassLoader.java +++ b/src/java.base/share/classes/java/lang/ClassLoader.java @@ -675,12 +675,11 @@ public abstract class ClassLoader { 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})); diff --git a/src/java.base/share/classes/java/lang/reflect/Proxy.java b/src/java.base/share/classes/java/lang/reflect/Proxy.java index ee3395b6080cf92f75fe67ea7581a6a957afd036..74c700f18edac782a487704db8789327d3d36320 100644 --- a/src/java.base/share/classes/java/lang/reflect/Proxy.java +++ b/src/java.base/share/classes/java/lang/reflect/Proxy.java @@ -1034,11 +1034,8 @@ public class Proxy implements java.io.Serializable { // 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)); diff --git a/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java b/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java index 758d59aa0c0407674660ee041ec32c3c7eb6fc0e..12bb6a3923848f4b8a88c0d39a0c151a557abc54 100644 --- a/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java +++ b/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java @@ -332,16 +332,6 @@ public class VerifyAccess { 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.