Solving a unique uncontrolled throw warning

I have an object where the type of a generic T is lost at some point through a giant chain of interfaces. I was wondering if it is possible to use a function to restore some type safety by checking types:

 private T metaData; // type of T is lost public <R> R getMetaData(Class<R> className) { assert className.isInstance(metaData); return (R) metaData; } 

However, this implementation generates the warning โ€œUnverified cast:โ€œ T โ€toโ€œ R. โ€Is there an implementation that avoids this warning or is it the only solution to suppress it?

+5
source share
1 answer

You do not need to specify R with () , you can use Class instead. How,

 return className.cast(metaData); 

which does not cause any warnings.

+7
source

Source: https://habr.com/ru/post/1275669/


All Articles