mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8190733: Use Class::getPackageName in java.base implementation
Reviewed-by: mchung, rriggs
This commit is contained in:
parent
73f177ea06
commit
5d7c797278
5 changed files with 10 additions and 36 deletions
|
@ -495,8 +495,8 @@ public interface ObjectInputFilter {
|
||||||
// Wildcard cases
|
// Wildcard cases
|
||||||
if (p.endsWith(".*")) {
|
if (p.endsWith(".*")) {
|
||||||
// Pattern is a package name with a wildcard
|
// Pattern is a package name with a wildcard
|
||||||
final String pkg = p.substring(poffset, nameLen - 1);
|
final String pkg = p.substring(poffset, nameLen - 2);
|
||||||
if (pkg.length() < 2) {
|
if (pkg.isEmpty()) {
|
||||||
throw new IllegalArgumentException("package missing in: \"" + pattern + "\"");
|
throw new IllegalArgumentException("package missing in: \"" + pattern + "\"");
|
||||||
}
|
}
|
||||||
if (negate) {
|
if (negate) {
|
||||||
|
@ -651,13 +651,12 @@ public interface ObjectInputFilter {
|
||||||
* Returns {@code true} if the class is in the package.
|
* Returns {@code true} if the class is in the package.
|
||||||
*
|
*
|
||||||
* @param c a class
|
* @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,
|
* @return {@code true} if the class is in the package,
|
||||||
* otherwise {@code false}
|
* otherwise {@code false}
|
||||||
*/
|
*/
|
||||||
private static boolean matchesPackage(Class<?> c, String pkg) {
|
private static boolean matchesPackage(Class<?> c, String pkg) {
|
||||||
String n = c.getName();
|
return pkg.equals(c.getPackageName());
|
||||||
return n.startsWith(pkg) && n.lastIndexOf('.') == pkg.length() - 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1580,18 +1580,7 @@ public class ObjectStreamClass implements Serializable {
|
||||||
*/
|
*/
|
||||||
private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
|
private static boolean packageEquals(Class<?> cl1, Class<?> cl2) {
|
||||||
return (cl1.getClassLoader() == cl2.getClassLoader() &&
|
return (cl1.getClassLoader() == cl2.getClassLoader() &&
|
||||||
getPackageName(cl1).equals(getPackageName(cl2)));
|
cl1.getPackageName().equals(cl2.getPackageName()));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) : "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -675,12 +675,11 @@ public abstract class ClassLoader {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final String name = cls.getName();
|
final String packageName = cls.getPackageName();
|
||||||
final int i = name.lastIndexOf('.');
|
if (!packageName.isEmpty()) {
|
||||||
if (i != -1) {
|
|
||||||
AccessController.doPrivileged(new PrivilegedAction<>() {
|
AccessController.doPrivileged(new PrivilegedAction<>() {
|
||||||
public Void run() {
|
public Void run() {
|
||||||
sm.checkPackageAccess(name.substring(0, i));
|
sm.checkPackageAccess(packageName);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}, new AccessControlContext(new ProtectionDomain[] {pd}));
|
}, 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
|
// do permission check if the caller is in a different runtime package
|
||||||
// of the proxy class
|
// of the proxy class
|
||||||
int n = proxyClass.getName().lastIndexOf('.');
|
String pkg = proxyClass.getPackageName();
|
||||||
String pkg = (n == -1) ? "" : proxyClass.getName().substring(0, n);
|
String callerPkg = caller.getPackageName();
|
||||||
|
|
||||||
n = caller.getName().lastIndexOf('.');
|
|
||||||
String callerPkg = (n == -1) ? "" : caller.getName().substring(0, n);
|
|
||||||
|
|
||||||
if (pcl != ccl || !pkg.equals(callerPkg)) {
|
if (pcl != ccl || !pkg.equals(callerPkg)) {
|
||||||
sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
|
sm.checkPermission(new ReflectPermission("newProxyInPackage." + pkg));
|
||||||
|
|
|
@ -332,16 +332,6 @@ public class VerifyAccess {
|
||||||
return Objects.equals(class1.getPackageName(), class2.getPackageName());
|
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).
|
* 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.
|
* If this is true, they can share private access with each other.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue