Skip to content

Commit cd67323

Browse files
committed
Encapsulate intelligent class loader access
According to the javadoc, Thread#getContextClassLoader() can return null. In that case, we want to fall back to the system class loader.
1 parent 9de270c commit cd67323

File tree

8 files changed

+33
-15
lines changed

8 files changed

+33
-15
lines changed

src/main/java/org/scijava/Context.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,18 @@ public static List<Class<? extends Service>> serviceClassList(
440440
Arrays.asList(serviceClasses) : Arrays.asList(Service.class);
441441
}
442442

443+
/**
444+
* Gets the class loader to use. This will be the current thread's context
445+
* class loader if non-null; otherwise it will be the system class loader.
446+
*
447+
* @see Thread#getContextClassLoader()
448+
* @see ClassLoader#getSystemClassLoader()
449+
*/
450+
public static ClassLoader getClassLoader() {
451+
final ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
452+
return contextCL != null ? contextCL : ClassLoader.getSystemClassLoader();
453+
}
454+
443455
// -- Helper methods --
444456

445457
private List<Field> getParameterFields(final Object o) {
@@ -530,8 +542,7 @@ private String createMissingServiceMessage(
530542
final Class<? extends Service> serviceType)
531543
{
532544
final String nl = System.getProperty("line.separator");
533-
final ClassLoader classLoader = //
534-
Thread.currentThread().getContextClassLoader();
545+
final ClassLoader classLoader = getClassLoader();
535546
final StringBuilder msg = new StringBuilder(
536547
"Required service is missing: " + serviceType.getName() + nl);
537548
msg.append("Context: " + this + nl);
@@ -569,5 +580,4 @@ private static List<Class<? extends Service>> services(final boolean empty) {
569580
private static boolean strict() {
570581
return !"false".equals(System.getProperty(STRICT_PROPERTY));
571582
}
572-
573583
}

src/main/java/org/scijava/annotations/AnnotationCombiner.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.HashSet;
4444
import java.util.Set;
4545

46+
import org.scijava.Context;
4647
import org.scijava.util.Combiner;
4748
import org.scijava.util.FileUtils;
4849

@@ -65,7 +66,7 @@ public void combine(File outputDirectory) throws Exception {
6566
}
6667
final Set<String> annotationFiles = getAnnotationFiles();
6768

68-
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
69+
final ClassLoader loader = Context.getClassLoader();
6970

7071
log("");
7172
log("Writing annotations to " + outputDirectory.getAbsolutePath());
@@ -92,7 +93,7 @@ public Set<String> getAnnotationFiles() throws IOException {
9293

9394
for (final String prefix : new String[] { PREFIX, LEGACY_PREFIX }) {
9495
final Enumeration<URL> directories =
95-
Thread.currentThread().getContextClassLoader().getResources(prefix);
96+
Context.getClassLoader().getResources(prefix);
9697
while (directories.hasMoreElements()) {
9798
final URL url = directories.nextElement();
9899
for (final URL annotationIndexURL : FileUtils.listContents(url)) {

src/main/java/org/scijava/annotations/EclipseHelper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.jar.JarFile;
4444
import java.util.jar.Manifest;
4545

46+
import org.scijava.Context;
4647
import org.scijava.util.FileUtils;
4748

4849
/**
@@ -297,7 +298,7 @@ else if (file.isDirectory()) {
297298
*/
298299
public static void main(final String... args) {
299300
System.setProperty(FORCE_ANNOTATION_INDEX_PROPERTY, "true");
300-
updateAnnotationIndex(Thread.currentThread().getContextClassLoader());
301+
updateAnnotationIndex(Context.getClassLoader());
301302
}
302303

303304
}

src/main/java/org/scijava/plugin/DefaultPluginFinder.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.regex.Pattern;
3939
import java.util.regex.PatternSyntaxException;
4040

41+
import org.scijava.Context;
4142
import org.scijava.annotations.Index;
4243
import org.scijava.annotations.IndexItem;
4344

@@ -113,8 +114,8 @@ private PluginInfo<SciJavaPlugin> createInfo(
113114
}
114115

115116
private ClassLoader getClassLoader() {
116-
if (customClassLoader != null) return customClassLoader;
117-
return Thread.currentThread().getContextClassLoader();
117+
return customClassLoader != null ? //
118+
customClassLoader : Context.getClassLoader();
118119
}
119120

120121
// -- Helper classes --

src/main/java/org/scijava/util/FileUtils.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
import java.util.regex.Matcher;
5959
import java.util.regex.Pattern;
6060

61+
import org.scijava.Context;
62+
6163
/**
6264
* Useful methods for working with file paths.
6365
*
@@ -664,7 +666,7 @@ public static Map<String, URL> findResources(final String regex,
664666
final String pathPrefix, final File baseDirectory)
665667
{
666668
// scan URL resource paths first
667-
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
669+
final ClassLoader loader = Context.getClassLoader();
668670
final ArrayList<URL> urls = new ArrayList<>();
669671
try {
670672
urls.addAll(Collections.list(loader.getResources(pathPrefix + "/")));

src/main/java/org/scijava/util/POM.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
import javax.xml.parsers.ParserConfigurationException;
4545

46+
import org.scijava.Context;
4647
import org.scijava.Versioned;
4748
import org.xml.sax.SAXException;
4849

@@ -280,8 +281,7 @@ public static POM getPOM(final Class<?> c, final String groupId,
280281
public static List<POM> getAllPOMs() {
281282
// find all META-INF/maven/ folders on the classpath
282283
final String pomPrefix = "META-INF/maven/";
283-
final ClassLoader classLoader =
284-
Thread.currentThread().getContextClassLoader();
284+
final ClassLoader classLoader = Context.getClassLoader();
285285
final Enumeration<URL> resources;
286286
try {
287287
resources = classLoader.getResources(pomPrefix);

src/main/java/org/scijava/util/ServiceCombiner.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545

4646
import javax.xml.ws.Service;
4747

48+
import org.scijava.Context;
49+
4850
/**
4951
* Combines {@link Service} information from all JAR files on the classpath.
5052
*
@@ -61,8 +63,7 @@ public void combine(final File outputDirectory) throws IOException {
6163
final Map<String, StringBuilder> files =
6264
new HashMap<>();
6365
final Enumeration<URL> directories =
64-
Thread.currentThread().getContextClassLoader().getResources(
65-
SERVICES_PREFIX);
66+
Context.getClassLoader().getResources(SERVICES_PREFIX);
6667

6768
// Iterate over all the service files
6869
while (directories.hasMoreElements()) {

src/main/java/org/scijava/util/Types.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
import java.util.Objects;
7070
import java.util.Set;
7171

72+
import org.scijava.Context;
73+
7274
/**
7375
* Utility class for working with generic types, fields and methods.
7476
* <p>
@@ -221,8 +223,8 @@ public static Class<?> load(final String name, final ClassLoader classLoader,
221223

222224
// load the class!
223225
try {
224-
final ClassLoader cl = classLoader == null ? //
225-
Thread.currentThread().getContextClassLoader() : classLoader;
226+
final ClassLoader cl = //
227+
classLoader != null ? classLoader : Context.getClassLoader();
226228
return cl.loadClass(className);
227229
}
228230
catch (final Throwable t) {

0 commit comments

Comments
 (0)