Type safety: Unchecked cast from Object
Solution 1
Yes - this is a natural consequence of type erasure. If o
is actually an instance of Action<String>
that won't be caught by the cast - you'll only see the problem when you try to use it, passing in a ClientInterface
instead of a string.
You can get rid of the warning using:
@SuppressWarnings("unchecked")
as a function annotation, but you can't easily sort out the underlying problem :(
Solution 2
As usual, Jon Skeet is right.
To elaborate on the not-easily part of his answer:
Given
class ClientAction implements Action<ClientInterface> {}
You can write:
Class<? extends Action<ClientInterface>> c = ClientAction.class;
Action<ClientInterface> action = c.newInstance();
This eliminates both the cast and the warning, at the price of introducing a non-generic type so you can use .class
to get a sufficiently accurately typed Class
object.
Solution 3
The warning means that the compiler can't guarantee type safety even if the casting works fine at runtime. Due to erasure, at runtime the casting is simply a casting to Action. It is possible that the underlying generic class is not of type ClientInterface as expected. In this case, the problem will appear later (maybe even much later), as a ClassCastException.
In this specific case I recommend suppressing this specific warning by the following compiler directive:
@SuppressWarnings("unchecked")
Solution 4
Don't worry about. It is because the Java compiler has no way to know, what is the real type of the object.
Solution 5
You've lost the type information because of erasure (i.e., the parameterised types have been erased), hence the warning. You can't do anything about it, other than clean up the surrounding code so that generics are used more frequently, so you can pass the generic type information and avoid casting at all.
Related videos on Youtube
Matthew
Updated on March 27, 2020Comments
-
Matthew about 4 years
I try to cast an object to my Action class, but it results in a warning:
Type safety: Unchecked cast from Object to Action<ClientInterface> Action<ClientInterface> action = null; try { Object o = c.newInstance(); if (o instanceof Action<?>) { action = (Action<ClientInterface>) o; } else { // TODO 2 Auto-generated catch block throw new InstantiationException(); } [...]
Thank you for any help
-
meriton about 14 yearsWhat is the type of
c
? -
Matthew about 14 yearsRegarding the type of c : Class<?> c = null; c = Class.forName(className); Anyway, I had enough clear answers ! Thank you all, is it possible to somehow close the question or mark it as answered ?
-
meriton about 14 yearsYes, use the green checkmark left of your preferred answer.
-
blahdiblah about 11 yearspossible duplicate of How do I address unchecked cast warnings?
-
Cosmin over 5 yearsWhat is the question?
-