1 package org.andromda.core.common;
2
3 import org.apache.commons.lang.StringUtils;
4
5
6
7
8
9
10 public class ExceptionUtils
11 {
12
13
14
15
16
17
18
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
34
35
36
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
50
51
52
53
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
73
74
75
76
77
78
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
94
95
96
97
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
111
112
113
114
115
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
135
136
137
138
139
140
141
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
159
160
161
162
163
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
179
180
181
182
183
184
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
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
215
216
217
218
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
232
233
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 }