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