001: package org.andromda.core.common;
002:
003: import org.apache.commons.lang.StringUtils;
004:
005: /**
006: * Contains Exception handling utilities.
007: *
008: * @author Chad Brandon
009: */
010: public class ExceptionUtils {
011: /**
012: * Checks if the argument is null, and if so, throws an IllegalArgumentException, does nothing if not.
013: *
014: * @param methodExecuteName the name of the method we are currently executing
015: * @param argumentName the name of the argument we are checking for null
016: * @param argument the argument we are checking
017: * @deprecated used {@link #checkNull(String, Object)} instead since we can detect the method name.
018: */
019: public static void checkNull(final String methodExecuteName,
020: final String argumentName, final Object argument) {
021: checkNull(argumentName, argument, 3);
022: }
023:
024: /**
025: * Checks if the argument is null, and if so, throws an IllegalArgumentException, does nothing if not.
026: *
027: * @param argumentName the name of the argument we are checking for null
028: * @param argument the argument we are checking
029: */
030: public static void checkNull(final String argumentName,
031: final Object argument) {
032: checkNull(argumentName, argument, 3);
033: }
034:
035: /**
036: * Checks if the argument is null, and if so, throws an IllegalArgumentException, does nothing if not.
037: *
038: * @param argumentName the name of the argument we are checking for null
039: * @param argument the argument we are checking
040: * @param stackDepth the depth of the stack from which to retrieve the methodInformation.
041: */
042: private static void checkNull(final String argumentName,
043: final Object argument, final int stackDepth) {
044: if (StringUtils.isEmpty(argumentName)) {
045: throw new IllegalArgumentException(
046: "'argumentName' can not be null or an empty String");
047: }
048:
049: if (argument == null) {
050: throw new IllegalArgumentException(
051: getMethodName(stackDepth) + " - '" + argumentName
052: + "' can not be null");
053: }
054: }
055:
056: /**
057: * Checks if the argument is null or an empty String throws an IllegalArgumentException if it is, does nothing if
058: * not.
059: *
060: * @param methodExecuteName the name of the method we are currently executing
061: * @param argumentName the name of the argument we are checking for null
062: * @param argument the argument we are checking
063: * @deprecated use {@link #checkEmpty(String, String)} instead since we can detect the method name.
064: */
065: public static void checkEmpty(final String methodExecuteName,
066: final String argumentName, final String argument) {
067: checkEmpty(argumentName, argument, 3);
068: }
069:
070: /**
071: * Checks if the argument is null or an empty String throws an IllegalArgumentException if it is, does nothing if
072: * not.
073: *
074: * @param argumentName the name of the argument we are checking for null
075: * @param argument the argument we are checking
076: */
077: public static void checkEmpty(final String argumentName,
078: final String argument) {
079: checkEmpty(argumentName, argument, 3);
080: }
081:
082: /**
083: * Checks if the argument is null or an empty String throws an IllegalArgumentException if it is, does nothing if
084: * not.
085: *
086: * @param argumentName the name of the argument we are checking for null
087: * @param argument the argument we are checking
088: * @param stackDepth the depth of the stack from which to retrieve the methodInformation.
089: */
090: private static void checkEmpty(final String argumentName,
091: final String argument, final int stackDepth) {
092: if (StringUtils.isEmpty(argumentName)) {
093: throw new IllegalArgumentException(
094: "'argumentName' can not be null or an empty String");
095: }
096: if (StringUtils.isEmpty(argument)) {
097: throw new IllegalArgumentException(
098: getMethodName(stackDepth) + " - '" + argumentName
099: + "' can not be null or an empty String");
100: }
101: }
102:
103: /**
104: * Checks if the argumentClass is assignable to assignableToClass, and if not throws an IllegalArgumentException,
105: * otherwise does nothing.
106: *
107: * @param methodExecuteName the method name of the method, this method is being executed within
108: * @param assignableToClass the Class that argumentClass must be assignable to
109: * @param argumentClass the argumentClass we are checking
110: * @param argumentName the name of the argument we are checking
111: * @deprecated use {@link #checkAssignable(Class, String, Class)} since we can detect the method name.
112: */
113: public static void checkAssignable(final String methodExecuteName,
114: final Class assignableToClass, final String argumentName,
115: final Class argumentClass) {
116: checkAssignable(assignableToClass, argumentName, argumentClass,
117: 3);
118: }
119:
120: /**
121: * Checks if the argumentClass is assignable to assignableToClass, and if not throws an IllegalArgumentException,
122: * otherwise does nothing.
123: *
124: * @param assignableToClass the Class that argumentClass must be assignable to
125: * @param argumentClass the argumentClass we are checking
126: * @param argumentName the name of the argument we are checking
127: */
128: public static void checkAssignable(final Class assignableToClass,
129: final String argumentName, final Class argumentClass) {
130: checkAssignable(assignableToClass, argumentName, argumentClass,
131: 3);
132: }
133:
134: /**
135: * Checks if the argumentClass is assignable to assignableToClass, and if not throws an IllegalArgumentException,
136: * otherwise does nothing.
137: *
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: * @param stackDepth the depth of the stack from which to retrieve the method information.
142: */
143: private static void checkAssignable(final Class assignableToClass,
144: final String argumentName, final Class argumentClass,
145: final int stackDepth) {
146: if (assignableToClass == null) {
147: throw new IllegalArgumentException(
148: "'assignableToClass' can not be null");
149: }
150: if (argumentClass == null) {
151: throw new IllegalArgumentException(
152: "'argumentClass' can not be null");
153: }
154: if (StringUtils.isEmpty(argumentName)) {
155: throw new IllegalArgumentException(
156: "'argumentName can not be null or an empty String");
157: }
158:
159: // this is what the method is for
160: if (!assignableToClass.isAssignableFrom(argumentClass)) {
161: throw new IllegalArgumentException(
162: getMethodName(stackDepth) + " - '" + argumentName
163: + "' class --> '" + argumentClass
164: + "' must be assignable to class --> '"
165: + assignableToClass + "'");
166: }
167: }
168:
169: /**
170: * Attempts to retrieve the root cause of the exception, if it can not be
171: * found, the <code>throwable</code> itself is returned.
172: *
173: * @param throwable the exception from which to retrieve the root cause.
174: * @return the root cause of the exception
175: */
176: public static Throwable getRootCause(Throwable throwable) {
177: final Throwable root = org.apache.commons.lang.exception.ExceptionUtils
178: .getRootCause(throwable);
179: if (root != null) {
180: throwable = root;
181: }
182: return throwable;
183: }
184:
185: /**
186: * Gets the appropriate method name for the method being checked.
187: *
188: * @return the name of the method.
189: */
190: private static String getMethodName(int stackDepth) {
191: String methodName = null;
192: final Throwable throwable = new Throwable();
193: final StackTraceElement[] stack = throwable.getStackTrace();
194: if (stack.length >= stackDepth) {
195: final StackTraceElement element = stack[stackDepth];
196: methodName = element.getClassName() + '.'
197: + element.getMethodName();
198: }
199: return methodName;
200: }
201: }
|