8190733: Use Class::getPackageName in java.base implementation
Reviewed-by: mchung, rriggs
This commit is contained in:
parent
73f177ea06
commit
5d7c797278
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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}));
|
||||
|
@ -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));
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user