Confusing 'null'-check in JDK-8235961 fix

Andrey Turbanov turbanoff at gmail.com
Wed Dec 23 13:10:02 UTC 2020


Hello.
I've found confusing placement of `null` check in the fix for JDK-8235961
Method 'com.sun.rowset.CachedRowSetImpl#getColIdxByName'

private int getColIdxByName(String name) throws SQLException {
    RowSetMD = (RowSetMetaDataImpl)this.getMetaData();
    int cols = RowSetMD.getColumnCount();
    if (RowSetMD != null) {
        for (int i = 1; i <= cols; ++i) {
            String colName = RowSetMD.getColumnName(i);
            if (colName != null)
                if (name.equalsIgnoreCase(colName))
                    return (i);
                else
                    continue;
        }
    }
    throw new SQLException(resBundle.handleGetObject("cachedrowsetimpl.invalcolnm").toString());
}

As you can see, field 'RowSetMD' is checked for 'null' _after_
dereferencing. I think proper code should look like this:

private int getColIdxByName(String name) throws SQLException {
    RowSetMD = (RowSetMetaDataImpl)this.getMetaData();
    if (RowSetMD != null) {
       int cols = RowSetMD.getColumnCount();
        for (int i = 1; i <= cols; ++i) {
...

Andrey Turbanov


More information about the core-libs-dev mailing list