This is a downcast. It's the dynamic cast where the compiler has no idea of the actual object the reference is pointing to.
You get this warning because the target type of the cast Action<ClientInterface>
is a parameterized type and the compiler cannot guarantee that the object being type casted is of the same type.
If you don't want to suppress this warning and don't care about the type parameter, you could change the code to this by using wildcards:
Action<?> action = null;
try {
Object o = c.newInstance();
if (o instanceof Action<?>) {
action = (Action<?>) o;
} else {
// TODO 2 Auto-generated catch block
throw new InstantiationException();
}
[...]
This is much safer because the instanceof
can't check that o
is a object of Action<ClientInterface>
, it just check if o
is a object of Action
since further generic type information will be erased at runtime.