TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: Decorate Java stack traces with source code snippets

110 点作者 laech超过 1 年前

11 条评论

sakopov超过 1 年前
This is pretty awesome! This got me curious if I can do this in C# so I took a quick stab at it. It&#x27;s not actually updating the original stack trace but it&#x27;s kind of cool. Might be worth exploring further :)<p><pre><code> static void Main() { try { throw new DivideByZeroException(&quot;Boom!&quot;); } catch (Exception ex) { var trace = new StackTrace(ex, true); var frame = trace.GetFrame(0); var targetFilename = frame.GetFileName(); var targetLineNumber = frame.GetFileLineNumber(); Console.WriteLine($&quot;Exception occurred at {targetFilename}, line {targetLineNumber}&quot;); if (File.Exists(targetFilename)) { var source = File.ReadAllLines(targetFilename); var startIndex = Math.Max(0, targetLineNumber - 3); var endIndex = Math.Min(source.Length - 1, targetLineNumber + 1); for (var i = startIndex; i &lt;= endIndex; i++) { var lineNumber = i + 1; Console.WriteLine($&quot;{(lineNumber == targetLineNumber ? $&quot;&gt;&gt; {lineNumber}&quot; : $&quot; {lineNumber}&quot;)}{source[i]}&quot;); } } } }</code></pre>
junon超过 1 年前
I did this a while back for Python:<p><a href="https:&#x2F;&#x2F;GitHub.com&#x2F;qix-&#x2F;better-exceptions">https:&#x2F;&#x2F;GitHub.com&#x2F;qix-&#x2F;better-exceptions</a>
评论 #37790705 未加载
评论 #37799348 未加载
zmmmmm超过 1 年前
Reminds me of power assertions in Groovy - one of my favorite features!
评论 #37788328 未加载
nunobrito超过 1 年前
Thanks for publishing. From the title was expecting that we&#x27;d get the code snippet related to the code itself, rather than the junit where the code failed.<p>But I guess for such feature to be possible you&#x27;d have to pack the source code inside the jar itself.
评论 #37788097 未加载
gunnarmorling超过 1 年前
Loving it! Building a Maven Surefire plug-in to do this was on my list of potential side projects for quite a while, so cool that someone finally did this. Even better as a JUnit extension. Congrats on the release, can&#x27;t wait to give it a try!.
theanonymousone超过 1 年前
One big &quot;off-label&quot; use of stack traces for me is to determine the caller method of the current method. I use it in a lightly customised log function to also log the name of the method that has emitted the log.
评论 #37789677 未加载
评论 #37789189 未加载
CrimsonChapulin超过 1 年前
FYI: for the gradle dependencies section on the read me, both junit4 and junit5 say junit5.
评论 #37795101 未加载
rickette超过 1 年前
This look pretty useful, thanks.
treebeard5440超过 1 年前
Great work - I like this a lot
surajcm超过 1 年前
Awesome !! Good to see this
chii超过 1 年前
Very cool work!<p>Some unsolicited opinion: One potential code pet peeve i always have is that the overloads being used in java serves no purpose, but makes the code super confusing to read without using an IDE.<p>For example, <a href="https:&#x2F;&#x2F;github.com&#x2F;laech&#x2F;java-stacksrc&#x2F;blob&#x2F;main&#x2F;core&#x2F;src&#x2F;main&#x2F;java&#x2F;nz&#x2F;lae&#x2F;stacksrc&#x2F;StackTraceDecorator.java#L82">https:&#x2F;&#x2F;github.com&#x2F;laech&#x2F;java-stacksrc&#x2F;blob&#x2F;main&#x2F;core&#x2F;src&#x2F;ma...</a> , which is code to decorate the stacktrace element, calls the function `decorate`. However, there&#x27;s several overloads, all named decorate! It is certainly a better naming convention to call it `decorateElement` imho.