I have this piece of code that gives the warning mentioned in the title:
List<Student> studentList = session.createCriteria(Student.class)
.add(Restrictions.eq("indexNumber", indexNum))
.list();
I've read the thread How do I fix "The expression of type List needs unchecked conversion...'? and there's a great solution by @BrunoDeFraine:
public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
List<T> r = new ArrayList<T>(c.size());
for(Object o: c)
r.add(clazz.cast(o));
return r;
}
then I can just do this:
List<SyndEntry> entries = castList(SyndEntry.class, sf.getEntries());
This works great, but in my case I have Criteria as argument, not class and collection. My question is, can this method be adapted to have criteria as argument, or should I simply use @SuppressWarnings("unchecked")?
Moved from comment:
You want to cast a Criteria object? That makes no sense. criteria.list()
returns a List, so you could just use that.
The castList
method is odd anyways, you don't gain any compile-time type-safety (it will still only fail at runtime if something is wrong), and it could slow things down, especially if the list is long. I'd just use @SuppressWarnings or live with the warning.