Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Collection<Class<?>> getAllInterfaceImplementations(String pathToJar,
- Class<?> interfaceType) throws Exception {
- JarFile jarFile = new JarFile(pathToJar);
- Enumeration<JarEntry> e = jarFile.entries();
- URL[] urls = {new URL("jar:file:" + pathToJar + "!/")};
- URLClassLoader cl = URLClassLoader.newInstance(urls);
- Collection<Class<?>> classes = new ArrayList<>();
- while (e.hasMoreElements()) {
- JarEntry je = e.nextElement();
- if (je.isDirectory() || !je.getName().endsWith(".class"))
- continue;
- String className = je.getName().substring(0, je.getName().length() - ".class".length());
- className = className.replace('/', '.');
- Class type = cl.loadClass(className);
- if (interfaceType.isAssignableFrom(type))
- classes.add(type);
- }
- return classes;
- }
- String pathToJar = "<путь до JAVA_HOME>/rt.jar";
- for (Class<?> type : getAllInterfaceImplementations(pathToJar, Collection.class))
- System.out.println(type.getName());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement