1 16 package org.quartz.utils; 17 18 import java.lang.reflect.Method ; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 40 public class ExceptionHelper { 41 private static Boolean supportsNestedThrowable = null; 42 43 private ExceptionHelper() { 44 } 45 46 50 public static Throwable setCause(Throwable exception, Throwable cause) { 51 if (exception != null) { 52 if (supportsNestedThrowable()) { 53 try { 54 Method initCauseMethod = 55 exception.getClass().getMethod("initCause", new Class [] {Throwable .class}); 56 initCauseMethod.invoke(exception, new Object [] {cause}); 57 } catch (Exception e) { 58 getLog().warn( 59 "Unable to invoke initCause() method on class: " + 60 exception.getClass().getName(), e); 61 } 62 } 63 } 64 return exception; 65 } 66 67 71 public static Throwable getCause(Throwable exception) { 72 if (supportsNestedThrowable()) { 73 try { 74 Method getCauseMethod = 75 exception.getClass().getMethod("getCause", (Class [])null); 76 return (Throwable )getCauseMethod.invoke(exception, (Object [])null); 77 } catch (Exception e) { 78 getLog().warn( 79 "Unable to invoke getCause() method on class: " + 80 exception.getClass().getName(), e); 81 } 82 } 83 84 return null; 85 } 86 87 91 public static synchronized boolean supportsNestedThrowable() { 92 if (supportsNestedThrowable == null) { 93 try { 94 Throwable .class.getMethod("initCause", new Class [] {Throwable .class}); 95 Throwable .class.getMethod("getCause", (Class [])null); 96 supportsNestedThrowable = Boolean.TRUE; 97 getLog().debug("Detected JDK support for nested exceptions."); 98 } catch (NoSuchMethodException e) { 99 supportsNestedThrowable = Boolean.FALSE; 100 getLog().debug("Nested exceptions are not supported by this JDK."); 101 } 102 } 103 104 return supportsNestedThrowable.booleanValue(); 105 } 106 107 private static Log getLog() { 108 return LogFactory.getLog(ExceptionHelper.class); 109 } 110 } 111 | Popular Tags |