这很容易:
Throwable有方法
getStackTrace()和
setStackTrace()。
从我的一个项目(非开源,但也许有一天我将打开远程调用引擎):
为了您的方便翻译:
合并堆栈跟踪。下端(在调用层次结构中更深的位置,在数组的末尾/输出)是堆栈中已经存在的内容,高端将从当前堆栈中获取。在它们之间,我们将放置一个
远程呼叫标记 。
private void mergeStackTraces(Throwable error) { StackTraceElement[] currentStack = new Throwable().getStackTrace(); int currentStackLimit = 5; // TODO: raussuchen StackTraceElement[] oldStack = error.getStackTrace(); StackTraceElement[] zusammen = new StackTraceElement[currentStack.length - currentStackLimit + oldStack.length + 1]; System.arraycopy(oldStack, 0, zusammen, 0, oldStack.length); zusammen[oldStack.length] = new StackTraceElement("══════════════════════════", "<remote call %" +callID+ ">", "", -3); System.arraycopy(currentStack, currentStackLimit, zusammen, oldStack.length+1, currentStack.length - currentStackLimit); error.setStackTrace(zusammen); }
(在服务器端,我已经切断了堆栈跟踪中与方法调用本身无关的部分,即与消息处理相关的所有部分。)
这将导致如下所示的组合堆栈跟踪:
java.lang.SecurityException: Das Passwort für Nutzer »Paul« ist falsch. at de.fencing_game.db.userdb.Db4oUserDB.login(Db4oUserDB.java:304) at de.fencing_game.server.impl.StandardServers$SSServer.run(StandardServers.java:316) at de.fencing_game.server.impl.StandardServers$SSServer.run(StandardServers.java:314) at java.security.AccessController.doPrivileged(Native Method) at de.fencing_game.server.impl.StandardServers$SSServer.login(StandardServers.java:313) at de.fencing_game.transport.server.ServerTransport$ConnectionInfo.login(ServerTransport.java:460) at ══════════════════════════.<remote call %2>() at $Proxy1.login(Unknown Source) at de.fencing_game.gui.basics.LoginUtils.login(LoginUtils.java:80) at de.fencing_game.gui.Lobby.connectTo(Lobby.java:302) at de.fencing_game.gui.Lobby.run(Lobby.java:849) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:226) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:647) at java.awt.EventQueue.access══════════════════════════0(EventQueue.java:96) at java.awt.EventQueue.run(EventQueue.java:608) at java.awt.EventQueue.run(EventQueue.java:606) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext.doIntersectionPrivilege(AccessControlContext.java:105) at java.awt.EventQueue.dispatchEvent(EventQueue.java:617) at java.awt.EventDispatchThread.pumponeEventForFilters(EventDispatchThread.java:275) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177) at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
我想RMI系统所做的事情非常相似(只是没有编辑:)。
对于您的用例,您必须在启动内部线程时保存外部线程的堆栈跟踪,然后在run方法中捕获异常并将外部堆栈跟踪追加到内部异常的堆栈跟踪。不过,我确实建议您放置某种类型的分隔符。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)