Search in sources :

Example 1 with ResourceInfo

use of co.cask.cdap.common.internal.guava.ClassPath.ResourceInfo in project cdap by caskdata.

the class ClassPathResources method findClassDependencies.

/**
   * Finds all resource names that the given set of classes depends on.
   *
   * @param classLoader class loader for looking up .class resources
   * @param classes set of class names that need to trace dependencies from
   * @param result collection to store the resulting resource names
   * @param <T> type of the result collection
   * @throws IOException if fails to load class bytecode during tracing
   */
private static <T extends Collection<String>> T findClassDependencies(final ClassLoader classLoader, Iterable<String> classes, final T result) throws IOException {
    final Set<String> bootstrapClassPaths = getBootstrapClassPaths();
    final Set<URL> classPathSeen = Sets.newHashSet();
    Dependencies.findClassDependencies(classLoader, new ClassAcceptor() {

        @Override
        public boolean accept(String className, URL classUrl, URL classPathUrl) {
            // Ignore bootstrap classes
            if (bootstrapClassPaths.contains(classPathUrl.getFile())) {
                return false;
            }
            // visible through the program classloader.
            if (className.startsWith("org.slf4j.impl.")) {
                return false;
            }
            if (!classPathSeen.add(classPathUrl)) {
                return true;
            }
            // Add all resources in the given class path
            try {
                ClassPath classPath = ClassPath.from(classPathUrl.toURI(), classLoader);
                for (ClassPath.ResourceInfo resourceInfo : classPath.getResources()) {
                    result.add(resourceInfo.getResourceName());
                }
            } catch (Exception e) {
            // If fail to get classes/resources from the classpath, ignore this classpath.
            }
            return true;
        }
    }, classes);
    return result;
}
Also used : ClassPath(co.cask.cdap.common.internal.guava.ClassPath) ResourceInfo(co.cask.cdap.common.internal.guava.ClassPath.ResourceInfo) ClassAcceptor(org.apache.twill.api.ClassAcceptor) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Aggregations

ClassPath (co.cask.cdap.common.internal.guava.ClassPath)1 ResourceInfo (co.cask.cdap.common.internal.guava.ClassPath.ResourceInfo)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ClassAcceptor (org.apache.twill.api.ClassAcceptor)1