8362237: IllegalArgumentException in the launcher when exception without stack trace is thrown

Reviewed-by: kcr, vromero
This commit is contained in:
Christian Stein 2025-08-01 11:01:56 +00:00
parent 6c5804722b
commit 8ac4a88f3c
2 changed files with 49 additions and 6 deletions

View File

@ -259,13 +259,19 @@ public final class SourceLauncher {
}
} catch (IllegalAccessException e) {
throw new Fault(Errors.CantAccessMainMethod(mainClassName));
} catch (InvocationTargetException e) {
} catch (InvocationTargetException exception) {
// remove stack frames for source launcher
int invocationFrames = e.getStackTrace().length;
Throwable target = e.getCause();
StackTraceElement[] targetTrace = target.getStackTrace();
target.setStackTrace(Arrays.copyOfRange(targetTrace, 0, targetTrace.length - invocationFrames));
throw e;
StackTraceElement[] invocationElements = exception.getStackTrace();
if (invocationElements == null) throw exception;
Throwable cause = exception.getCause();
if (cause == null) throw exception;
StackTraceElement[] causeElements = cause.getStackTrace();
if (causeElements == null) throw exception;
int range = causeElements.length - invocationElements.length;
if (range >= 0) {
cause.setStackTrace(Arrays.copyOfRange(causeElements, 0, range));
}
throw exception;
}
return mainClass;

View File

@ -24,6 +24,7 @@
/*
* @test
* @bug 8192920 8204588 8246774 8248843 8268869 8235876 8328339 8335896 8344706
* 8362237
* @summary Test source launcher
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
@ -714,6 +715,42 @@ public class SourceLauncherTest extends TestRunner {
"at Thrower.main(Thrower.java:4)");
}
/*
* Tests in which main throws an exception without a stacktrace.
*/
@Test
public void testTargetException2(Path base) throws IOException {
tb.writeJavaFiles(base, """
public class TestLauncher {
public static TestLauncher testCheckcast(Object arg) {
return (TestLauncher)arg;
}
public static void main(String[] args) {
// Warmup to trigger C2 compilation
TestLauncher t = new TestLauncher();
for (int i = 0; i < 10_000; ++i) {
testCheckcast(t);
try {
testCheckcast(42);
} catch (Exception e) {
// Expected
}
}
// This will throw a ClassCastException without
// a stack trace if OmitStackTraceInFastThrow
// is enabled (default)
testCheckcast(42);
}
}
""");
Path file = base.resolve("TestLauncher.java");
Result r = run(file, Collections.emptyList(), List.of("3"));
checkEmpty("stdout", r.stdOut);
checkEmpty("stderr", r.stdErr);
checkTrace("exception", r.exception, "java.lang.ClassCastException");
}
@Test
public void testNoDuplicateIncubatorWarning(Path base) throws Exception {
Path module = base.resolve("lib");