Scans the source of a ClassLoader and finds all loadable classes and resources. Warning: Currently only URLClassLoader and only file:// urls are supported. @author Ben Yu @since 14.0
| 61 | * @since 14.0 |
| 62 | */ |
| 63 | @Beta |
| 64 | public final class ClassPath { |
| 65 | private static final Logger logger = Logger.getLogger(ClassPath.class.getName()); |
| 66 | |
| 67 | private static final Predicate<ClassInfo> IS_TOP_LEVEL = |
| 68 | new Predicate<ClassInfo>() { |
| 69 | @Override |
| 70 | public boolean apply(ClassInfo info) { |
| 71 | return info.className.indexOf('$') == -1; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | /** Separator for the Class-Path manifest attribute value in jar files. */ |
| 76 | private static final Splitter CLASS_PATH_ATTRIBUTE_SEPARATOR = |
| 77 | Splitter.on(" ").omitEmptyStrings(); |
| 78 | |
| 79 | private static final String CLASS_FILE_NAME_EXTENSION = ".class"; |
| 80 | |
| 81 | private final ImmutableSet<ResourceInfo> resources; |
| 82 | |
| 83 | private ClassPath(ImmutableSet<ResourceInfo> resources) { |
| 84 | this.resources = resources; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns a {@code ClassPath} representing all classes and resources loadable from {@code |
| 89 | * classloader} and its parent class loaders. |
| 90 | * |
| 91 | * <p><b>Warning:</b> Currently only {@link URLClassLoader} and only {@code file://} urls are |
| 92 | * supported. |
| 93 | * |
| 94 | * @throws IOException if the attempt to read class path resources (jar files or directories) |
| 95 | * failed. |
| 96 | */ |
| 97 | public static ClassPath from(ClassLoader classloader) throws IOException { |
| 98 | DefaultScanner scanner = new DefaultScanner(); |
| 99 | scanner.scan(classloader); |
| 100 | return new ClassPath(scanner.getResources()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns all resources loadable from the current class path, including the class files of all |
| 105 | * loadable classes but excluding the "META-INF/MANIFEST.MF" file. |
| 106 | */ |
| 107 | public ImmutableSet<ResourceInfo> getResources() { |
| 108 | return resources; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns all classes loadable from the current class path. |
| 113 | * |
| 114 | * @since 16.0 |
| 115 | */ |
| 116 | public ImmutableSet<ClassInfo> getAllClasses() { |
| 117 | return FluentIterable.from(resources).filter(ClassInfo.class).toSet(); |
| 118 | } |
| 119 | |
| 120 | /** Returns all top level classes loadable from the current class path. */ |
nothing calls this directly
no test coverage detected