Skip to content

Commit

Permalink
8335896: Source launcher should set TCCL
Browse files Browse the repository at this point in the history
Reviewed-by: alanb
  • Loading branch information
sormuras committed Jul 21, 2024
1 parent b21cb44 commit ad498f5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
23 changes: 22 additions & 1 deletion test/langtools/tools/javac/launcher/SourceLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ad498f5

Please sign in to comment.