Skip to content

Instantly share code, notes, and snippets.

@monksy
Created February 13, 2013 20:37
Show Gist options
  • Select an option

  • Save monksy/4948011 to your computer and use it in GitHub Desktop.

Select an option

Save monksy/4948011 to your computer and use it in GitHub Desktop.
This class is responsible for finding all of the classes within a package path. This does not work for remote packages. This code was based on the work found here: http://svn.apache.org/repos/asf/incubator/chukwa/trunk/src/main/java/org/apache/hadoop/chukwa/util/ClassUtils.java http://tapestry.1045711.n5.nabble.com/T5-Auto-Binding-of-DAOs-td5715…
import com.google.common.base.Preconditions;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ClassFinder {
public static String[] findClasses(String packageName)
throws ClassNotFoundException {
Preconditions.checkNotNull(packageName);
Preconditions.checkArgument(!packageName.isEmpty());
Set<String> classes = new HashSet<>();
ArrayList<File> directories = new ArrayList<>();
// Get a File object for the package
try {
ClassLoader cld = Thread.currentThread().getContextClassLoader();
if (cld == null) {
throw new ClassNotFoundException("Can't get class loader.");
}
String path = packageName.replace('.', '/');
// Ask for all resources for the path
Enumeration<URL> resources = cld.getResources(path);
while (resources.hasMoreElements()) {
URL res = resources.nextElement();
if (res.getProtocol().equalsIgnoreCase("jar")) {
JarURLConnection conn = (JarURLConnection) res.openConnection();
JarFile jar = conn.getJarFile();
for (JarEntry e : Collections.list(jar.entries())) {
String curName = e.getName();
if (curName.startsWith(packageName.replace('.', '/'))
&& curName.endsWith(".class") && !curName.contains("$")) {
String className =
e.getName().replace("/", ".").substring(0, curName.length() - 6);
// System.out.println(className);
classes.add(className);
}
}
} else {
directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
}
}
} catch (NullPointerException x) {
throw new ClassNotFoundException(packageName + " does not appear to be "
+ "a valid package (Null pointer exception)");
} catch (UnsupportedEncodingException encex) {
throw new ClassNotFoundException(packageName + " does not appear to be "
+ "a valid package (Unsupported encoding)");
} catch (IOException ioex) {
throw new ClassNotFoundException("IOException was thrown when trying "
+ "to get all resources for " + packageName);
}
for (File directory : directories) {
if (directory.exists()) {
// Get the list of the files contained in the package
String[] files = directory.list();
for (String cName : files) {
if (cName.toLowerCase().endsWith(".class")) {
cName = packageName + "."+ cName.substring(0, cName.length() - 6);
cName = cName.replaceAll("\\.\\.", "\\.");
classes.add(cName);
}
}
} else {
throw new ClassNotFoundException(packageName + " does not appear to be a valid package");
}
}
return classes.toArray(new String[classes.size()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment