Guest User

Untitled

a guest
Jul 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. Class<? extends Callable<Integer>> clazz = (Class<? extends Callable<Integer>>) Class.forName(options.valueOf(className)).asSubclass(Callable.class);
  2.  
  3. Class<Callable<Integer>> classCI = ...;
  4.  
  5. Class<? extends Callable<Integer>> clazz =
  6. Class.forName(options.valueOf(className))
  7. .asSubclass(classCI);
  8.  
  9. Class<Callable<Integer>> classCI = (Class<Callable<Integer>>)Callable.class;
  10.  
  11. Object o = ...;
  12. String s1 = (String)o; // may fail, no javac warning
  13. String s2 = String.class.cast(o); // may fail, no javac warning
  14.  
  15. @SuppressWarning( "unchecked" )
  16. Class<? Callable<Integer>> getClass(String className)
  17. {
  18. Class clazz = Class.forName(className);
  19. via reflection, check generic super interfaces of clazz
  20. if there's no Callable<Integer> super interface
  21. throw "className is not a Callable<Integer>"
  22.  
  23. // we have *checked*, the following cast is safe
  24. return (Class<? Callable<Integer>>)clazz;
  25. }
Add Comment
Please, Sign In to add comment