ClassLoader Test


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());
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值