The FindBugs Blog

Monday, November 13, 2006

WTF in javax.sql.rowset.serial.SerialRef

Sometimes, a bug report will point you to some code that just elicits a WTF response.

FindBugs generated a null pointer warning in javax.sql.rowset.serial.SerialRef, complaining about the call if (!object.equals(null)):

/**
* Returns an Object representing the SQL structured type
* to which this SerialRef object refers. The attributes
* of the structured type are mapped according to the given type map.
*
* @param map a java.util.Map object containing zero or
* more entries, with each entry consisting of 1) a String
* giving the fully qualified name of a UDT and 2) the
* Class object for the SQLData implementation
* that defines how the UDT is to be mapped
* @return an object instance resolved from the Ref reference and mapped
* according to the supplied type map
* @throws SerialException if an error is encountered in the reference
* resolution
*/
public Object getObject(java.util.Map<String,Class<?>> map)
throws SerialException
{
map = new Hashtable(map);
if (!object.equals(null)) {
return map.get(object);
} else {
throw new SerialException("The object is not set");
}
}


But when you look at that code, that seems to be the least of the problems.
Why is the map being converted into a new Hashtable, and what is this method intended to do?

Since the return type of this isn't Class, I'm guessing that it should
return something other than Class objects. I'm guessing it should create an instance of the class found in the Map. Does anyone know?

This is one of the places where using a generic type would have been very useful in documenting the specification of the method, even if it wasn't that useful to users of the method. If the method had been declared as:
public <T> T getObject(java.util.Map<String,Class<? extends T>> map)
then the relationship between the map and the return value would have been exceptionally clear. Of course, I don't know if this is the intended specification of the class.

P.S. Thanks to Sun's open sourcing of Java, I no longer have to worry about posting examples with source code from Sun's JDK.