use of org.eclipse.osgi.internal.loader.classpath.ClasspathEntry in project knime-core by knime.
the class EclipseUtil method findClasses.
/**
* Searches the given class loader for classes that match the given class filter. This method is capable of
* searching through Eclipse plug-ins but it will not recursivly search dependencies.
*
* @param filter a filter for classes
* @param classLoader the class loader that should be used for searching
* @return a collection with matching classes
* @throws IOException if an I/O error occurs while scanning the class path
* @since 2.12
*/
public static Collection<Class<?>> findClasses(final ClassFilter filter, final ClassLoader classLoader) throws IOException {
List<URL> classPathUrls = new ArrayList<>();
if (classLoader instanceof ModuleClassLoader) {
ModuleClassLoader cl = (ModuleClassLoader) classLoader;
ClasspathManager cpm = cl.getClasspathManager();
for (ClasspathEntry e : cpm.getHostClasspathEntries()) {
BundleFile bf = e.getBundleFile();
classPathUrls.add(bf.getEntry("").getLocalURL());
}
} else if (classLoader instanceof URLClassLoader) {
URLClassLoader cl = (URLClassLoader) classLoader;
for (URL u : cl.getURLs()) {
classPathUrls.add(u);
}
} else {
FileLocator.toFileURL(classLoader.getResource(""));
}
// filter classpath for nested entries
for (Iterator<URL> it = classPathUrls.iterator(); it.hasNext(); ) {
URL url1 = it.next();
for (URL url2 : classPathUrls) {
if ((url1 != url2) && url2.getPath().startsWith(url1.getPath())) {
it.remove();
break;
}
}
}
List<Class<?>> classes = new ArrayList<>();
for (URL url : classPathUrls) {
if ("file".equals(url.getProtocol())) {
String path = url.getPath();
// path = path.replaceFirst(Pattern.quote(classPath) + "$", "");
collectInDirectory(new File(path), "", filter, classes, classLoader);
} else if ("jar".equals(url.getProtocol())) {
String path = url.getPath().replaceFirst("^file:", "").replaceFirst("\\!.+$", "");
collectInJar(new JarFile(path), filter, classes, classLoader);
} else {
throw new IllegalStateException("Cannot read from protocol '" + url.getProtocol() + "'");
}
}
return classes;
}
Aggregations