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

Сергей Цыпанов github.com+10835776+stsypanov at openjdk.java.net
Mon Apr 26 11:27:31 UTC 2021


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?

-------------

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


More information about the core-libs-dev mailing list