public class ClassScanner
{
// scan extraClasspath for specific classes
public List<Class<?>> scanClasspathForClass(String scanCriteria)
{
...
}
public static Class<?> reloadClass(Class<?> clazz, ClassLoader clazzLoader)
{
// Question: how to reload a loaded class (ClassScanner in this example) with a different arbitrary ClassLoader?
}
// an example of how reloadClass() would be used
// in real case, this function would be in different class
public List<Class<?>> scan(URL[] extraClasspath)
{
URLClassLoader urlClazzLoader = new URLClassLoader(extraClasspath, null);
Class<?> newClass = reloadClass(ClassScanner.class, urlClazzLoader);
return ((ClassScanner) newClass.newInstance()).scanClasspathForClass();
}
}
上面的代码演示了这个问题以及为什么它是一个问题。我需要实施reloadClass()
. 我想知道 Java 1.6 中是否有可靠的解决方案。一个有用的参考是Find where java class is loaded from。
感谢帮助!