Search in sources :

Example 1 with ClasspathEntry

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;
}
Also used : ClasspathManager(org.eclipse.osgi.internal.loader.classpath.ClasspathManager) ModuleClassLoader(org.eclipse.osgi.internal.loader.ModuleClassLoader) ArrayList(java.util.ArrayList) JarFile(java.util.jar.JarFile) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) ClasspathEntry(org.eclipse.osgi.internal.loader.classpath.ClasspathEntry) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) JarFile(java.util.jar.JarFile) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile) File(java.io.File)

Aggregations

File (java.io.File)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 ModuleClassLoader (org.eclipse.osgi.internal.loader.ModuleClassLoader)1 ClasspathEntry (org.eclipse.osgi.internal.loader.classpath.ClasspathEntry)1 ClasspathManager (org.eclipse.osgi.internal.loader.classpath.ClasspathManager)1 BundleFile (org.eclipse.osgi.storage.bundlefile.BundleFile)1