Java Question

Type safety: Unchecked cast from List to List<String>

I have this method where I cast the results to (List ) , but my eclipse is still complaining ! Type safety: Unchecked cast from List to List

public List<String> getDevices(Long productId) {

String queryString = "SELECT FROM t_operation op WHERE op.discriminator = 'ANDROID' and PRODUCT =:productId ";

try {
Query query = getEntityManager().createQuery(queryString);
query.setParameter("productId", productId);

return (List<String> ) query.getResultList();

} catch (RuntimeException re) {
throw re;


Answer Source

You will get this warning due runtime check cast.

Even if you use if(query.getResultList() instanceof List<?>) you will get this warning, so...

  • use @SuppressWarnings("unchecked") or
  • use generics