View Javadoc

1   package org.andromda.core.common;
2   
3   import org.apache.commons.lang.StringUtils;
4   
5   
6   /***
7    * Contains Exception handling utilities.
8    *
9    * @author Chad Brandon
10   */
11  public class ExceptionUtils
12  {
13      /***
14       * Checks if the argument is null, and if so, throws an IllegalArgumentException, does nothing if not.
15       *
16       * @param methodExecuteName the name of the method we are currently executing
17       * @param argumentName the name of the argument we are checking for null
18       * @param argument the argument we are checking
19       * @deprecated used {@link #checkNull(String, Object)} instead since we can detect the method name.
20       */
21      public static void checkNull(
22          final String methodExecuteName,
23          final String argumentName,
24          final Object argument)
25      {
26          checkNull(
27              argumentName,
28              argument,
29              3);
30      }
31  
32      /***
33       * Checks if the argument is null, and if so, throws an IllegalArgumentException, does nothing if not.
34       *
35       * @param argumentName the name of the argument we are checking for null
36       * @param argument the argument we are checking
37       */
38      public static void checkNull(
39          final String argumentName,
40          final Object argument)
41      {
42          checkNull(
43              argumentName,
44              argument,
45              3);
46      }
47  
48      /***
49       * Checks if the argument is null, and if so, throws an IllegalArgumentException, does nothing if not.
50       *
51       * @param argumentName the name of the argument we are checking for null
52       * @param argument the argument we are checking
53       * @param stackDepth the depth of the stack from which to retrieve the methodInformation.
54       */
55      private static void checkNull(
56          final String argumentName,
57          final Object argument,
58          final int stackDepth)
59      {
60          if (StringUtils.isEmpty(argumentName))
61          {
62              throw new IllegalArgumentException("'argumentName' can not be null or an empty String");
63          }
64  
65          if (argument == null)
66          {
67              throw new IllegalArgumentException(getMethodName(stackDepth) + " - '" + argumentName + "' can not be null");
68          }
69      }
70  
71      /***
72       * Checks if the argument is null or an empty String throws an IllegalArgumentException if it is, does nothing if
73       * not.
74       *
75       * @param methodExecuteName the name of the method we are currently executing
76       * @param argumentName the name of the argument we are checking for null
77       * @param argument the argument we are checking
78       * @deprecated use {@link #checkEmpty(String, String)} instead since we can detect the method name.
79       */
80      public static void checkEmpty(
81          final String methodExecuteName,
82          final String argumentName,
83          final String argument)
84      {
85          checkEmpty(
86              argumentName,
87              argument,
88              3);
89      }
90  
91      /***
92       * Checks if the argument is null or an empty String throws an IllegalArgumentException if it is, does nothing if
93       * not.
94       *
95       * @param argumentName the name of the argument we are checking for null
96       * @param argument the argument we are checking
97       */
98      public static void checkEmpty(
99          final String argumentName,
100         final String argument)
101     {
102         checkEmpty(
103             argumentName,
104             argument,
105             3);
106     }
107 
108     /***
109      * Checks if the argument is null or an empty String throws an IllegalArgumentException if it is, does nothing if
110      * not.
111      *
112      * @param argumentName the name of the argument we are checking for null
113      * @param argument the argument we are checking
114      * @param stackDepth the depth of the stack from which to retrieve the methodInformation.
115      */
116     private static void checkEmpty(
117         final String argumentName,
118         final String argument,
119         final int stackDepth)
120     {
121         if (StringUtils.isEmpty(argumentName))
122         {
123             throw new IllegalArgumentException("'argumentName' can not be null or an empty String");
124         }
125         if (StringUtils.isEmpty(argument))
126         {
127             throw new IllegalArgumentException(getMethodName(stackDepth) + " - '" + argumentName +
128                 "' can not be null or an empty String");
129         }
130     }
131 
132     /***
133      * Checks if the argumentClass is assignable to assignableToClass, and if not throws an IllegalArgumentException,
134      * otherwise does nothing.
135      *
136      * @param methodExecuteName the method name of the method, this method is being executed within
137      * @param assignableToClass the Class that argumentClass must be assignable to
138      * @param argumentClass the argumentClass we are checking
139      * @param argumentName the name of the argument we are checking
140      * @deprecated use {@link #checkAssignable(Class, String, Class)} since we can detect the method name.
141      */
142     public static void checkAssignable(
143         final String methodExecuteName,
144         final Class assignableToClass,
145         final String argumentName,
146         final Class argumentClass)
147     {
148         checkAssignable(
149             assignableToClass,
150             argumentName,
151             argumentClass,
152             3);
153     }
154 
155     /***
156      * Checks if the argumentClass is assignable to assignableToClass, and if not throws an IllegalArgumentException,
157      * otherwise does nothing.
158      *
159      * @param assignableToClass the Class that argumentClass must be assignable to
160      * @param argumentClass the argumentClass we are checking
161      * @param argumentName the name of the argument we are checking
162      */
163     public static void checkAssignable(
164         final Class assignableToClass,
165         final String argumentName,
166         final Class argumentClass)
167     {
168         checkAssignable(
169             assignableToClass,
170             argumentName,
171             argumentClass,
172             3);
173     }
174 
175     /***
176      * Checks if the argumentClass is assignable to assignableToClass, and if not throws an IllegalArgumentException,
177      * otherwise does nothing.
178      *
179      * @param assignableToClass the Class that argumentClass must be assignable to
180      * @param argumentClass the argumentClass we are checking
181      * @param argumentName the name of the argument we are checking
182      * @param stackDepth the depth of the stack from which to retrieve the method information.
183      */
184     private static void checkAssignable(
185         final Class assignableToClass,
186         final String argumentName,
187         final Class argumentClass,
188         final int stackDepth)
189     {
190         if (assignableToClass == null)
191         {
192             throw new IllegalArgumentException("'assignableToClass' can not be null");
193         }
194         if (argumentClass == null)
195         {
196             throw new IllegalArgumentException("'argumentClass' can not be null");
197         }
198         if (StringUtils.isEmpty(argumentName))
199         {
200             throw new IllegalArgumentException("'argumentName can not be null or an empty String");
201         }
202 
203         // this is what the method is for
204         if (!assignableToClass.isAssignableFrom(argumentClass))
205         {
206             throw new IllegalArgumentException(getMethodName(stackDepth) + " - '" + argumentName + "' class --> '" +
207                 argumentClass + "' must be assignable to class --> '" + assignableToClass + "'");
208         }
209     }
210 
211     /***
212      * Attempts to retrieve the root cause of the exception, if it can not be
213      * found, the <code>throwable</code> itself is returned.
214      *
215      * @param throwable the exception from which to retrieve the root cause.
216      * @return the root cause of the exception
217      */
218     public static Throwable getRootCause(Throwable throwable)
219     {
220         final Throwable root = org.apache.commons.lang.exception.ExceptionUtils.getRootCause(throwable);
221         if (root != null)
222         {
223             throwable = root;
224         }
225         return throwable;
226     }
227 
228     /***
229      * Gets the appropriate method name for the method being checked.
230      *
231      * @return the name of the method.
232      */
233     private static String getMethodName(int stackDepth)
234     {
235         String methodName = null;
236         final Throwable throwable = new Throwable();
237         final StackTraceElement[] stack = throwable.getStackTrace();
238         if (stack.length >= stackDepth)
239         {
240             final StackTraceElement element = stack[stackDepth];
241             methodName = element.getClassName() + '.' + element.getMethodName();
242         }
243         return methodName;
244     }
245 }