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