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
14 * IllegalArgumentException, does nothing if not.
15 *
16 * @param methodExecuteName the name of the method we are currently
17 * executing
18 * @param argumentName the name of the argument we are checking for null
19 * @param argument the argument we are checking
20 */
21 public static void checkNull(
22 String methodExecuteName,
23 String argumentName,
24 Object argument)
25 {
26 final String methodName = "ExceptionUtils.checkNull";
27 if (StringUtils.isEmpty(methodExecuteName))
28 {
29 throw new IllegalArgumentException(methodName
30 + " - methodExecuteName can not be null or an empty String");
31 }
32 if (StringUtils.isEmpty(argumentName))
33 {
34 throw new IllegalArgumentException("methodName: '" + methodName
35 + "' - argumentName can not be null or an empty String");
36 }
37
38
39 if (argument == null)
40 {
41 throw new IllegalArgumentException("methodName: "
42 + methodExecuteName + " - '" + argumentName
43 + "' can not be null");
44 }
45 }
46
47 /***
48 * Checks if the argument is null or an empty String throws an
49 * IllegalArgumentException if it is, does nothing if not.
50 *
51 * @param methodExecuteName the name of the method we are currently
52 * executing
53 * @param argumentName the name of the argument we are checking for null
54 * @param argument the argument we are checking
55 */
56 public static void checkEmpty(
57 String methodExecuteName,
58 String argumentName,
59 String argument)
60 {
61 final String methodName = "ExceptionUtils.checkEmpty";
62 if (StringUtils.isEmpty(methodExecuteName))
63 {
64 throw new IllegalArgumentException(methodName
65 + " - methodExecuteName can not be null or an empty String");
66 }
67 if (StringUtils.isEmpty(argumentName))
68 {
69 throw new IllegalArgumentException(methodName
70 + " - argumentName can not be null or an empty String");
71 }
72
73
74 if (StringUtils.isEmpty(argument))
75 {
76 throw new IllegalArgumentException("methodName: "
77 + methodExecuteName + " - '" + argumentName
78 + "' can not be null or an empty String");
79 }
80 }
81
82 /***
83 * Checks if the argumentClass is assignable to assignableToClass, and if
84 * not throws an IllegalArgumentException, otherwise does nothing.
85 *
86 * @param methodExecuteName the method name of the method, this method is
87 * being executed within
88 * @param assignableToClass the Class that argumentClass must be assignable
89 * to
90 * @param argumentClass the argumentClass we are checking
91 * @param argumentName the name of the argument we are checking
92 */
93 public static void checkAssignable(
94 String methodExecuteName,
95 Class assignableToClass,
96 String argumentName,
97 Class argumentClass)
98 {
99 final String methodName = "ExceptionUtils.checkAssignable";
100 if (StringUtils.isEmpty(methodExecuteName))
101 {
102 throw new IllegalArgumentException(methodName
103 + " - methodExecuteName can not be null or an empty String");
104 }
105 if (assignableToClass == null)
106 {
107 throw new IllegalArgumentException(methodName
108 + " - assignableToClass can not be null");
109 }
110 if (argumentClass == null)
111 {
112 throw new IllegalArgumentException(methodName
113 + " - argumentClass can not be null");
114 }
115 if (StringUtils.isEmpty(argumentName))
116 {
117 throw new IllegalArgumentException(methodName
118 + " - argumentName can not be null or an empty String");
119 }
120
121
122 if (!assignableToClass.isAssignableFrom(argumentClass))
123 {
124 throw new IllegalArgumentException("methodName: "
125 + methodExecuteName + " - '" + argumentName + "' class --> '"
126 + argumentClass + "' must be assignable to class --> '"
127 + assignableToClass + "'");
128 }
129 }
130 }