RFR: 8265418: Clean-up redundant null-checks of Class.getPackageName()

Peter Levart peter.levart at gmail.com
Mon Apr 26 14:09:54 UTC 2021


On 4/26/21 1:27 PM, Сергей Цыпанов wrote:
> On Mon, 26 Apr 2021 08:45:52 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>
>>> @AlanBateman if DL is not responding, will it be ok to just revert the changes related to `java.util.concurrent`?
>> That should be fine, the null check in Objects.equals is benign with these usages.
> One more thing I'm thinking about (not to be done in this PR of course) is to move call to `String.intern()` from where it is now in `Class.getPackageName()`
>
> public String getPackageName() {
>      String pn = this.packageName;
>      if (pn == null) {
>          Class<?> c = this;
>          while (c.isArray()) {
>              c = c.getComponentType();
>          }
>          if (c.isPrimitive()) {
>              pn = "java.lang";
>          } else {
>              String cn = c.getName();
>              int dot = cn.lastIndexOf('.');
>              pn = (dot != -1) ? cn.substring(0, dot).intern() : "";    // <---
>          }
>          this.packageName = pn;
>      }
>      return pn;
> }
>
> to `packageName` field assignement like
>
> this.packageName = pn.intern();
>
> this would add two more Strings (`""` and `"java.lang"`) into string table and allow to avoid `String.equals()` in favour of `==` call when comparing package names. What do you think?


I think this is not needed as string constants are all already interned 
strings (guaranteed by VM). Also be careful not to return a string which 
is not interned (which would happen if you did what you are proposing 
above).


Regards, Peter


>
> -------------
>
> PR: https://git.openjdk.java.net/jdk/pull/3571


More information about the core-libs-dev mailing list