RFR: 8335896: Source launcher should set TCCL
Christian Stein
cstein at openjdk.org
Tue Jul 16 08:11:51 UTC 2024
On Mon, 15 Jul 2024 16:07:12 GMT, Alan Bateman <alanb at openjdk.org> wrote:
>> Please review this change to set the context class loader of the current thread to the in-memory class loader when the `java` launcher is invoked in source mode. Having the source launcher set the TCCL to the in-memory classloader is benefical for scenarious depending on the TCCL being set to the application-loading loader.
>>
>> For example, the single-argument taking [`ServiceLoader.load(Class)`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/ServiceLoader.html#load(java.lang.Class)) method creates "a new service loader for the given service type, using the current thread's [context class loader](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#getContextClassLoader())."
>
> src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java line 204:
>
>> 202: ClassLoader loader = context.newClassLoaderFor(parentLoader, firstClassName);
>> 203: firstClass = Class.forName(firstClassName, false, loader);
>> 204: Thread.currentThread().setContextClassLoader(loader);
>
> Are you sure this it the right place? It might be, but seeing it in a try-catch is surprising. It should be set before the initializer for the main class runs, which might be here but checking.
Would the constructor of `MemoryClassLoader` be a better place?
Because `context.newClassLoaderFor()` has two exit-points where an instance of `MemoryClassLoader` is created.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/20097#discussion_r1678950513
More information about the compiler-dev
mailing list