坑爹的Sun JDK

Zollty發表於2013-10-30

 

Sun的這個java.lang.Throwable 原始碼 設計非常糟糕,完全沒有擴充套件性,

 

我在IBM 的Java JDK下,繼承java.lang.Throwable重新定義了一個ExceptionWrapper,過載了java.lang.Throwable的大部分方法,

目的就是,不用Java本身的那種堆疊資訊,大家知道的,Java本身的堆疊資訊冗餘性非常大,點選一個Action報錯就上百行,當時真正有價值的錯誤資訊就幾行。

 

IBM的JDK是可以重寫java.lang.Throwable的方法的,但是Sun的JDK坑爹的是private的方法,重寫不了!這種設計真是坑爹。

不只是我遇到這個問題,網上也有人再說,問題的詳細描述如下:

Hello java gods!  I've no idea, whether this is the right forum to post my request to, but I hope, some sun java developers will read it and may be able to do the following change:  class Throwable, method void printStackTraceAsCause(PrintStream s, StackTraceElement[] causedTrace)  and  class Throwable, method void printStackTraceAsCause(PrintWriter s, StackTraceElement[] causedTrace)  are both declared private. This should be protected, because we are developing a client server environment where exceptions are transferred over the network and for several reasons, I use a special own StackTraceElement. This is necessary, because the original StackTraceElements are created "magically" by the VM and cannot be constructed by normal code. Additionally, I need more information and have added fields to my own RemoteStackTraceElement.  Well, if the above methods were protected instead of private, it would always execute my methods instead of the original ones, if one of my Exceptions is in the list of causes. This means, I could encapsulate my exception the following way, which is sometimes needed, e.g. in Listeners:  void methodGivenByInterface() throws ExceptionGivenByInterface {   try {     client.execCmd(...);   } catch (RemoteException x) {     throw new ExceptionGivenByInterface(x);   } }  If the method that executes myMethod does a printStackTrace, it executes the one of ExceptionGivenByInterface. Because my RemoteException is the cause, the printStackTraceAsCause of it will be executed. But because it is declared private in Throwable, it does not execute my own printStackTraceAsCause but the one of Throwable.  I hope, I could make clear, why it is necessary to change private to protected, and I hope, someone finds the time to change this one word at both methods   Thousand thanks in advance for getting a protected Throwable.printStackTraceAsCause in j2sdk1.5x!  Best regards, Marco

 

 

 

相關文章