Is there a way to add additional information to a java stacktrace?
I am developing an interpreter for a script language and would like to see the corresponding lines of script code in the java stacktrace.
The output could look something like this:
java开发者_JAVA技巧.lang.NullPointerException
at package.IPF_Try.execute(IPF_Try.java:76) called in script.scr:155
at package.IPF_Block.execute(IPF_Block.java:304)
at package.IPF_If.execute(IPF_If.java:105) called in script.scr:130
at package.IPF_Block.execute(IPF_Block.java:304)
at package.IPF_Main.execute(IPF_Main.java:147)
...
or this:
java.lang.NullPointerException
at package.IPF_Try.execute(IPF_Try.java:76)
--- called in script.scr:155 ---
at package.IPF_Block.execute(IPF_Block.java:304)
at package.IPF_If.execute(IPF_If.java:105)
--- called in script.scr:130---
at package.IPF_Block.execute(IPF_Block.java:304)
at package.IPF_Main.execute(IPF_Main.java:147)
...
This would make debugging a lot easier, unfortunately google could not find anything to achieve this.
The only way I could think of was to dynamlically generate a lot of classes with methods, whose name contains the information I need and which simply call the next method in the stacktrace - but that seems like a waste of (permgen) memory and cpu cycles to me.
If you translate your scripts to bytecode, you can provide debugging details using the SourceFile
and LineNumber
attributes.
I am not aware of a mechanism to inject information into the call-stack at runtime.
精彩评论