if (file.isDirectory()) loadImplementationsInDirectory(packageOrClass, file); else if (file.getName().endsWith(".class")) addIfMatching(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 */ public static void loadImplementationsInJar(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) && name.endsWith(".class")) addIfMatching(name); } } catch (IOException ioe) { System.out.println("Could not search jar file '" + jarfile + "' for classes matching criteria: " + " due to an IOException"); } }
public static void addIfMatching(String fqn) { try { String externalName = fqn.substring(0, fqn.indexOf('.')).replace('/', '.'); ClassLoader loader = ClassLoader.getSystemClassLoader(); /* * if (log.isDebugEnabled()) { log.debug("Checking to see if class " + * externalName + " matches criteria [" + test + "]"); } */
Class type = loader.loadClass(externalName); System.out.println(type.getName()); } catch (Throwable t) { System.out.println("Could not examine class '" + fqn + "' due to a " + t.getClass().getName() + " with message: " + t.getMessage()); } }