Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. Collection<Class<?>> getAllInterfaceImplementations(String pathToJar,
  2. Class<?> interfaceType) throws Exception {
  3. JarFile jarFile = new JarFile(pathToJar);
  4. Enumeration<JarEntry> e = jarFile.entries();
  5.  
  6. URL[] urls = {new URL("jar:file:" + pathToJar + "!/")};
  7. URLClassLoader cl = URLClassLoader.newInstance(urls);
  8.  
  9. Collection<Class<?>> classes = new ArrayList<>();
  10. while (e.hasMoreElements()) {
  11. JarEntry je = e.nextElement();
  12. if (je.isDirectory() || !je.getName().endsWith(".class"))
  13. continue;
  14.  
  15. String className = je.getName().substring(0, je.getName().length() - ".class".length());
  16. className = className.replace('/', '.');
  17. Class type = cl.loadClass(className);
  18. if (interfaceType.isAssignableFrom(type))
  19. classes.add(type);
  20. }
  21.  
  22. return classes;
  23. }
  24.  
  25. String pathToJar = "<путь до JAVA_HOME>/rt.jar";
  26.  
  27. for (Class<?> type : getAllInterfaceImplementations(pathToJar, Collection.class))
  28. System.out.println(type.getName());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement