RFR: 8354789: Unnecessary null check in sun.awt.windows.WToolkit.getFontPeer

Alexey Ivanov aivanov at openjdk.org
Wed Apr 16 12:44:45 UTC 2025


On Thu, 16 Jan 2025 12:03:50 GMT, Andrey Turbanov <aturbanov at openjdk.org> wrote:

> There is redundant `null` comparison in `sun.awt.windows.WToolkit.getFontPeer`
> Local variable `retval` can't be null after new object assignment.
> https://github.com/openjdk/jdk/blob/24de9dee80738fe6ab1fc726b071546c85bbf79a/src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java#L604-L605

Looks good to me, however, I think we can improve the code further… What do you think?

src/java.desktop/windows/classes/sun/awt/windows/WToolkit.java line 595:

> 593: 
> 594:     @Override
> 595:     public FontPeer getFontPeer(String name, int style) {

I suggest refactoring the code further…


    public FontPeer getFontPeer(String name, int style) {
        String lcName = name.toLowerCase();
        if (null != cacheFontPeer) {
            FontPeer cachedVal = cacheFontPeer.get(lcName + style);
            if (null != cachedVal) {
                return cachedVal;
            }
        }

        FontPeer retval = new WFontPeer(name, style);
        if (null == cacheFontPeer) {
            cacheFontPeer = new Hashtable<>(5, 0.9f);
        }
        if (null != cacheFontPeer) {
            cacheFontPeer.put(lcName + style, retval);
        }
        return retval;
    }


Isn't this version clearer?

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

PR Review: https://git.openjdk.org/jdk/pull/23150#pullrequestreview-2772370543
PR Review Comment: https://git.openjdk.org/jdk/pull/23150#discussion_r2046841311


More information about the client-libs-dev mailing list