1 55 package org.apache.avalon.framework; 56 57 import java.io.PrintWriter ; 58 import java.io.StringWriter ; 59 import java.lang.reflect.Method ; 60 import java.util.StringTokenizer ; 61 62 70 public final class ExceptionUtil 71 { 72 private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); 73 private static final String GET_CAUSE_NAME = "getCause"; 74 private static final Class [] GET_CAUSE_PARAMTYPES = new Class [ 0 ]; 75 76 79 private ExceptionUtil() 80 { 81 } 82 83 89 public static String printStackTrace( final Throwable throwable ) 90 { 91 return printStackTrace( throwable, 0, true ); 92 } 93 94 101 public static String printStackTrace( final Throwable throwable, 102 final boolean printCascading ) 103 { 104 return printStackTrace( throwable, 0, printCascading ); 105 } 106 107 116 public static String printStackTrace( final Throwable throwable, final int depth ) 117 { 118 int dp = depth; 119 final String [] lines = captureStackTrace( throwable ); 120 121 if( 0 == dp || dp > lines.length ) 122 { 123 dp = lines.length; 124 } 125 126 final StringBuffer sb = new StringBuffer (); 127 128 for( int i = 0; i < dp; i++ ) 129 { 130 sb.append( lines[ i ] ); 131 sb.append( LINE_SEPARATOR ); 132 } 133 134 return sb.toString(); 135 } 136 137 145 public static String printStackTrace( final Throwable throwable, 146 final int depth, 147 final boolean printCascading ) 148 { 149 return printStackTrace( throwable, depth, printCascading, true ); 150 } 151 152 165 public static String printStackTrace( final Throwable throwable, 166 final int depth, 167 final boolean printCascading, 168 final boolean useReflection ) 169 { 170 final String result = printStackTrace( throwable, depth ); 171 172 if( !printCascading ) 173 { 174 return result; 175 } 176 else 177 { 178 final StringBuffer sb = new StringBuffer (); 179 sb.append( result ); 180 181 Throwable cause = getCause( throwable, useReflection ); 182 183 while( null != cause ) 184 { 185 sb.append( "rethrown from" ); 186 sb.append( LINE_SEPARATOR ); 187 sb.append( printStackTrace( cause, depth ) ); 188 189 cause = getCause( cause, useReflection ); 190 } 191 192 return sb.toString(); 193 } 194 } 195 196 203 public static Throwable getCause( final Throwable throwable, 204 final boolean useReflection ) 205 { 206 if( throwable instanceof CascadingThrowable ) 207 { 208 return ( (CascadingThrowable)throwable ).getCause(); 209 } 210 else if( useReflection ) 211 { 212 try 213 { 214 final Class clazz = throwable.getClass(); 215 final Method method = 216 clazz.getMethod( GET_CAUSE_NAME, GET_CAUSE_PARAMTYPES ); 217 return (Throwable )method.invoke( throwable, null ); 218 } 219 catch( final Throwable t ) 220 { 221 return null; 222 } 223 } 224 else 225 { 226 return null; 227 } 228 } 229 230 236 public static String [] captureStackTrace( final Throwable throwable ) 237 { 238 final StringWriter sw = new StringWriter (); 239 throwable.printStackTrace( new PrintWriter ( sw, true ) ); 240 return splitStringInternal( sw.toString(), LINE_SEPARATOR ); 241 } 242 243 251 public static String [] splitString( final String string, final String onToken ) 252 { 253 return splitStringInternal( string, onToken ); 254 } 255 256 263 private static String [] splitStringInternal( final String string, final String onToken ) 264 { 265 final StringTokenizer tokenizer = new StringTokenizer ( string, onToken ); 266 final String [] result = new String [ tokenizer.countTokens() ]; 267 268 for( int i = 0; i < result.length; i++ ) 269 { 270 result[ i ] = tokenizer.nextToken(); 271 } 272 273 return result; 274 } 275 } 276 | Popular Tags |