/** * Finds matches in a physical directory on a filesystem. Examines all * files within a directory - if the File object is not a directory, and ends with <i>.class</i> * the file is loaded and tested to see if it is acceptable according to the Test. Operates * recursively to find classes within a folder structure matching the package structure. * * @param test a Test used to filter the classes that are discovered * @param parent the package name up to this directory in the package hierarchy. E.g. if * /classes is in the classpath and we wish to examine files in /classes/org/apache then * the values of <i>parent</i> would be <i>org/apache</i> * @param location a File object representing a directory */ private void loadImplementationsInDirectory(Test test, String parent, File location) { File[] files = location.listFiles(); StringBuilder builder = null; for (File file : files) { builder = new StringBuilder(100); builder.append(parent).append("/").append(file.getName()); String packageOrClass = ( parent == null ? file.getName() : builder.toString() ); if (file.isDirectory()) { loadImplementationsInDirectory(test, packageOrClass, file); } else if (isTestApplicable(test, file.getName())) { addIfMatching(test, packageOrClass); } } }
/** * Finds matching classes within a jar files that contains a folder structure * matching the package structure. If the File is not a JarFile or does not exist a warning * will be logged, but no error will be raised. * * @param test a Test used to filter the classes that are discovered * @param parent the parent package under which classes must be in order to be considered * @param jarfile the jar file to be examined for classes */ private void loadImplementationsInJar(Test test, String parent, File jarfile) { try { JarEntry entry; JarInputStream jarStream = new JarInputStream(new FileInputStream(jarfile)); while ( (entry = jarStream.getNextJarEntry() ) != null) { String name = entry.getName(); if (!entry.isDirectory() && name.startsWith(parent) && isTestApplicable(test, name)) { addIfMatching(test, name); } } } catch (IOException ioe) { LOG.error("Could not search jar file '" + jarfile + "' for classes matching criteria: " + test + " due to an IOException", ioe); } }