I want to remove the suppression of the unchecked conversion warning. How can I cast o
to T
with complete surety?
Of note: the ResultSet in this snippet is a proprietary wrapper that is source-agnostic. It's very similar to java.sql.ResultSet, but it is not the same. Also, using Eclipse Mars.1 with Java 8 u45, and both the IDE and javac issue the warning. I realize that since it's wrapped in that if
statement, that it's technically not an issue, but I absolutely hate to suppress warnings. And I feel like there's gotta be a completely type-safe way to perform that conversion.
public class ResultSetQuery {
public static <T> List<T> collectValues(ResultSet rs, String keyName, Class<T> tclass) {
List<T> result = new LinkedList<>();
while(rs.next()) {
Object o = rs.getData(keyName);
if (tclass.isAssignableFrom(o.getClass())) {
@SuppressWarnings("unchecked")
T v = (T)o;
result.add(v);
} else {
result.add(null);
}
}
return result;
}
}