From 40997323a598045510a525aecd51dd84a5c1c64b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 9 Apr 2018 15:38:33 +0200 Subject: [PATCH] Improve ClassLoaderUtils.loadClassWithTCCL() 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 (cherry picked from commit 4a3cc19a42b3740ec518569ca274153b66f2bc82) --- .../yangtools/util/ClassLoaderUtils.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/ClassLoaderUtils.java b/common/util/src/main/java/org/opendaylight/yangtools/util/ClassLoaderUtils.java index 6e65b03658..b5064080ad 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/ClassLoaderUtils.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/ClassLoaderUtils.java @@ -130,14 +130,28 @@ 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); } - public static Class tryToLoadClassWithTCCL(final String fullyQualifiedName) { + 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(fullyQualifiedName); + return loadClass(tccl, fullyQualifiedClassName); } catch (final ClassNotFoundException e) { - LOG.debug("Failed to load class {}", fullyQualifiedName, e); + LOG.debug("Failed to load class {}", fullyQualifiedClassName, e); return null; } } -- 2.36.6