From bdaa79206d98ee0e3a33312311aecbfa7515fb60 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sat, 7 Apr 2012 22:27:57 +0000 Subject: [PATCH] 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 --- .../analysis/core/TestRandomChains.java | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java b/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java index b463acf9e75..017793cdc78 100644 --- a/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java +++ b/modules/analysis/common/src/test/org/apache/lucene/analysis/core/TestRandomChains.java @@ -55,7 +55,8 @@ public class TestRandomChains extends BaseTokenStreamTestCase { @BeforeClass public static void beforeClass() throws Exception { - List> analysisClasses = getClassesForPackage("org.apache.lucene.analysis"); + List> analysisClasses = new ArrayList>(); + getClassesForPackage("org.apache.lucene.analysis", analysisClasses); tokenizers = new ArrayList>(); tokenfilters = new ArrayList>(); charfilters = new ArrayList>(); @@ -274,17 +275,16 @@ public class TestRandomChains extends BaseTokenStreamTestCase { } } - private static List> getClassesForPackage(String pckgname) throws Exception { - ArrayList directories = new ArrayList(); - ClassLoader cld = Thread.currentThread().getContextClassLoader(); - String path = pckgname.replace('.', '/'); - Enumeration resources = cld.getResources(path); + private static void getClassesForPackage(String pckgname, List> classes) throws Exception { + final ArrayList directories = new ArrayList(); + final ClassLoader cld = TestRandomChains.class.getClassLoader(); + final String path = pckgname.replace('.', '/'); + final Enumeration resources = cld.getResources(path); while (resources.hasMoreElements()) { final File f = new File(resources.nextElement().toURI()); directories.add(f); } - ArrayList> classes = new ArrayList>(); 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)); - } + 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")) { + // 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; } }