From ad498f57fcead174306c6e6e3b2d1f9916821b84 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Sun, 21 Jul 2024 08:49:16 +0000 Subject: [PATCH] 8335896: Source launcher should set TCCL Reviewed-by: alanb --- .../tools/javac/launcher/MemoryContext.java | 3 +-- .../tools/javac/launcher/SourceLauncher.java | 3 ++- .../javac/launcher/SourceLauncherTest.java | 23 ++++++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java index f804174c711fd..941f5c4c40ec4 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java @@ -184,10 +184,9 @@ byte[] compileJavaFileByName(String name) { * @param parent the class loader to be used as the parent loader * @param mainClassName the fully-qualified name of the application class to load * @return class loader object able to find and load the desired class - * @throws ClassNotFoundException if the class cannot be located * @throws Fault if a modular application class is in the unnamed package */ - ClassLoader newClassLoaderFor(ClassLoader parent, String mainClassName) throws ClassNotFoundException, Fault { + ClassLoader newClassLoaderFor(ClassLoader parent, String mainClassName) throws Fault { var moduleInfoBytes = inMemoryClasses.get("module-info"); if (moduleInfoBytes == null) { // Trivial case: no compiled module descriptor available, no extra module layer required diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java index 4ae6b841542a3..8a8226b3dba1b 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java @@ -198,8 +198,9 @@ private Class execute(MemoryContext context, String[] mainArgs) // 1. Find a main method in the first class and if there is one - invoke it Class firstClass; String firstClassName = program.qualifiedTypeNames().getFirst(); + ClassLoader loader = context.newClassLoaderFor(parentLoader, firstClassName); + Thread.currentThread().setContextClassLoader(loader); try { - ClassLoader loader = context.newClassLoaderFor(parentLoader, firstClassName); firstClass = Class.forName(firstClassName, false, loader); } catch (ClassNotFoundException e) { throw new Fault(Errors.CantFindClass(firstClassName)); diff --git a/test/langtools/tools/javac/launcher/SourceLauncherTest.java b/test/langtools/tools/javac/launcher/SourceLauncherTest.java index 5e70e84482d32..e3ed1c3e8af51 100644 --- a/test/langtools/tools/javac/launcher/SourceLauncherTest.java +++ b/test/langtools/tools/javac/launcher/SourceLauncherTest.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8192920 8204588 8246774 8248843 8268869 8235876 8328339 + * @bug 8192920 8204588 8246774 8248843 8268869 8235876 8328339 8335896 * @summary Test source launcher * @library /tools/lib * @enablePreview @@ -276,6 +276,27 @@ public void testSystemProperty(Path base) throws IOException { checkEqual("stdout", log.trim(), file.toAbsolutePath().toString()); } + @Test + public void testThreadContextClassLoader(Path base) throws IOException { + tb.writeJavaFiles(base, //language=java + """ + class ThreadContextClassLoader { + public static void main(String... args) { + var expected = ThreadContextClassLoader.class.getClassLoader(); + var actual = Thread.currentThread().getContextClassLoader(); + System.out.println(expected == actual); + } + } + """); + + Path file = base.resolve("ThreadContextClassLoader.java"); + String log = new JavaTask(tb) + .className(file.toString()) + .run(Task.Expect.SUCCESS) + .getOutput(Task.OutputKind.STDOUT); + checkEqual("stdout", log.trim(), "true"); + } + void testSuccess(Path file, String expect) throws IOException { Result r = run(file, Collections.emptyList(), List.of("1", "2", "3")); checkEqual("stdout", r.stdOut, expect);