-
Notifications
You must be signed in to change notification settings - Fork 37
Description
In continuation to #149 , I am opening this discussion on potential additional changes to support a custom thread ContextClassLoader
. The Java17 feature removal of a custom SystemClassLoader
has another ramification that I want to fix in a separate PR once its ready. When new threads are formed, they inherit a Thread ContextClassLoader
from their parent thread, which in many cases might be the SystemClassLoader
. When I could substitute the scl, I was able to ensure the new threads had a ClassLoader that could reach their necessary classes because eventually every ClassLoader had a parent ClassLoader of the scl. Since this is not possible in newer versions of Java, I have created a temporary workaround as I explore options to fix the problem. I think in time I could make a better long-term feature addition. Some example code of how the problem manifests on the rust-side:
let jvm: Jvm = Jvm::attach_thread().expect("unable to attach to the JVM");
// this jvm might not have the necessary ClassLoader.
// It will always have j4rs and its dependencies, but not necessarily additional Java classes that the subsequent rust code relies on.
// my current quick-fix approach that does jvm.invoke_static("java.lang.Thread"...)... code to update the ClassLoader.
update_thread_classloader(&jvm);
// now calls to jvm.invoke() and jvm.invoke_static() can work with custom Classes outside of original j4rs+dependencies
// Unfortunately to do this quickly, I am holding a reference to a j4rs Instance of the ClassLoader of interest
// and provide it as an InvocationArg to the invoke() call.
// Instead of my custom workaround above, two potential designs I am exploring:
// a) add a second jvm method call that sets up the classloader. Something like
jvm.set_thread_classloader(...parameters...)
// b) Do everything in a single Jvm method call, replacing the attach_thread() line with
let jvm = Jvm::attach_thread_with_classloader(...parameters...)
The design work is to figure out how we want to hold onto the Instance<ClassLoader>
, such as add a Field to the Java-side j4rs
code or other mechanism that makes it available to j4rs
rust-side code. @astonbitecode, if you have any preferences to the implementation of this feature, I will see about getting this change done. Thank you.