You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Daniel Rall <dl...@finemaltcoding.com> on 2002/08/15 01:19:04 UTC
[lang][dbcp] ExceptionUtils
AFAIK, this takes care of ExceptionUtils for 1.0. Interested parties,
please assure that this class is sufficient for your needs. Note that
a couple more commits updated the JavaDoc on the constants in this
class.
dlr@apache.org writes:
> dlr 2002/08/14 16:08:28
>
> Modified: lang/src/java/org/apache/commons/lang/exception
> ExceptionUtils.java
> Log:
> CAUSE_METHOD_NAMES: Repurposed CAUSE_METHOD_NAME to be a list of
> method names which may yield a wrapped exception.
>
> getCause(Throwable): Now just wraps getCause(Throwable, String[]),
> passing in CAUSE_METHOD_NAMES as its list of method names.
>
> getCause(Throwable, String[]): Refactored code extracted from
> getCause(Throwable) which looks at a list of method names, plus a
> "detail" field.
>
> getCauseUsingMethodName(): Swapped parameter order for consistency.
>
> getCauseUsingFieldName(): New method which introspects fields instead
> of methods.
>
> Revision Changes Path
> 1.6 +81 -11 jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
>
> Index: ExceptionUtils.java
> ===================================================================
> RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUtils.java,v
> retrieving revision 1.5
> retrieving revision 1.6
> diff -u -u -r1.5 -r1.6
> --- ExceptionUtils.java 14 Aug 2002 21:05:19 -0000 1.5
> +++ ExceptionUtils.java 14 Aug 2002 23:08:28 -0000 1.6
> @@ -54,6 +54,7 @@
> * <http://www.apache.org/>.
> */
>
> +import java.lang.reflect.Field;
> import java.lang.reflect.InvocationTargetException;
> import java.lang.reflect.Method;
> import java.sql.SQLException;
> @@ -69,7 +70,13 @@
> /**
> * The name of the <code>getCause()</code> method.
> */
> - protected static final String CAUSE_METHOD_NAME = "getCause";
> + protected static final String[] CAUSE_METHOD_NAMES =
> + {
> + "getCause",
> + "getNextException",
> + "getTargetException",
> + "getException"
> + };
>
> /**
> * The parameters of the <code>getCause()</code> method.
> @@ -86,25 +93,50 @@
>
> /**
> * Introspects the specified <code>Throwable</code> for a
> - * <code>getCause()</code> method which returns a
> + * <code>getCause()</code>, <code>getNextException()</code>,
> + * <code>getTargetException()</code>, or
> + * <code>getException()</code> method which returns a
> * <code>Throwable</code> object (standard as of JDK 1.4, and part
> * of the {@link
> * org.apache.commons.lang.exception.NestableException} API),
> - * extracting and returning the cause of the exception.
> - * Otherwise, returns <code>null</code>.
> - *
> - * <p>TODO: Examine for a "detail" public member attribute from
> - * java.rmi.RemoteException.
> + * extracting and returning the cause of the exception. In the
> + * absence of any such method, the object is inspected for a
> + * <code>detail</code> field assignable to a
> + * <code>Throwable</code>. If none of the above is found, returns
> + * <code>null</code>.
> *
> * @param t The exception to introspect for a cause.
> * @return The cause of the <code>Throwable</code>.
> */
> public static Throwable getCause(Throwable t)
> {
> + return getCause(t, CAUSE_METHOD_NAMES);
> + }
> +
> + /**
> + * Extends the API of {@link #getCause(Throwable)} by
> + * introspecting for only user-specified method names.
> + *
> + * @see #getCause(Throwable)
> + */
> + public static Throwable getCause(Throwable t, String[] methodNames)
> + {
> Throwable cause = getCauseUsingWellKnownTypes(t);
> if (cause == null)
> {
> - cause = getCauseUsingMethodName(CAUSE_METHOD_NAME, t);
> + for (int i = 0; i < methodNames.length; i++)
> + {
> + cause = getCauseUsingMethodName(t, methodNames[i]);
> + if (cause != null)
> + {
> + break;
> + }
> + }
> +
> + if (cause == null)
> + {
> + cause = getCauseUsingFieldName(t, "detail");
> + }
> }
> return cause;
> }
> @@ -165,13 +197,13 @@
> }
>
> /**
> - * @param methodName The name of the method to find and invoke.
> * @param t The exception to examine.
> + * @param methodName The name of the method to find and invoke.
> * @return The wrapped exception, or <code>null</code> if not
> * found.
> */
> - private static Throwable getCauseUsingMethodName(String methodName,
> - Throwable t)
> + private static Throwable getCauseUsingMethodName(Throwable t,
> + String methodName)
> {
> Method method = null;
> try
> @@ -199,6 +231,44 @@
> {
> }
> catch (InvocationTargetException ignored)
> + {
> + }
> + }
> + return null;
> + }
> +
> + /**
> + * @param t The exception to examine.
> + * @param fieldName The name of the attribute to examine.
> + * @return The wrapped exception, or <code>null</code> if not
> + * found.
> + */
> + private static Throwable getCauseUsingFieldName(Throwable t,
> + String fieldName)
> + {
> + Field field = null;
> + try
> + {
> + field = t.getClass().getField(fieldName);
> + }
> + catch (NoSuchFieldException ignored)
> + {
> + }
> + catch (SecurityException ignored)
> + {
> + }
> +
> + if (field != null &&
> + Throwable.class.isAssignableFrom(field.getType()))
> + {
> + try
> + {
> + return (Throwable) field.get(t);
> + }
> + catch (IllegalAccessException ignored)
> + {
> + }
> + catch (IllegalArgumentException ignored)
> {
> }
> }
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang][dbcp] ExceptionUtils
Posted by Daniel Rall <dl...@finemaltcoding.com>.
This sounds reasonable to me, and I agree to take this on so long as
it sounds reasonable to everyone else as well. I'm going out of town
tonight and will be gone over the weekend, so won't be able to put in
any time on this until next week.
"Stephen Colebourne" <sc...@btopenworld.com> writes:
> I would like to see the ExceptionUtils class match the abilities of
> NestedException. This means more methods ;-)
>
> getThrowableCount(Throwable)
> getThrowables(Throwable)
> indexOfThrowable(Throwable, Class)
> indexOfThrowable(Throwable, Class, int)
> printStackTrace(Throwable, PrintWriter)
>
> In theory this should be refactoring code from NestableDelegate to
> ExceptionUtils.
>
> Stephen
>
> ----- Original Message -----
> From: "Daniel Rall" <dl...@finemaltcoding.com>
> To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
> Sent: Thursday, August 15, 2002 12:19 AM
> Subject: [lang][dbcp] ExceptionUtils
>
>
> > AFAIK, this takes care of ExceptionUtils for 1.0. Interested parties,
> > please assure that this class is sufficient for your needs. Note that
> > a couple more commits updated the JavaDoc on the constants in this
> > class.
> >
> > dlr@apache.org writes:
> >
> > > dlr 2002/08/14 16:08:28
> > >
> > > Modified: lang/src/java/org/apache/commons/lang/exception
> > > ExceptionUtils.java
> > > Log:
> > > CAUSE_METHOD_NAMES: Repurposed CAUSE_METHOD_NAME to be a list of
> > > method names which may yield a wrapped exception.
> > >
> > > getCause(Throwable): Now just wraps getCause(Throwable, String[]),
> > > passing in CAUSE_METHOD_NAMES as its list of method names.
> > >
> > > getCause(Throwable, String[]): Refactored code extracted from
> > > getCause(Throwable) which looks at a list of method names, plus a
> > > "detail" field.
> > >
> > > getCauseUsingMethodName(): Swapped parameter order for consistency.
> > >
> > > getCauseUsingFieldName(): New method which introspects fields instead
> > > of methods.
> > >
> > > Revision Changes Path
> > > 1.6 +81 -11
> jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUti
> ls.java
> > >
> > > Index: ExceptionUtils.java
> > > ===================================================================
> > > RCS file:
> /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/Ex
> ceptionUtils.java,v
> > > retrieving revision 1.5
> > > retrieving revision 1.6
> > > diff -u -u -r1.5 -r1.6
> > > --- ExceptionUtils.java 14 Aug 2002 21:05:19 -0000 1.5
> > > +++ ExceptionUtils.java 14 Aug 2002 23:08:28 -0000 1.6
> > > @@ -54,6 +54,7 @@
> > > * <http://www.apache.org/>.
> > > */
> > >
> > > +import java.lang.reflect.Field;
> > > import java.lang.reflect.InvocationTargetException;
> > > import java.lang.reflect.Method;
> > > import java.sql.SQLException;
> > > @@ -69,7 +70,13 @@
> > > /**
> > > * The name of the <code>getCause()</code> method.
> > > */
> > > - protected static final String CAUSE_METHOD_NAME = "getCause";
> > > + protected static final String[] CAUSE_METHOD_NAMES =
> > > + {
> > > + "getCause",
> > > + "getNextException",
> > > + "getTargetException",
> > > + "getException"
> > > + };
> > >
> > > /**
> > > * The parameters of the <code>getCause()</code> method.
> > > @@ -86,25 +93,50 @@
> > >
> > > /**
> > > * Introspects the specified <code>Throwable</code> for a
> > > - * <code>getCause()</code> method which returns a
> > > + * <code>getCause()</code>, <code>getNextException()</code>,
> > > + * <code>getTargetException()</code>, or
> > > + * <code>getException()</code> method which returns a
> > > * <code>Throwable</code> object (standard as of JDK 1.4, and
> part
> > > * of the {@link
> > > * org.apache.commons.lang.exception.NestableException} API),
> > > - * extracting and returning the cause of the exception.
> > > - * Otherwise, returns <code>null</code>.
> > > - *
> > > - * <p>TODO: Examine for a "detail" public member attribute from
> > > - * java.rmi.RemoteException.
> > > + * extracting and returning the cause of the exception. In the
> > > + * absence of any such method, the object is inspected for a
> > > + * <code>detail</code> field assignable to a
> > > + * <code>Throwable</code>. If none of the above is found,
> returns
> > > + * <code>null</code>.
> > > *
> > > * @param t The exception to introspect for a cause.
> > > * @return The cause of the <code>Throwable</code>.
> > > */
> > > public static Throwable getCause(Throwable t)
> > > {
> > > + return getCause(t, CAUSE_METHOD_NAMES);
> > > + }
> > > +
> > > + /**
> > > + * Extends the API of {@link #getCause(Throwable)} by
> > > + * introspecting for only user-specified method names.
> > > + *
> > > + * @see #getCause(Throwable)
> > > + */
> > > + public static Throwable getCause(Throwable t, String[]
> methodNames)
> > > + {
> > > Throwable cause = getCauseUsingWellKnownTypes(t);
> > > if (cause == null)
> > > {
> > > - cause = getCauseUsingMethodName(CAUSE_METHOD_NAME, t);
> > > + for (int i = 0; i < methodNames.length; i++)
> > > + {
> > > + cause = getCauseUsingMethodName(t, methodNames[i]);
> > > + if (cause != null)
> > > + {
> > > + break;
> > > + }
> > > + }
> > > +
> > > + if (cause == null)
> > > + {
> > > + cause = getCauseUsingFieldName(t, "detail");
> > > + }
> > > }
> > > return cause;
> > > }
> > > @@ -165,13 +197,13 @@
> > > }
> > >
> > > /**
> > > - * @param methodName The name of the method to find and invoke.
> > > * @param t The exception to examine.
> > > + * @param methodName The name of the method to find and invoke.
> > > * @return The wrapped exception, or <code>null</code> if not
> > > * found.
> > > */
> > > - private static Throwable getCauseUsingMethodName(String
> methodName,
> > > - Throwable t)
> > > + private static Throwable getCauseUsingMethodName(Throwable t,
> > > + String
> methodName)
> > > {
> > > Method method = null;
> > > try
> > > @@ -199,6 +231,44 @@
> > > {
> > > }
> > > catch (InvocationTargetException ignored)
> > > + {
> > > + }
> > > + }
> > > + return null;
> > > + }
> > > +
> > > + /**
> > > + * @param t The exception to examine.
> > > + * @param fieldName The name of the attribute to examine.
> > > + * @return The wrapped exception, or <code>null</code> if not
> > > + * found.
> > > + */
> > > + private static Throwable getCauseUsingFieldName(Throwable t,
> > > + String fieldName)
> > > + {
> > > + Field field = null;
> > > + try
> > > + {
> > > + field = t.getClass().getField(fieldName);
> > > + }
> > > + catch (NoSuchFieldException ignored)
> > > + {
> > > + }
> > > + catch (SecurityException ignored)
> > > + {
> > > + }
> > > +
> > > + if (field != null &&
> > > + Throwable.class.isAssignableFrom(field.getType()))
> > > + {
> > > + try
> > > + {
> > > + return (Throwable) field.get(t);
> > > + }
> > > + catch (IllegalAccessException ignored)
> > > + {
> > > + }
> > > + catch (IllegalArgumentException ignored)
> > > {
> > > }
> > > }
> >
> > --
> > To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> > For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> >
>
>
> --
> To unsubscribe, e-mail: <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
--
Daniel Rall <dl...@finemaltcoding.com>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [lang][dbcp] ExceptionUtils
Posted by Stephen Colebourne <sc...@btopenworld.com>.
I would like to see the ExceptionUtils class match the abilities of
NestedException. This means more methods ;-)
getThrowableCount(Throwable)
getThrowables(Throwable)
indexOfThrowable(Throwable, Class)
indexOfThrowable(Throwable, Class, int)
printStackTrace(Throwable, PrintWriter)
In theory this should be refactoring code from NestableDelegate to
ExceptionUtils.
Stephen
----- Original Message -----
From: "Daniel Rall" <dl...@finemaltcoding.com>
To: "Jakarta Commons Developers List" <co...@jakarta.apache.org>
Sent: Thursday, August 15, 2002 12:19 AM
Subject: [lang][dbcp] ExceptionUtils
> AFAIK, this takes care of ExceptionUtils for 1.0. Interested parties,
> please assure that this class is sufficient for your needs. Note that
> a couple more commits updated the JavaDoc on the constants in this
> class.
>
> dlr@apache.org writes:
>
> > dlr 2002/08/14 16:08:28
> >
> > Modified: lang/src/java/org/apache/commons/lang/exception
> > ExceptionUtils.java
> > Log:
> > CAUSE_METHOD_NAMES: Repurposed CAUSE_METHOD_NAME to be a list of
> > method names which may yield a wrapped exception.
> >
> > getCause(Throwable): Now just wraps getCause(Throwable, String[]),
> > passing in CAUSE_METHOD_NAMES as its list of method names.
> >
> > getCause(Throwable, String[]): Refactored code extracted from
> > getCause(Throwable) which looks at a list of method names, plus a
> > "detail" field.
> >
> > getCauseUsingMethodName(): Swapped parameter order for consistency.
> >
> > getCauseUsingFieldName(): New method which introspects fields instead
> > of methods.
> >
> > Revision Changes Path
> > 1.6 +81 -11
jakarta-commons/lang/src/java/org/apache/commons/lang/exception/ExceptionUti
ls.java
> >
> > Index: ExceptionUtils.java
> > ===================================================================
> > RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/exception/Ex
ceptionUtils.java,v
> > retrieving revision 1.5
> > retrieving revision 1.6
> > diff -u -u -r1.5 -r1.6
> > --- ExceptionUtils.java 14 Aug 2002 21:05:19 -0000 1.5
> > +++ ExceptionUtils.java 14 Aug 2002 23:08:28 -0000 1.6
> > @@ -54,6 +54,7 @@
> > * <http://www.apache.org/>.
> > */
> >
> > +import java.lang.reflect.Field;
> > import java.lang.reflect.InvocationTargetException;
> > import java.lang.reflect.Method;
> > import java.sql.SQLException;
> > @@ -69,7 +70,13 @@
> > /**
> > * The name of the <code>getCause()</code> method.
> > */
> > - protected static final String CAUSE_METHOD_NAME = "getCause";
> > + protected static final String[] CAUSE_METHOD_NAMES =
> > + {
> > + "getCause",
> > + "getNextException",
> > + "getTargetException",
> > + "getException"
> > + };
> >
> > /**
> > * The parameters of the <code>getCause()</code> method.
> > @@ -86,25 +93,50 @@
> >
> > /**
> > * Introspects the specified <code>Throwable</code> for a
> > - * <code>getCause()</code> method which returns a
> > + * <code>getCause()</code>, <code>getNextException()</code>,
> > + * <code>getTargetException()</code>, or
> > + * <code>getException()</code> method which returns a
> > * <code>Throwable</code> object (standard as of JDK 1.4, and
part
> > * of the {@link
> > * org.apache.commons.lang.exception.NestableException} API),
> > - * extracting and returning the cause of the exception.
> > - * Otherwise, returns <code>null</code>.
> > - *
> > - * <p>TODO: Examine for a "detail" public member attribute from
> > - * java.rmi.RemoteException.
> > + * extracting and returning the cause of the exception. In the
> > + * absence of any such method, the object is inspected for a
> > + * <code>detail</code> field assignable to a
> > + * <code>Throwable</code>. If none of the above is found,
returns
> > + * <code>null</code>.
> > *
> > * @param t The exception to introspect for a cause.
> > * @return The cause of the <code>Throwable</code>.
> > */
> > public static Throwable getCause(Throwable t)
> > {
> > + return getCause(t, CAUSE_METHOD_NAMES);
> > + }
> > +
> > + /**
> > + * Extends the API of {@link #getCause(Throwable)} by
> > + * introspecting for only user-specified method names.
> > + *
> > + * @see #getCause(Throwable)
> > + */
> > + public static Throwable getCause(Throwable t, String[]
methodNames)
> > + {
> > Throwable cause = getCauseUsingWellKnownTypes(t);
> > if (cause == null)
> > {
> > - cause = getCauseUsingMethodName(CAUSE_METHOD_NAME, t);
> > + for (int i = 0; i < methodNames.length; i++)
> > + {
> > + cause = getCauseUsingMethodName(t, methodNames[i]);
> > + if (cause != null)
> > + {
> > + break;
> > + }
> > + }
> > +
> > + if (cause == null)
> > + {
> > + cause = getCauseUsingFieldName(t, "detail");
> > + }
> > }
> > return cause;
> > }
> > @@ -165,13 +197,13 @@
> > }
> >
> > /**
> > - * @param methodName The name of the method to find and invoke.
> > * @param t The exception to examine.
> > + * @param methodName The name of the method to find and invoke.
> > * @return The wrapped exception, or <code>null</code> if not
> > * found.
> > */
> > - private static Throwable getCauseUsingMethodName(String
methodName,
> > - Throwable t)
> > + private static Throwable getCauseUsingMethodName(Throwable t,
> > + String
methodName)
> > {
> > Method method = null;
> > try
> > @@ -199,6 +231,44 @@
> > {
> > }
> > catch (InvocationTargetException ignored)
> > + {
> > + }
> > + }
> > + return null;
> > + }
> > +
> > + /**
> > + * @param t The exception to examine.
> > + * @param fieldName The name of the attribute to examine.
> > + * @return The wrapped exception, or <code>null</code> if not
> > + * found.
> > + */
> > + private static Throwable getCauseUsingFieldName(Throwable t,
> > + String fieldName)
> > + {
> > + Field field = null;
> > + try
> > + {
> > + field = t.getClass().getField(fieldName);
> > + }
> > + catch (NoSuchFieldException ignored)
> > + {
> > + }
> > + catch (SecurityException ignored)
> > + {
> > + }
> > +
> > + if (field != null &&
> > + Throwable.class.isAssignableFrom(field.getType()))
> > + {
> > + try
> > + {
> > + return (Throwable) field.get(t);
> > + }
> > + catch (IllegalAccessException ignored)
> > + {
> > + }
> > + catch (IllegalArgumentException ignored)
> > {
> > }
> > }
>
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>