avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-avalon/src/java/org/apache/avalon/framework ExceptionUtil.java
Date Fri, 01 Mar 2002 10:24:00 GMT
donaldp     02/03/01 02:24:00

  Modified:    src/java/org/apache/avalon/framework ExceptionUtil.java
  Log:
  Allow you to print out nexted/cascading/etc exceptions when the exception object has a method
  
  Throwable getCause()
  
  This is to make it possible to print out all exceptions on JDK1.4
  
  Revision  Changes    Path
  1.8       +76 -14    jakarta-avalon/src/java/org/apache/avalon/framework/ExceptionUtil.java
  
  Index: ExceptionUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon/src/java/org/apache/avalon/framework/ExceptionUtil.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ExceptionUtil.java	11 Dec 2001 09:00:44 -0000	1.7
  +++ ExceptionUtil.java	1 Mar 2002 10:24:00 -0000	1.8
  @@ -9,8 +9,9 @@
   
   import java.io.PrintWriter;
   import java.io.StringWriter;
  -import java.util.ArrayList;
   import java.util.StringTokenizer;
  +import java.lang.reflect.Method;
  +import java.lang.reflect.InvocationTargetException;
   
   /**
    * This class provides basic facilities for manipulating exceptions.
  @@ -22,6 +23,10 @@
    */
   public final class ExceptionUtil
   {
  +    private static final String LINE_SEPARATOR = "\n";
  +    private static final String GET_CAUSE_NAME = "getCause";
  +    private static final Class[] GET_CAUSE_PARAMTYPES = new Class[ 0 ];
  +
       /**
        * Private constructor to prevent instantiation.
        */
  @@ -29,17 +34,31 @@
       {
       }
   
  +    /**
  +     * Generate string for specified exception and the cause of
  +     * this exception (if any).
  +     */
       public static String printStackTrace( final Throwable throwable )
       {
           return printStackTrace( throwable, 0, true );
       }
   
  +    /**
  +     * Generate string for specified exception and if printCascading
  +     * is true will print all cascading exceptions.
  +     */
       public static String printStackTrace( final Throwable throwable,
                                             final boolean printCascading )
       {
           return printStackTrace( throwable, 0, printCascading );
       }
   
  +    /**
  +     * Serialize the specified <code>Throwable</code> to a string.
  +     * Restrict the number of frames printed out to the specified depth.
  +     * If the depth specified is <code>0</code> then all the frames are
  +     * converted into a string.
  +     */
       public static String printStackTrace( final Throwable throwable, final int depth )
       {
           int dp = depth;
  @@ -55,19 +74,38 @@
           for( int i = 0; i < dp; i++ )
           {
               sb.append( lines[ i ] );
  -            sb.append( '\n' );
  +            sb.append( LINE_SEPARATOR );
           }
   
           return sb.toString();
       }
   
  +    /**
  +     * Generate exception string for specified exception to specified depth
  +     * and all Cascading exceptions if printCascading is true.
  +     */
       public static String printStackTrace( final Throwable throwable,
                                             final int depth,
                                             final boolean printCascading )
       {
  +        return printStackTrace( throwable, depth, printCascading, false );
  +    }
  +
  +    /**
  +     * Generate exception string for specified exception to specified depth
  +     * and all Cascading exceptions if printCascading is true. If useReflection
  +     * is true then the method will also attempt to use reflection to find a
  +     * method with signature <code>Throwable getCause()</code>. This makes
  +     * it compatible with JDK1.4 mechanisms for nesting exceptions.
  +     */
  +    public static String printStackTrace( final Throwable throwable,
  +                                          final int depth,
  +                                          final boolean printCascading,
  +                                          final boolean useReflection )
  +    {
           final String result = printStackTrace( throwable, depth );
   
  -        if( !printCascading || !(throwable instanceof CascadingThrowable) )
  +        if( !printCascading )
           {
               return result;
           }
  @@ -76,21 +114,15 @@
               final StringBuffer sb = new StringBuffer();
               sb.append( result );
   
  -            Throwable cause = ((CascadingThrowable)throwable).getCause();
  +            Throwable cause = getCause( throwable, useReflection );
   
               while( null != cause )
               {
  -                sb.append( "rethrown from\n" );
  +                sb.append( "rethrown from" );
  +                sb.append( LINE_SEPARATOR );
                   sb.append( printStackTrace( cause, depth ) );
   
  -                if( cause instanceof CascadingThrowable )
  -                {
  -                    cause = ((CascadingThrowable)cause).getCause();
  -                }
  -                else
  -                {
  -                    cause = null;
  -                }
  +                cause = getCause( cause, useReflection );
               }
   
               return sb.toString();
  @@ -98,6 +130,36 @@
       }
   
       /**
  +     * Utility method to get cause of exception.
  +     */
  +    private static Throwable getCause( final Throwable throwable,
  +                                       final boolean useReflection )
  +    {
  +        if( throwable instanceof CascadingThrowable )
  +        {
  +            return ( (CascadingThrowable)throwable ).getCause();
  +        }
  +        else if( useReflection )
  +        {
  +            try
  +            {
  +                final Class clazz = throwable.getClass();
  +                final Method method =
  +                    clazz.getMethod( GET_CAUSE_NAME, GET_CAUSE_PARAMTYPES );
  +                return (Throwable)method.invoke( throwable, null );
  +            }
  +            catch( final Throwable t )
  +            {
  +                return null;
  +            }
  +        }
  +        else
  +        {
  +            return null;
  +        }
  +    }
  +
  +    /**
        * Captures the stack trace associated with this exception.
        *
        * @return an array of Strings describing stack frames.
  @@ -106,7 +168,7 @@
       {
           final StringWriter sw = new StringWriter();
           throwable.printStackTrace( new PrintWriter( sw, true ) );
  -        return splitString( sw.toString(), "\n" );
  +        return splitString( sw.toString(), LINE_SEPARATOR );
       }
   
       /**
  
  
  

--
To unsubscribe, e-mail:   <mailto:avalon-cvs-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:avalon-cvs-help@jakarta.apache.org>


Mime
View raw message