843 /** |
843 /** |
844 * Creates a DOMException. On J2SE 1.4 and above the cause for the exception will be set. |
844 * Creates a DOMException. On J2SE 1.4 and above the cause for the exception will be set. |
845 */ |
845 */ |
846 public static DOMException createDOMException(short code, Throwable cause) { |
846 public static DOMException createDOMException(short code, Throwable cause) { |
847 DOMException de = new DOMException(code, cause != null ? cause.getMessage() : null); |
847 DOMException de = new DOMException(code, cause != null ? cause.getMessage() : null); |
848 if (cause != null && ThrowableMethods.fgThrowableMethodsAvailable) { |
848 if (cause != null) de.initCause(cause); |
849 try { |
|
850 ThrowableMethods.fgThrowableInitCauseMethod.invoke(de, new Object [] {cause}); |
|
851 } |
|
852 // Something went wrong. There's not much we can do about it. |
|
853 catch (Exception e) {} |
|
854 } |
|
855 return de; |
849 return de; |
856 } |
850 } |
857 |
851 |
858 /** |
852 /** |
859 * Creates an LSException. On J2SE 1.4 and above the cause for the exception will be set. |
853 * Creates an LSException. On J2SE 1.4 and above the cause for the exception will be set. |
860 */ |
854 */ |
861 public static LSException createLSException(short code, Throwable cause) { |
855 public static LSException createLSException(short code, Throwable cause) { |
862 LSException lse = new LSException(code, cause != null ? cause.getMessage() : null); |
856 LSException lse = new LSException(code, cause != null ? cause.getMessage() : null); |
863 if (cause != null && ThrowableMethods.fgThrowableMethodsAvailable) { |
857 if (cause != null) lse.initCause(cause); |
864 try { |
|
865 ThrowableMethods.fgThrowableInitCauseMethod.invoke(lse, new Object [] {cause}); |
|
866 } |
|
867 // Something went wrong. There's not much we can do about it. |
|
868 catch (Exception e) {} |
|
869 } |
|
870 return lse; |
858 return lse; |
871 } |
859 } |
872 |
860 |
873 /** |
|
874 * Holder of methods from java.lang.Throwable. |
|
875 */ |
|
876 static class ThrowableMethods { |
|
877 |
|
878 // Method: java.lang.Throwable.initCause(java.lang.Throwable) |
|
879 private static java.lang.reflect.Method fgThrowableInitCauseMethod = null; |
|
880 |
|
881 // Flag indicating whether or not Throwable methods available. |
|
882 private static boolean fgThrowableMethodsAvailable = false; |
|
883 |
|
884 private ThrowableMethods() {} |
|
885 |
|
886 // Attempt to get methods for java.lang.Throwable on class initialization. |
|
887 static { |
|
888 try { |
|
889 fgThrowableInitCauseMethod = Throwable.class.getMethod("initCause", new Class [] {Throwable.class}); |
|
890 fgThrowableMethodsAvailable = true; |
|
891 } |
|
892 // ClassNotFoundException, NoSuchMethodException or SecurityException |
|
893 // Whatever the case, we cannot use java.lang.Throwable.initCause(java.lang.Throwable). |
|
894 catch (Exception exc) { |
|
895 fgThrowableInitCauseMethod = null; |
|
896 fgThrowableMethodsAvailable = false; |
|
897 } |
|
898 } |
|
899 } |
|
900 |
|
901 } // class DOMUtil |
861 } // class DOMUtil |