From 73eb04a0d3245381f9d466e6ed1b9b88a0f9ecd9 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 8 May 2013 13:23:10 -0400 Subject: [PATCH] Help SezPoz a little SezPoz uses the current Thread's context class loader to discover our plugins. But if the DefaultPluginFinder cannot be loaded by said class loader, it is safe to assume that SezPoz cannot find the plugins wanted by the caller, either. So let's not use the current Thread's context class loader in that case, but the best we can do in that case: the class loader that found the DefaultPluginFinder. Signed-off-by: Johannes Schindelin --- .../scijava/plugin/DefaultPluginFinder.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/scijava/plugin/DefaultPluginFinder.java b/src/main/java/org/scijava/plugin/DefaultPluginFinder.java index 3c34ba6a5..20c58a30c 100644 --- a/src/main/java/org/scijava/plugin/DefaultPluginFinder.java +++ b/src/main/java/org/scijava/plugin/DefaultPluginFinder.java @@ -110,7 +110,24 @@ private PluginInfo createInfo( private ClassLoader getClassLoader() { if (customClassLoader != null) return customClassLoader; - return Thread.currentThread().getContextClassLoader(); + + /* + * If not even the current class can be found by the current + * Thread's context class loader, chances are that the plugins + * the caller tries to discover using this plugin finder cannot + * be found, either. Therefore let's use the current class' + * class loader in that case. This is not completely + * fool-proof, but better than nothing. + */ + final ClassLoader thisLoader = getClass().getClassLoader(); + final ClassLoader contextLoader = + Thread.currentThread().getContextClassLoader(); + for (ClassLoader loader = contextLoader; + loader != null; + loader = loader.getParent()) { + if (thisLoader == loader) return contextLoader; + } + return thisLoader; } }