Regarding 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 ?
–
MatthewApr 7 '10 at 14:28

Yes, use the green checkmark left of your preferred answer.
–
meritonApr 7 '10 at 15:44

5 Answers
5

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 :(

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:

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.