Search code examples
javavectorsuppress-warningsdefaulttablemodelunchecked

Java call to Vector.set() results in 'unchecked' warning


I am extending javax.swing.table.DefaultTableModel, and am adding a method which calls Vector.set(...) on a class variable. It results in an unchecked warning, which I want to fix rather than suppress. Since this isn't extending Vector, I can't use <E> it seems, and I don't have a way of knowing what type of Object will be in the Vector. Suggestions?

Method:

/**
 * Replace a row in the dataVector.  Convenience method for 
 * getDataVector().set(index, element)
 * @param rowNum the index of the row to replace
 * @param replaceRow the element to be stored at the specified position
 * @return the element previously at the specified position
 * @throws ArrayIndexOutOfBoundsException if the index is out of range
 */
public Vector setRow(int rowNum, Vector replaceRow) {
  return (Vector)dataVector.set(rowNum, replaceRow);
}

This results in:

warning: [unchecked] unchecked call to set(int,E) as a member of the raw type Vector
return (Vector)dataVector.set(rowNum, replaceRow);
                             ^
where E is a type-variable:
E extends Object declared in class Vector
1 warning

Solution

  • Sorry, I did not realize that DefaultTableModel was using an unparameterized, raw type for dataVector. I think in this case, all you can really do is slap on a @SuppressWarnings("unchecked") annotation for that function (which will make the compiler stop complaining), javadoc it thoroughly, and call it a day:

    /* DOCUMENT THIS THOROUGHLY */
    @SuppressWarnings("unchecked")
    public Vector setRow(final int rowNum, final Vector replaceRow) {
        return (Vector)dataVector.set(rowNum, replaceRow);
    }
    

    Old answer:

    Based on your code, it looks like what you really want to do is this:

    Vector<Vector<Object>> dataVector = new Vector<Vector<Object>>();
    
    public Vector<Object> setRow(final int rowNum, final Vector<Object> replaceRow) {
        return dataVector.set(rowNum, replaceRow);
    }
    

    The way your code is written/designed, it appears that dataVector is intended to actually be a "Vector of Vectors", where each element (a Vector) can hold any kind of Object? Using generics this way in your function and dataVector will eliminate the unchecked warnings.

    If I have misunderstood, please let me know.