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
ois actually an instance ofAction<String>that won’t be caught by the cast – you’ll only see the problem when you try to use it, passing in aClientInterfaceinstead of a string.You can get rid of the warning using:
as a function annotation, but you can’t easily sort out the underlying problem 🙁