@Public public final class TemporaryClassLoaderContext extends Object implements AutoCloseable
try (TemporaryClassLoaderContext ignored = TemporaryClassLoaderContext.of(classloader)) {
// code that needs the context class loader
}
This is conceptually the same as the code below.
ClassLoader original = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(classloader);
try {
// code that needs the context class loader
} finally {
Thread.currentThread().setContextClassLoader(original);
}
Modifier and Type | Method and Description |
---|---|
void |
close() |
static TemporaryClassLoaderContext |
of(ClassLoader cl)
Sets the context class loader to the given ClassLoader and returns a resource that sets it
back to the current context ClassLoader when the resource is closed.
|
public static TemporaryClassLoaderContext of(ClassLoader cl)
try (TemporaryClassLoaderContext ignored = TemporaryClassLoaderContext.of(classloader)) {
// code that needs the context class loader
}
public void close()
close
in interface AutoCloseable
Copyright © 2014–2024 The Apache Software Foundation. All rights reserved.