Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 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.  
  16. String className = je.getName().substring(0, je.getName().length() - ".class".length());
  17. className = className.replace('/', '.');
  18. Class type = cl.loadClass(className);
  19. if (interfaceType.isAssignableFrom(type))
  20. classes.add(type);
  21. }
  22.  
  23. return classes;
  24. }
  25.  
  26. String pathToJar = "<путь до JAVA_HOME>/rt.jar";
  27.  
  28. for (Class<?> type : getAllInterfaceImplementations(pathToJar, Collection.class))
  29. System.out.println(type.getName());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement