Skip to content

Commit 8ac4a88

Browse files
committed
8362237: IllegalArgumentException in the launcher when exception without stack trace is thrown
Reviewed-by: kcr, vromero
1 parent 6c58047 commit 8ac4a88

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/SourceLauncher.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,19 @@ private Class<?> execute(MemoryContext context, String[] mainArgs)
259259
}
260260
} catch (IllegalAccessException e) {
261261
throw new Fault(Errors.CantAccessMainMethod(mainClassName));
262-
} catch (InvocationTargetException e) {
262+
} catch (InvocationTargetException exception) {
263263
// remove stack frames for source launcher
264-
int invocationFrames = e.getStackTrace().length;
265-
Throwable target = e.getCause();
266-
StackTraceElement[] targetTrace = target.getStackTrace();
267-
target.setStackTrace(Arrays.copyOfRange(targetTrace, 0, targetTrace.length - invocationFrames));
268-
throw e;
264+
StackTraceElement[] invocationElements = exception.getStackTrace();
265+
if (invocationElements == null) throw exception;
266+
Throwable cause = exception.getCause();
267+
if (cause == null) throw exception;
268+
StackTraceElement[] causeElements = cause.getStackTrace();
269+
if (causeElements == null) throw exception;
270+
int range = causeElements.length - invocationElements.length;
271+
if (range >= 0) {
272+
cause.setStackTrace(Arrays.copyOfRange(causeElements, 0, range));
273+
}
274+
throw exception;
269275
}
270276

271277
return mainClass;

test/langtools/tools/javac/launcher/SourceLauncherTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* @test
2626
* @bug 8192920 8204588 8246774 8248843 8268869 8235876 8328339 8335896 8344706
27+
* 8362237
2728
* @summary Test source launcher
2829
* @library /tools/lib
2930
* @modules jdk.compiler/com.sun.tools.javac.api
@@ -714,6 +715,42 @@ public void testTargetException1(Path base) throws IOException {
714715
"at Thrower.main(Thrower.java:4)");
715716
}
716717

718+
/*
719+
* Tests in which main throws an exception without a stacktrace.
720+
*/
721+
@Test
722+
public void testTargetException2(Path base) throws IOException {
723+
tb.writeJavaFiles(base, """
724+
public class TestLauncher {
725+
public static TestLauncher testCheckcast(Object arg) {
726+
return (TestLauncher)arg;
727+
}
728+
729+
public static void main(String[] args) {
730+
// Warmup to trigger C2 compilation
731+
TestLauncher t = new TestLauncher();
732+
for (int i = 0; i < 10_000; ++i) {
733+
testCheckcast(t);
734+
try {
735+
testCheckcast(42);
736+
} catch (Exception e) {
737+
// Expected
738+
}
739+
}
740+
// This will throw a ClassCastException without
741+
// a stack trace if OmitStackTraceInFastThrow
742+
// is enabled (default)
743+
testCheckcast(42);
744+
}
745+
}
746+
""");
747+
Path file = base.resolve("TestLauncher.java");
748+
Result r = run(file, Collections.emptyList(), List.of("3"));
749+
checkEmpty("stdout", r.stdOut);
750+
checkEmpty("stderr", r.stdErr);
751+
checkTrace("exception", r.exception, "java.lang.ClassCastException");
752+
}
753+
717754
@Test
718755
public void testNoDuplicateIncubatorWarning(Path base) throws Exception {
719756
Path module = base.resolve("lib");

0 commit comments

Comments
 (0)