| 11 | import java.util.jar.JarInputStream; |
| 12 | |
| 13 | public class ClasspathScanner { |
| 14 | |
| 15 | private String pkg; |
| 16 | private boolean recur = false; |
| 17 | |
| 18 | protected String getPackage() { |
| 19 | return pkg; |
| 20 | } |
| 21 | |
| 22 | public ClasspathScanner(String pkg, boolean subpackages) { |
| 23 | recur = subpackages; |
| 24 | sanitizePackage(pkg); |
| 25 | } |
| 26 | |
| 27 | public ClasspathScanner(String pkg) { |
| 28 | sanitizePackage(pkg); |
| 29 | } |
| 30 | |
| 31 | private void sanitizePackage(String pkgName) { |
| 32 | if ((pkgName == null) || (pkgName.trim().length() == 0)) throw new IllegalArgumentException("Base package cannot be null"); |
| 33 | pkg = pkgName.replace('.', '/'); |
| 34 | if (pkg.endsWith("*")) pkg = pkg.substring(0, pkg.length() - 1); |
| 35 | if (pkg.endsWith("/")) pkg = pkg.substring(0, pkg.length() - 1); |
| 36 | } |
| 37 | |
| 38 | protected ClassLoader getClassLoader() { |
| 39 | return Thread.currentThread().getContextClassLoader(); |
| 40 | } |
| 41 | |
| 42 | protected boolean isJARPath(String path) { |
| 43 | return (path.indexOf("!") > 0) & (path.indexOf(".jar") > 0); |
| 44 | } |
| 45 | |
| 46 | protected void add(Set<String> classes, String className) { |
| 47 | if ((className.startsWith(pkg)) && (className.endsWith(".class"))) { |
| 48 | boolean add = recur ? true : className.substring(pkg.length() + 1).indexOf("/") < 0; |
| 49 | if (add) classes.add(className.substring(0, className.length() - 6).replace('/', '.')); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | protected Set<String> getClassesFromJAR(String path) throws IOException { |
| 54 | Set<String> classes = new HashSet<String>(); |
| 55 | String jarPath = path.substring(0, path.indexOf("!")).substring(path.indexOf(":") + 1); |
| 56 | JarInputStream jarFile = new JarInputStream(new FileInputStream(jarPath)); |
| 57 | JarEntry jarEntry; |
| 58 | do { |
| 59 | jarEntry = jarFile.getNextJarEntry(); |
| 60 | if (jarEntry != null) add(classes, jarEntry.getName()); |
| 61 | } while (jarEntry != null); |
| 62 | return classes; |
| 63 | } |
| 64 | |
| 65 | protected Set<String> getClassesFromDirectory(String path) { |
| 66 | Set<String> classes = new HashSet<String>(); |
| 67 | File directory = new File(path); |
| 68 | if (directory.exists()) { |
| 69 | for (String file : directory.list()) { |
| 70 | File f = new File(directory, file); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…