public class ClassLoaderUtils extends Object
| Constructor and Description |
|---|
ClassLoaderUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
setContextClassLoader(Thread thread,
ClassLoader currentClassLoader,
ClassLoader newClassLoader)
Sets
newClassLoader as the context class loader for the thread, as long as said classloader is not the same
instance as currentClassLoader. |
public static void setContextClassLoader(Thread thread, ClassLoader currentClassLoader, ClassLoader newClassLoader)
newClassLoader as the context class loader for the thread, as long as said classloader is not the same
instance as currentClassLoader.
Since obtaining and setting the context classloader from a thread are expensive operations, the purpose of this method is to
avoid performing those operations when possible, which is why the two classloaders are tested not to be the same before
performing the set operation. For this method to make sense, currentClassLoader should actually be the current
context classloader from the thread.
This is how a typical use should look like:
Thread thread = Thread.currentThread();
ClassLoader currentClassLoader = thread.getContextClassLoader();
ClassLoader newClassLoader = getNewContextClassLoader(); // this one depends on your logic
ClassUtils.setContextClassLoader(thread, currentClassLoader, newClassLoader);
try {
// execute your logic
} finally {
// set things back as they were by reversing the arguments order
ClassUtils.setContextClassLoader(thread, newClassLoader, currentClassLoader);
}
thread - the thread which context classloader is to be changedcurrentClassLoader - the thread's current context classloadernewClassLoader - the new classloader to be setCopyright © 2024. All rights reserved.