|
4 | 4 |
|
5 | 5 | import java.util.ArrayList;
|
6 | 6 | import java.util.Arrays;
|
| 7 | +import java.util.concurrent.atomic.AtomicInteger; |
7 | 8 |
|
8 | 9 | /**
|
9 | 10 | * The ExceptionFormatter class provides utility methods for formatting exceptions.
|
@@ -38,44 +39,52 @@ public static Throwable findInnermostCause(Throwable t) {
|
38 | 39 | }
|
39 | 40 |
|
40 | 41 | /**
|
41 |
| - * Formats the stack trace of the given Throwable into a structured list. |
42 |
| - * This method emulates the Perl method "Carp::longmess". |
43 |
| - * It only includes stack trace elements that contain the method name "apply" and do not |
44 |
| - * originate from Java source files. |
| 42 | + * Formats the stack trace of a Throwable, replacing specific entries with artificial caller stack entries. |
45 | 43 | *
|
46 | 44 | * @param t The Throwable whose stack trace is to be formatted.
|
47 |
| - * @return A list of lists, where each inner list contains the package name, source file name, |
48 |
| - * and line number of a relevant stack trace element. |
| 45 | + * @return A list of lists, where each inner list represents a stack trace element with package name, source file, and line number. |
49 | 46 | */
|
50 | 47 | private static ArrayList<ArrayList<String>> formatThrowable(Throwable t) {
|
51 | 48 | ArrayList<ArrayList<String>> stackTrace = new ArrayList<>();
|
52 |
| - |
53 |
| - // System.out.println("innermostCause: "); innermostCause.printStackTrace(); |
54 |
| - |
55 |
| - // Filter and append the stack trace |
56 |
| - |
57 |
| - // Filter compiled Perl methods like: |
58 |
| - // org.perlonjava.anon1.apply(misc/snippets/CallerTest.pm @ CallerTest:36) |
59 |
| - // Filter `org.perlonjava.perlmodule` Perl-like Java methods like: |
60 |
| - // org.perlonjava.perlmodule.Exporter.exportOkTags(Exporter.java:159) |
| 49 | + AtomicInteger callerStackIndex = new AtomicInteger(); // Initialize the index for CallerStack |
61 | 50 |
|
62 | 51 | Arrays.stream(t.getStackTrace())
|
63 |
| - .filter(element -> |
64 |
| - element.getClassName().contains("org.perlonjava.anon") |
65 |
| - || element.getClassName().contains("org.perlonjava.perlmodule") |
66 |
| - ) |
67 | 52 | .forEach(element -> {
|
68 |
| - ByteCodeSourceMapper.SourceLocation loc = ByteCodeSourceMapper.parseStackTraceElement(element); |
69 |
| - stackTrace.add( |
70 |
| - new ArrayList<>( |
71 |
| - Arrays.asList( |
72 |
| - loc.packageName(), |
73 |
| - loc.sourceFileName(), |
74 |
| - String.valueOf(loc.lineNumber()) |
| 53 | + if (element.getClassName().equals("org.perlonjava.parser.StatementParser") && |
| 54 | + element.getMethodName().equals("parseUseDeclaration")) { |
| 55 | + // Artificial caller stack entry created at `use` statement |
| 56 | + CallerStack.CallerInfo callerInfo = CallerStack.peek(callerStackIndex.get()); |
| 57 | + if (callerInfo != null) { |
| 58 | + stackTrace.add( |
| 59 | + new ArrayList<>( |
| 60 | + Arrays.asList( |
| 61 | + callerInfo.packageName(), |
| 62 | + callerInfo.filename(), |
| 63 | + String.valueOf(callerInfo.line()) |
| 64 | + ) |
75 | 65 | )
|
76 |
| - ) |
77 |
| - ); |
| 66 | + ); |
| 67 | + callerStackIndex.getAndIncrement(); // Increment the index for the next potential match |
| 68 | + } |
| 69 | + } else if (element.getClassName().contains("org.perlonjava.anon") || |
| 70 | + element.getClassName().contains("org.perlonjava.perlmodule")) { |
| 71 | + // - Compiled Perl methods like: |
| 72 | + // org.perlonjava.anon1.apply(misc/snippets/CallerTest.pm @ CallerTest:36) |
| 73 | + // - Perl-like Java methods like: |
| 74 | + // org.perlonjava.perlmodule.Exporter.exportOkTags(Exporter.java:159) |
| 75 | + ByteCodeSourceMapper.SourceLocation loc = ByteCodeSourceMapper.parseStackTraceElement(element); |
| 76 | + stackTrace.add( |
| 77 | + new ArrayList<>( |
| 78 | + Arrays.asList( |
| 79 | + loc.packageName(), |
| 80 | + loc.sourceFileName(), |
| 81 | + String.valueOf(loc.lineNumber()) |
| 82 | + ) |
| 83 | + ) |
| 84 | + ); |
| 85 | + } |
78 | 86 | });
|
| 87 | + |
79 | 88 | return stackTrace;
|
80 | 89 | }
|
81 | 90 | }
|
0 commit comments