[Patch][JDK10] Use Class.getPackageName() where possible

mandy chung mandy.chung at oracle.com
Fri Nov 3 03:23:08 UTC 2017



On 11/2/17 5:55 PM, Christoph Dreis wrote:
> Hey,
>
> I noticed two places in the codebase that could call JDK 9's new method
> Class.getPackageName().

It's good to see JDK code be updated to use Class::getPackageName. One 
thing to pay attention is that Class.getPackageName() returns 
"java.lang" for primitive type and void.  Your patch fixing 
ObjectStreamClass::getPackageName and Proxy::checkNewProxyPermission 
look fine.

There are other places that can be converted.  Do you mind updating 
java.io.ObjectInputFilter::matchesPackage and 
ClassLoader::checkPackageAccess?   I may miss there are other places in 
java.base.

I can sponsor this once you have an updated patch.

Mandy

> Would be happy if this is sponsored in case the patch is correct.
>
> Cheers,
> Christoph
>
> ====== PATCH =======
> diff -r 438e0c9f2f17
> src/java.base/share/classes/java/io/ObjectStreamClass.java
> --- a/src/java.base/share/classes/java/io/ObjectStreamClass.java	Mon
> Oct 30 17:49:33 2017 -0700
> +++ b/src/java.base/share/classes/java/io/ObjectStreamClass.java	Fri
> Nov 03 01:47:04 2017 +0100
> @@ -1587,11 +1587,7 @@
>        * 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) : "";
> +        return cl.getPackageName();
>       }
>   
>       /**
> diff -r 438e0c9f2f17
> src/java.base/share/classes/java/lang/reflect/Proxy.java
> --- a/src/java.base/share/classes/java/lang/reflect/Proxy.java	Mon Oct 30
> 17:49:33 2017 -0700
> +++ b/src/java.base/share/classes/java/lang/reflect/Proxy.java	Fri Nov 03
> 01:47:04 2017 +0100
> @@ -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));
>



More information about the core-libs-dev mailing list