001/*
002 * Copyright (C) 2012 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.reflect;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_PATH;
020import static com.google.common.base.StandardSystemProperty.PATH_SEPARATOR;
021import static java.util.logging.Level.WARNING;
022
023import com.google.common.annotations.Beta;
024import com.google.common.annotations.VisibleForTesting;
025import com.google.common.base.CharMatcher;
026import com.google.common.base.Predicate;
027import com.google.common.base.Splitter;
028import com.google.common.collect.FluentIterable;
029import com.google.common.collect.ImmutableList;
030import com.google.common.collect.ImmutableMap;
031import com.google.common.collect.ImmutableSet;
032import com.google.common.collect.Maps;
033import com.google.common.collect.MultimapBuilder;
034import com.google.common.collect.SetMultimap;
035import com.google.common.collect.Sets;
036import com.google.common.io.ByteSource;
037import com.google.common.io.CharSource;
038import com.google.common.io.Resources;
039import java.io.File;
040import java.io.IOException;
041import java.net.MalformedURLException;
042import java.net.URISyntaxException;
043import java.net.URL;
044import java.net.URLClassLoader;
045import java.nio.charset.Charset;
046import java.util.Enumeration;
047import java.util.HashSet;
048import java.util.LinkedHashMap;
049import java.util.Map.Entry;
050import java.util.NoSuchElementException;
051import java.util.Set;
052import java.util.jar.Attributes;
053import java.util.jar.JarEntry;
054import java.util.jar.JarFile;
055import java.util.jar.Manifest;
056import java.util.logging.Logger;
057import org.checkerframework.checker.nullness.compatqual.NullableDecl;
058
059/**
060 * Scans the source of a {@link ClassLoader} and finds all loadable classes and resources.
061 *
062 * <p><b>Warning:</b> Current limitations:
063 *
064 * <ul>
065 *   <li>Looks only for files and JARs in URLs available from {@link URLClassLoader} instances or
066 *       the {@linkplain ClassLoader#getSystemClassLoader() system class loader}.
067 *   <li>Only understands {@code file:} URLs.
068 * </ul>
069 *
070 * <p>In the case of directory classloaders, symlinks are supported but cycles are not traversed.
071 * This guarantees discovery of each <em>unique</em> loadable resource. However, not all possible
072 * aliases for resources on cyclic paths will be listed.
073 *
074 * @author Ben Yu
075 * @since 14.0
076 */
077@Beta
078public final class ClassPath {
079  private static final Logger logger = Logger.getLogger(ClassPath.class.getName());
080
081  private static final Predicate<ClassInfo> IS_TOP_LEVEL =
082      new Predicate<ClassInfo>() {
083        @Override
084        public boolean apply(ClassInfo info) {
085          return info.className.indexOf('$') == -1;
086        }
087      };
088
089  /** Separator for the Class-Path manifest attribute value in jar files. */
090  private static final Splitter CLASS_PATH_ATTRIBUTE_SEPARATOR =
091      Splitter.on(" ").omitEmptyStrings();
092
093  private static final String CLASS_FILE_NAME_EXTENSION = ".class";
094
095  private final ImmutableSet<ResourceInfo> resources;
096
097  private ClassPath(ImmutableSet<ResourceInfo> resources) {
098    this.resources = resources;
099  }
100
101  /**
102   * Returns a {@code ClassPath} representing all classes and resources loadable from {@code
103   * classloader} and its ancestor class loaders.
104   *
105   * <p><b>Warning:</b> {@code ClassPath} can find classes and resources only from:
106   *
107   * <ul>
108   *   <li>{@link URLClassLoader} instances' {@code file:} URLs
109   *   <li>the {@linkplain ClassLoader#getSystemClassLoader() system class loader}. To search the
110   *       system class loader even when it is not a {@link URLClassLoader} (as in Java 9), {@code
111   *       ClassPath} searches the files from the {@code java.class.path} system property.
112   * </ul>
113   *
114   * @throws IOException if the attempt to read class path resources (jar files or directories)
115   *     failed.
116   */
117  public static ClassPath from(ClassLoader classloader) throws IOException {
118    DefaultScanner scanner = new DefaultScanner();
119    scanner.scan(classloader);
120    return new ClassPath(scanner.getResources());
121  }
122
123  /**
124   * Returns all resources loadable from the current class path, including the class files of all
125   * loadable classes but excluding the "META-INF/MANIFEST.MF" file.
126   */
127  public ImmutableSet<ResourceInfo> getResources() {
128    return resources;
129  }
130
131  /**
132   * Returns all classes loadable from the current class path.
133   *
134   * @since 16.0
135   */
136  public ImmutableSet<ClassInfo> getAllClasses() {
137    return FluentIterable.from(resources).filter(ClassInfo.class).toSet();
138  }
139
140  /** Returns all top level classes loadable from the current class path. */
141  public ImmutableSet<ClassInfo> getTopLevelClasses() {
142    return FluentIterable.from(resources).filter(ClassInfo.class).filter(IS_TOP_LEVEL).toSet();
143  }
144
145  /** Returns all top level classes whose package name is {@code packageName}. */
146  public ImmutableSet<ClassInfo> getTopLevelClasses(String packageName) {
147    checkNotNull(packageName);
148    ImmutableSet.Builder<ClassInfo> builder = ImmutableSet.builder();
149    for (ClassInfo classInfo : getTopLevelClasses()) {
150      if (classInfo.getPackageName().equals(packageName)) {
151        builder.add(classInfo);
152      }
153    }
154    return builder.build();
155  }
156
157  /**
158   * Returns all top level classes whose package name is {@code packageName} or starts with {@code
159   * packageName} followed by a '.'.
160   */
161  public ImmutableSet<ClassInfo> getTopLevelClassesRecursive(String packageName) {
162    checkNotNull(packageName);
163    String packagePrefix = packageName + '.';
164    ImmutableSet.Builder<ClassInfo> builder = ImmutableSet.builder();
165    for (ClassInfo classInfo : getTopLevelClasses()) {
166      if (classInfo.getName().startsWith(packagePrefix)) {
167        builder.add(classInfo);
168      }
169    }
170    return builder.build();
171  }
172
173  /**
174   * Represents a class path resource that can be either a class file or any other resource file
175   * loadable from the class path.
176   *
177   * @since 14.0
178   */
179  @Beta
180  public static class ResourceInfo {
181    private final String resourceName;
182
183    final ClassLoader loader;
184
185    static ResourceInfo of(String resourceName, ClassLoader loader) {
186      if (resourceName.endsWith(CLASS_FILE_NAME_EXTENSION)) {
187        return new ClassInfo(resourceName, loader);
188      } else {
189        return new ResourceInfo(resourceName, loader);
190      }
191    }
192
193    ResourceInfo(String resourceName, ClassLoader loader) {
194      this.resourceName = checkNotNull(resourceName);
195      this.loader = checkNotNull(loader);
196    }
197
198    /**
199     * Returns the url identifying the resource.
200     *
201     * <p>See {@link ClassLoader#getResource}
202     *
203     * @throws NoSuchElementException if the resource cannot be loaded through the class loader,
204     *     despite physically existing in the class path.
205     */
206    public final URL url() {
207      URL url = loader.getResource(resourceName);
208      if (url == null) {
209        throw new NoSuchElementException(resourceName);
210      }
211      return url;
212    }
213
214    /**
215     * Returns a {@link ByteSource} view of the resource from which its bytes can be read.
216     *
217     * @throws NoSuchElementException if the resource cannot be loaded through the class loader,
218     *     despite physically existing in the class path.
219     * @since 20.0
220     */
221    public final ByteSource asByteSource() {
222      return Resources.asByteSource(url());
223    }
224
225    /**
226     * Returns a {@link CharSource} view of the resource from which its bytes can be read as
227     * characters decoded with the given {@code charset}.
228     *
229     * @throws NoSuchElementException if the resource cannot be loaded through the class loader,
230     *     despite physically existing in the class path.
231     * @since 20.0
232     */
233    public final CharSource asCharSource(Charset charset) {
234      return Resources.asCharSource(url(), charset);
235    }
236
237    /** Returns the fully qualified name of the resource. Such as "com/mycomp/foo/bar.txt". */
238    public final String getResourceName() {
239      return resourceName;
240    }
241
242    @Override
243    public int hashCode() {
244      return resourceName.hashCode();
245    }
246
247    @Override
248    public boolean equals(Object obj) {
249      if (obj instanceof ResourceInfo) {
250        ResourceInfo that = (ResourceInfo) obj;
251        return resourceName.equals(that.resourceName) && loader == that.loader;
252      }
253      return false;
254    }
255
256    // Do not change this arbitrarily. We rely on it for sorting ResourceInfo.
257    @Override
258    public String toString() {
259      return resourceName;
260    }
261  }
262
263  /**
264   * Represents a class that can be loaded through {@link #load}.
265   *
266   * @since 14.0
267   */
268  @Beta
269  public static final class ClassInfo extends ResourceInfo {
270    private final String className;
271
272    ClassInfo(String resourceName, ClassLoader loader) {
273      super(resourceName, loader);
274      this.className = getClassName(resourceName);
275    }
276
277    /**
278     * Returns the package name of the class, without attempting to load the class.
279     *
280     * <p>Behaves identically to {@link Package#getName()} but does not require the class (or
281     * package) to be loaded.
282     */
283    public String getPackageName() {
284      return Reflection.getPackageName(className);
285    }
286
287    /**
288     * Returns the simple name of the underlying class as given in the source code.
289     *
290     * <p>Behaves identically to {@link Class#getSimpleName()} but does not require the class to be
291     * loaded.
292     */
293    public String getSimpleName() {
294      int lastDollarSign = className.lastIndexOf('$');
295      if (lastDollarSign != -1) {
296        String innerClassName = className.substring(lastDollarSign + 1);
297        // local and anonymous classes are prefixed with number (1,2,3...), anonymous classes are
298        // entirely numeric whereas local classes have the user supplied name as a suffix
299        return CharMatcher.digit().trimLeadingFrom(innerClassName);
300      }
301      String packageName = getPackageName();
302      if (packageName.isEmpty()) {
303        return className;
304      }
305
306      // Since this is a top level class, its simple name is always the part after package name.
307      return className.substring(packageName.length() + 1);
308    }
309
310    /**
311     * Returns the fully qualified name of the class.
312     *
313     * <p>Behaves identically to {@link Class#getName()} but does not require the class to be
314     * loaded.
315     */
316    public String getName() {
317      return className;
318    }
319
320    /**
321     * Loads (but doesn't link or initialize) the class.
322     *
323     * @throws LinkageError when there were errors in loading classes that this class depends on.
324     *     For example, {@link NoClassDefFoundError}.
325     */
326    public Class<?> load() {
327      try {
328        return loader.loadClass(className);
329      } catch (ClassNotFoundException e) {
330        // Shouldn't happen, since the class name is read from the class path.
331        throw new IllegalStateException(e);
332      }
333    }
334
335    @Override
336    public String toString() {
337      return className;
338    }
339  }
340
341  /**
342   * Abstract class that scans through the class path represented by a {@link ClassLoader} and calls
343   * {@link #scanDirectory} and {@link #scanJarFile} for directories and jar files on the class path
344   * respectively.
345   */
346  abstract static class Scanner {
347
348    // We only scan each file once independent of the classloader that resource might be associated
349    // with.
350    private final Set<File> scannedUris = Sets.newHashSet();
351
352    public final void scan(ClassLoader classloader) throws IOException {
353      for (Entry<File, ClassLoader> entry : getClassPathEntries(classloader).entrySet()) {
354        scan(entry.getKey(), entry.getValue());
355      }
356    }
357
358    /** Called when a directory is scanned for resource files. */
359    protected abstract void scanDirectory(ClassLoader loader, File directory) throws IOException;
360
361    /** Called when a jar file is scanned for resource entries. */
362    protected abstract void scanJarFile(ClassLoader loader, JarFile file) throws IOException;
363
364    @VisibleForTesting
365    final void scan(File file, ClassLoader classloader) throws IOException {
366      if (scannedUris.add(file.getCanonicalFile())) {
367        scanFrom(file, classloader);
368      }
369    }
370
371    private void scanFrom(File file, ClassLoader classloader) throws IOException {
372      try {
373        if (!file.exists()) {
374          return;
375        }
376      } catch (SecurityException e) {
377        logger.warning("Cannot access " + file + ": " + e);
378        // TODO(emcmanus): consider whether to log other failure cases too.
379        return;
380      }
381      if (file.isDirectory()) {
382        scanDirectory(classloader, file);
383      } else {
384        scanJar(file, classloader);
385      }
386    }
387
388    private void scanJar(File file, ClassLoader classloader) throws IOException {
389      JarFile jarFile;
390      try {
391        jarFile = new JarFile(file);
392      } catch (IOException e) {
393        // Not a jar file
394        return;
395      }
396      try {
397        for (File path : getClassPathFromManifest(file, jarFile.getManifest())) {
398          scan(path, classloader);
399        }
400        scanJarFile(classloader, jarFile);
401      } finally {
402        try {
403          jarFile.close();
404        } catch (IOException ignored) {
405        }
406      }
407    }
408
409    /**
410     * Returns the class path URIs specified by the {@code Class-Path} manifest attribute, according
411     * to <a
412     * href="http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Main_Attributes">JAR
413     * File Specification</a>. If {@code manifest} is null, it means the jar file has no manifest,
414     * and an empty set will be returned.
415     */
416    @VisibleForTesting
417    static ImmutableSet<File> getClassPathFromManifest(
418        File jarFile, @NullableDecl Manifest manifest) {
419      if (manifest == null) {
420        return ImmutableSet.of();
421      }
422      ImmutableSet.Builder<File> builder = ImmutableSet.builder();
423      String classpathAttribute =
424          manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH.toString());
425      if (classpathAttribute != null) {
426        for (String path : CLASS_PATH_ATTRIBUTE_SEPARATOR.split(classpathAttribute)) {
427          URL url;
428          try {
429            url = getClassPathEntry(jarFile, path);
430          } catch (MalformedURLException e) {
431            // Ignore bad entry
432            logger.warning("Invalid Class-Path entry: " + path);
433            continue;
434          }
435          if (url.getProtocol().equals("file")) {
436            builder.add(toFile(url));
437          }
438        }
439      }
440      return builder.build();
441    }
442
443    @VisibleForTesting
444    static ImmutableMap<File, ClassLoader> getClassPathEntries(ClassLoader classloader) {
445      LinkedHashMap<File, ClassLoader> entries = Maps.newLinkedHashMap();
446      // Search parent first, since it's the order ClassLoader#loadClass() uses.
447      ClassLoader parent = classloader.getParent();
448      if (parent != null) {
449        entries.putAll(getClassPathEntries(parent));
450      }
451      for (URL url : getClassLoaderUrls(classloader)) {
452        if (url.getProtocol().equals("file")) {
453          File file = toFile(url);
454          if (!entries.containsKey(file)) {
455            entries.put(file, classloader);
456          }
457        }
458      }
459      return ImmutableMap.copyOf(entries);
460    }
461
462    private static ImmutableList<URL> getClassLoaderUrls(ClassLoader classloader) {
463      if (classloader instanceof URLClassLoader) {
464        return ImmutableList.copyOf(((URLClassLoader) classloader).getURLs());
465      }
466      if (classloader.equals(ClassLoader.getSystemClassLoader())) {
467        return parseJavaClassPath();
468      }
469      return ImmutableList.of();
470    }
471
472    /**
473     * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain
474     * System#getProperty system property}.
475     */
476    @VisibleForTesting // TODO(b/65488446): Make this a public API.
477    static ImmutableList<URL> parseJavaClassPath() {
478      ImmutableList.Builder<URL> urls = ImmutableList.builder();
479      for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) {
480        try {
481          try {
482            urls.add(new File(entry).toURI().toURL());
483          } catch (SecurityException e) { // File.toURI checks to see if the file is a directory
484            urls.add(new URL("file", null, new File(entry).getAbsolutePath()));
485          }
486        } catch (MalformedURLException e) {
487          logger.log(WARNING, "malformed classpath entry: " + entry, e);
488        }
489      }
490      return urls.build();
491    }
492
493    /**
494     * Returns the absolute uri of the Class-Path entry value as specified in <a
495     * href="http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Main_Attributes">JAR
496     * File Specification</a>. Even though the specification only talks about relative urls,
497     * absolute urls are actually supported too (for example, in Maven surefire plugin).
498     */
499    @VisibleForTesting
500    static URL getClassPathEntry(File jarFile, String path) throws MalformedURLException {
501      return new URL(jarFile.toURI().toURL(), path);
502    }
503  }
504
505  @VisibleForTesting
506  static final class DefaultScanner extends Scanner {
507    private final SetMultimap<ClassLoader, String> resources =
508        MultimapBuilder.hashKeys().linkedHashSetValues().build();
509
510    ImmutableSet<ResourceInfo> getResources() {
511      ImmutableSet.Builder<ResourceInfo> builder = ImmutableSet.builder();
512      for (Entry<ClassLoader, String> entry : resources.entries()) {
513        builder.add(ResourceInfo.of(entry.getValue(), entry.getKey()));
514      }
515      return builder.build();
516    }
517
518    @Override
519    protected void scanJarFile(ClassLoader classloader, JarFile file) {
520      Enumeration<JarEntry> entries = file.entries();
521      while (entries.hasMoreElements()) {
522        JarEntry entry = entries.nextElement();
523        if (entry.isDirectory() || entry.getName().equals(JarFile.MANIFEST_NAME)) {
524          continue;
525        }
526        resources.get(classloader).add(entry.getName());
527      }
528    }
529
530    @Override
531    protected void scanDirectory(ClassLoader classloader, File directory) throws IOException {
532      Set<File> currentPath = new HashSet<>();
533      currentPath.add(directory.getCanonicalFile());
534      scanDirectory(directory, classloader, "", currentPath);
535    }
536
537    /**
538     * Recursively scan the given directory, adding resources for each file encountered. Symlinks
539     * which have already been traversed in the current tree path will be skipped to eliminate
540     * cycles; otherwise symlinks are traversed.
541     *
542     * @param directory the root of the directory to scan
543     * @param classloader the classloader that includes resources found in {@code directory}
544     * @param packagePrefix resource path prefix inside {@code classloader} for any files found
545     *     under {@code directory}
546     * @param currentPath canonical files already visited in the current directory tree path, for
547     *     cycle elimination
548     */
549    private void scanDirectory(
550        File directory, ClassLoader classloader, String packagePrefix, Set<File> currentPath)
551        throws IOException {
552      File[] files = directory.listFiles();
553      if (files == null) {
554        logger.warning("Cannot read directory " + directory);
555        // IO error, just skip the directory
556        return;
557      }
558      for (File f : files) {
559        String name = f.getName();
560        if (f.isDirectory()) {
561          File deref = f.getCanonicalFile();
562          if (currentPath.add(deref)) {
563            scanDirectory(deref, classloader, packagePrefix + name + "/", currentPath);
564            currentPath.remove(deref);
565          }
566        } else {
567          String resourceName = packagePrefix + name;
568          if (!resourceName.equals(JarFile.MANIFEST_NAME)) {
569            resources.get(classloader).add(resourceName);
570          }
571        }
572      }
573    }
574  }
575
576  @VisibleForTesting
577  static String getClassName(String filename) {
578    int classNameEnd = filename.length() - CLASS_FILE_NAME_EXTENSION.length();
579    return filename.substring(0, classNameEnd).replace('/', '.');
580  }
581
582  // TODO(benyu): Try java.nio.file.Paths#get() when Guava drops JDK 6 support.
583  @VisibleForTesting
584  static File toFile(URL url) {
585    checkArgument(url.getProtocol().equals("file"));
586    try {
587      return new File(url.toURI()); // Accepts escaped characters like %20.
588    } catch (URISyntaxException e) { // URL.toURI() doesn't escape chars.
589      return new File(url.getPath()); // Accepts non-escaped chars like space.
590    }
591  }
592}