public static void main(String[] args) {
String pkgs = "com";
loadByClassLoader(pkgs);
//loadByResolverUtil();
}
public static void loadByResolverUtil(String pkgs){
ResolverUtil<Class> resolver = new ResolverUtil<Class>();
resolver.findInPackage(new Test() {
public boolean matches(Class type) {
// TODO: should also find annotated classes
// return (Action.class.isAssignableFrom(type) ||
// type.getSimpleName().endsWith("Action"));
return true;
}
}, pkgs);
System.out.println(resolver.getClasses().size());
for (Class<?> actionClass : resolver.getClasses()) {
System.out.println(actionClass.getName());
}
}
public static void loadByClassLoader(String pkgs) {
ClassLoader loader = ClassLoader.getSystemClassLoader();
Enumeration<URL> urls;
try {
urls = loader.getResources(pkgs);
while (urls.hasMoreElements()) {
String urlPath = urls.nextElement().getFile();
// urlPath = URLDecoder.decode(urlPath, "UTF-8");
// If it's a file in a directory, trim the stupid file: spec
if (urlPath.startsWith("file:"))
urlPath = urlPath.substring(5);
// Else it's in a JAR, grab the path to the jar
if (urlPath.indexOf('!') > 0)
urlPath = urlPath.substring(0, urlPath.indexOf('!'));
// log.info("Scanning for classes in [" + urlPath + "] matching
// criteria: " + test);
File file = new File(urlPath);
if (file.isDirectory())
loadImplementationsInDirectory(pkgs, file);
else
loadImplementationsInJar(pkgs, file);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void loadImplementationsInDirectory(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(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());
}
}
ClassLoader Test
最新推荐文章于 2022-06-26 12:23:47 发布