LUCENE-3919: Die, context class loader, die. Also don't initialize (run static ctors) unrelated classes!

@UweSays: "If you get the context classloader from a thread, in most cases you are doing something wrong because you don't understand how Java classloading works."

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1310893 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-04-07 22:27:57 +00:00
parent 7154c5466d
commit bdaa79206d
1 changed files with 16 additions and 15 deletions

View File

@ -55,7 +55,8 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
@BeforeClass
public static void beforeClass() throws Exception {
List<Class<?>> analysisClasses = getClassesForPackage("org.apache.lucene.analysis");
List<Class<?>> analysisClasses = new ArrayList<Class<?>>();
getClassesForPackage("org.apache.lucene.analysis", analysisClasses);
tokenizers = new ArrayList<Class<? extends Tokenizer>>();
tokenfilters = new ArrayList<Class<? extends TokenFilter>>();
charfilters = new ArrayList<Class<? extends CharStream>>();
@ -274,17 +275,16 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
}
}
private static List<Class<?>> getClassesForPackage(String pckgname) throws Exception {
ArrayList<File> directories = new ArrayList<File>();
ClassLoader cld = Thread.currentThread().getContextClassLoader();
String path = pckgname.replace('.', '/');
Enumeration<URL> resources = cld.getResources(path);
private static void getClassesForPackage(String pckgname, List<Class<?>> classes) throws Exception {
final ArrayList<File> directories = new ArrayList<File>();
final ClassLoader cld = TestRandomChains.class.getClassLoader();
final String path = pckgname.replace('.', '/');
final Enumeration<URL> resources = cld.getResources(path);
while (resources.hasMoreElements()) {
final File f = new File(resources.nextElement().toURI());
directories.add(f);
}
ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
for (File directory : directories) {
if (directory.exists()) {
String[] files = directory.list();
@ -292,19 +292,20 @@ public class TestRandomChains extends BaseTokenStreamTestCase {
if (new File(directory, file).isDirectory()) {
// recurse
String subPackage = pckgname + "." + file;
classes.addAll(getClassesForPackage(subPackage));
getClassesForPackage(subPackage, classes);
}
if (file.endsWith(".class")) {
String clazzName = file.substring(0, file.length() - 6);
// exclude Test classes that happen to be in these packages.
// class.ForName'ing some of them can cause trouble.
if (!clazzName.endsWith("Test") && !clazzName.startsWith("Test")) {
classes.add(Class.forName(pckgname + '.' + clazzName));
// Don't run static initializers, as we won't use most of them.
// Java will do that automatically once accessed/instantiated.
classes.add(Class.forName(pckgname + '.' + clazzName, false, cld));
}
}
}
}
}
return classes;
}
}