1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.clirr.core.internal;
21
22 import java.lang.reflect.Method;
23
24 /***
25 * A helper class to initialize the cause of an exception.
26 *
27 * This allows Clirr to be compile time compatible with JDK 1.3
28 * at still taking advantage of the JDK 1.4 chained exceptions feature
29 * if available.
30 *
31 * @author lkuehne
32 */
33 public final class ExceptionUtil
34 {
35 /*** Disallow instantiation. */
36 private ExceptionUtil()
37 {
38 }
39
40 private static Method initCauseMethod;
41
42 static
43 {
44 try
45 {
46 initCauseMethod = Throwable.class.getMethod("initCause", new Class[]{Throwable.class});
47 }
48 catch (NoSuchMethodException e)
49 {
50
51 initCauseMethod = null;
52 }
53 }
54
55 /***
56 * Initializes the chained exception if possible.
57 * Does nothing if chained exceptions are not available on the
58 * current JDK (1.3 or lower).
59 *
60 * @param t the resulting exception (high abstraction level)
61 * @param cause the underlying cause of t (low abstraction level)
62 */
63 public static void initCause(Throwable t, Throwable cause)
64 {
65 if (initCauseMethod == null)
66 {
67 return;
68 }
69
70 try
71 {
72 initCauseMethod.invoke(t, new Throwable[]{cause});
73 }
74 catch (Exception e)
75 {
76 if (e instanceof RuntimeException)
77 {
78 throw (RuntimeException) e;
79 }
80 throw new RuntimeException("unable to initCause: " + e.toString());
81 }
82 }
83 }