I am creating a function where I want to register different type of Exception in external library. They already have a method defined which accept Class as parameter.
I want to read different exception from configuration and register it, however I am getting typecast warning. I can ignore/suppress it, but is there any way to not suppress it?
The exact warning is Type safety: Unchecked cast from Class<capture#2-of ?> to Class<? extends Throwable>
Here is the sample code which throws warning
Class<?> exceptionClass = Class.forName("java.lang.Exception"); //Getting this value from Config. Let us assume that config is good and I get only valid classname which extends Throwable
registerExceptionInExternalLibrary((Class<? extends Throwable>)exceptionClass); // Getting the warning here
I think I cannot do casting here, since there is not object. But if I do not use casting, then I get this warning
The method registerExceptionInExternalLibrary(Class<? extends Throwable>) in the type XXXXX is not applicable for the arguments (Class<capture#2-of ?>)
Here is that code sample
Class<?> exceptionClass = Class.forName("java.lang.Exception"); //Getting this value from Config. Let us assume that config is good and I get only valid classname which extends Throwable
registerExceptionInExternalLibrary(exceptionClass); // Getting the warning here