Improve ClassLoaderUtils.loadClassWithTCCL() 59/70659/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 9 Apr 2018 13:38:33 +0000 (15:38 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 9 Apr 2018 14:36:51 +0000 (14:36 +0000)
There are scenarios when we do not have a Thread Context Class Loader,
such as when executing from Netty's GlobalEventExecutor. When we attempt
to load a class in that scenario, the method will throw a NPE, which
exposes users which can deal with the class not being able to load
to a RuntimeException -- preventing recovery.

Detect the case when TCCL is null and report a failure to load the class,
noting the reason for the failure.

Change-Id: Ia6837ba451d290a2070bc24e9e7088c60275cae0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/util/src/main/java/org/opendaylight/yangtools/util/ClassLoaderUtils.java

index b380d323e49282384c217ad59186d23abab92783..e3e7a2c692411e4cc7c568f1f319b33e02924393 100644 (file)
@@ -215,13 +215,27 @@ public final class ClassLoaderUtils {
     }
 
     public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
-        return loadClass(Thread.currentThread().getContextClassLoader(), name);
+        final Thread thread = Thread.currentThread();
+        final ClassLoader tccl = thread.getContextClassLoader();
+        if (tccl == null) {
+            throw new ClassNotFoundException("Thread " + thread + " does not have a Context Class Loader, cannot load "
+                    + name);
+        }
+        return loadClass(tccl, name);
     }
 
     // FIXME: 3.0.0: Document and return Optional
     public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedClassName) {
+        final Thread thread = Thread.currentThread();
+        final ClassLoader tccl = thread.getContextClassLoader();
+        if (tccl == null) {
+            LOG.debug("Thread {} does not have a Context Class Loader, not loading class {}", thread,
+                fullyQualifiedClassName);
+            return null;
+        }
+
         try {
-            return loadClassWithTCCL(fullyQualifiedClassName);
+            return loadClass(tccl, fullyQualifiedClassName);
         } catch (final ClassNotFoundException e) {
             LOG.debug("Failed to load class {}", fullyQualifiedClassName, e);
             return null;