Type safety: The cast from Object to Collection<Project> is actually checki
I keep getting this warning:
Type safety: The cast from Object to Collection<Project> is actually checking against the erased
type Collection
this is the code that it complains about
final Collection<Project> projects = (Collection<Project>)request.getAttribute(Constants.PROJECTS);
How can I solve this issue?
# 1
You can add @SuppressWarnings("unchecked") to the method containing this code, but this doesn't get rid of the problem - it only supresses the warning.
The real problem is that the compiler is only checking that the attribute you're retrieving is a Collection - it can't check that it's a Collection of Projects. This is because parameter types are removed at compile time - when the code is running, it's just a Collection of Objects, not a Collection of anything in particular.
Have a look at this page:
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
The removal of the Project type parameter is known as type erasure. The JVM doesn't know at runtime that the collection is a Collection of Projects.