1 package org.codehaus.plexus.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import java.io.PrintStream;
58 import java.io.PrintWriter;
59 import java.io.StringWriter;
60 import java.lang.reflect.Field;
61 import java.lang.reflect.InvocationTargetException;
62 import java.lang.reflect.Method;
63 import java.sql.SQLException;
64 import java.util.ArrayList;
65 import java.util.Arrays;
66 import java.util.LinkedList;
67 import java.util.List;
68 import java.util.StringTokenizer;
69
70
71
72
73
74
75
76
77
78
79
80
81 public class ExceptionUtils
82 {
83
84
85
86
87 static final String WRAPPED_MARKER = " [wrapped] ";
88
89
90
91
92 protected static String[] CAUSE_METHOD_NAMES = { "getCause", "getNextException", "getTargetException",
93 "getException", "getSourceException", "getRootCause", "getCausedByException", "getNested" };
94
95
96
97
98 protected ExceptionUtils()
99 {
100 }
101
102
103
104
105
106
107
108
109 public static void addCauseMethodName( String methodName )
110 {
111 if ( methodName != null && methodName.length() > 0 )
112 {
113 List<String> list = new ArrayList<String>( Arrays.asList( CAUSE_METHOD_NAMES ) );
114 list.add( methodName );
115 CAUSE_METHOD_NAMES = list.toArray( new String[0] );
116 }
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 public static Throwable getCause( Throwable throwable )
151 {
152 return getCause( throwable, CAUSE_METHOD_NAMES );
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166 public static Throwable getCause( Throwable throwable, String[] methodNames )
167 {
168 Throwable cause = getCauseUsingWellKnownTypes( throwable );
169 if ( cause == null )
170 {
171 for ( String methodName : methodNames )
172 {
173 cause = getCauseUsingMethodName( throwable, methodName );
174 if ( cause != null )
175 {
176 break;
177 }
178 }
179
180 if ( cause == null )
181 {
182 cause = getCauseUsingFieldName( throwable, "detail" );
183 }
184 }
185 return cause;
186 }
187
188
189
190
191
192
193
194
195
196
197 public static Throwable getRootCause( Throwable throwable )
198 {
199 Throwable cause = getCause( throwable );
200 if ( cause != null )
201 {
202 throwable = cause;
203 while ( ( throwable = getCause( throwable ) ) != null )
204 {
205 cause = throwable;
206 }
207 }
208 return cause;
209 }
210
211
212
213
214
215
216
217
218
219
220 private static Throwable getCauseUsingWellKnownTypes( Throwable throwable )
221 {
222 if ( throwable instanceof SQLException )
223 {
224 return ( (SQLException) throwable ).getNextException();
225 }
226 else if ( throwable instanceof InvocationTargetException )
227 {
228 return ( (InvocationTargetException) throwable ).getTargetException();
229 }
230 else
231 {
232 return null;
233 }
234 }
235
236
237
238
239
240
241
242
243
244
245 private static Throwable getCauseUsingMethodName( Throwable throwable, String methodName )
246 {
247 Method method = null;
248 try
249 {
250 method = throwable.getClass().getMethod( methodName, null );
251 }
252 catch ( NoSuchMethodException ignored )
253 {
254 }
255 catch ( SecurityException ignored )
256 {
257 }
258
259 if ( method != null && Throwable.class.isAssignableFrom( method.getReturnType() ) )
260 {
261 try
262 {
263 return (Throwable) method.invoke( throwable, new Object[0] );
264 }
265 catch ( IllegalAccessException ignored )
266 {
267 }
268 catch ( IllegalArgumentException ignored )
269 {
270 }
271 catch ( InvocationTargetException ignored )
272 {
273 }
274 }
275 return null;
276 }
277
278
279
280
281
282
283
284
285
286
287 private static Throwable getCauseUsingFieldName( Throwable throwable, String fieldName )
288 {
289 Field field = null;
290 try
291 {
292 field = throwable.getClass().getField( fieldName );
293 }
294 catch ( NoSuchFieldException ignored )
295 {
296 }
297 catch ( SecurityException ignored )
298 {
299 }
300
301 if ( field != null && Throwable.class.isAssignableFrom( field.getType() ) )
302 {
303 try
304 {
305 return (Throwable) field.get( throwable );
306 }
307 catch ( IllegalAccessException ignored )
308 {
309 }
310 catch ( IllegalArgumentException ignored )
311 {
312 }
313 }
314 return null;
315 }
316
317
318
319
320
321
322
323
324
325 public static int getThrowableCount( Throwable throwable )
326 {
327
328 int count = 0;
329 while ( throwable != null )
330 {
331 count++;
332 throwable = ExceptionUtils.getCause( throwable );
333 }
334 return count;
335 }
336
337
338
339
340
341
342
343
344
345 public static Throwable[] getThrowables( Throwable throwable )
346 {
347 List<Throwable> list = new ArrayList<>();
348 while ( throwable != null )
349 {
350 list.add( throwable );
351 throwable = getCause( throwable );
352 }
353 return list.toArray( new Throwable[0] );
354 }
355
356
357
358
359
360
361
362
363
364
365
366 public static int indexOfThrowable( Throwable throwable, Class type )
367 {
368 return indexOfThrowable( throwable, type, 0 );
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385 public static int indexOfThrowable( Throwable throwable, Class type, int fromIndex )
386 {
387 if ( fromIndex < 0 )
388 {
389 throw new IndexOutOfBoundsException( "Throwable index out of range: " + fromIndex );
390 }
391 Throwable[] throwables = ExceptionUtils.getThrowables( throwable );
392 if ( fromIndex >= throwables.length )
393 {
394 throw new IndexOutOfBoundsException( "Throwable index out of range: " + fromIndex );
395 }
396 for ( int i = fromIndex; i < throwables.length; i++ )
397 {
398 if ( throwables[i].getClass().equals( type ) )
399 {
400 return i;
401 }
402 }
403 return -1;
404 }
405
406
407
408
409
410
411
412
413
414
415 public static void printRootCauseStackTrace( Throwable t, PrintStream stream )
416 {
417 String trace[] = getRootCauseStackTrace( t );
418 for ( String aTrace : trace )
419 {
420 stream.println( aTrace );
421 }
422 stream.flush();
423 }
424
425
426
427
428
429 public static void printRootCauseStackTrace( Throwable t )
430 {
431 printRootCauseStackTrace( t, System.err );
432 }
433
434
435
436
437
438
439 public static void printRootCauseStackTrace( Throwable t, PrintWriter writer )
440 {
441 String trace[] = getRootCauseStackTrace( t );
442 for ( String aTrace : trace )
443 {
444 writer.println( aTrace );
445 }
446 writer.flush();
447 }
448
449
450
451
452
453
454
455 public static String[] getRootCauseStackTrace( Throwable t )
456 {
457 Throwable[] throwables = getThrowables( t );
458 int count = throwables.length;
459 ArrayList<String> frames = new ArrayList<>();
460 List<String> nextTrace = getStackFrameList( throwables[count - 1] );
461 for ( int i = count; --i >= 0; )
462 {
463 List<String> trace = nextTrace;
464 if ( i != 0 )
465 {
466 nextTrace = getStackFrameList( throwables[i - 1] );
467 removeCommonFrames( trace, nextTrace );
468 }
469 if ( i == ( count - 1 ) )
470 {
471 frames.add( throwables[i].toString() );
472 }
473 else
474 {
475 frames.add( WRAPPED_MARKER + throwables[i].toString() );
476 }
477 for ( String aTrace : trace )
478 {
479 frames.add( aTrace );
480 }
481 }
482 return frames.toArray( new String[0] );
483 }
484
485
486
487
488
489
490
491 private static void removeCommonFrames( List<String> causeFrames, List<String> wrapperFrames )
492 {
493 int causeFrameIndex = causeFrames.size() - 1;
494 int wrapperFrameIndex = wrapperFrames.size() - 1;
495 while ( causeFrameIndex >= 0 && wrapperFrameIndex >= 0 )
496 {
497
498
499 String causeFrame = causeFrames.get( causeFrameIndex );
500 String wrapperFrame = wrapperFrames.get( wrapperFrameIndex );
501 if ( causeFrame.equals( wrapperFrame ) )
502 {
503 causeFrames.remove( causeFrameIndex );
504 }
505 causeFrameIndex--;
506 wrapperFrameIndex--;
507 }
508 }
509
510
511
512
513
514
515
516 public static String getStackTrace( Throwable t )
517 {
518 StringWriter sw = new StringWriter();
519 PrintWriter pw = new PrintWriter( sw, true );
520 t.printStackTrace( pw );
521 return sw.getBuffer().toString();
522 }
523
524
525
526
527
528
529
530 public static String getFullStackTrace( Throwable t )
531 {
532 StringWriter sw = new StringWriter();
533 PrintWriter pw = new PrintWriter( sw, true );
534 Throwable[] ts = getThrowables( t );
535 for ( Throwable t1 : ts )
536 {
537 t1.printStackTrace( pw );
538 if ( isNestedThrowable( t1 ) )
539 {
540 break;
541 }
542 }
543 return sw.getBuffer().toString();
544 }
545
546
547
548
549
550
551
552 public static boolean isNestedThrowable( Throwable throwable )
553 {
554 if ( throwable == null )
555 {
556 return false;
557 }
558
559 if ( throwable instanceof SQLException )
560 {
561 return true;
562 }
563 else if ( throwable instanceof InvocationTargetException )
564 {
565 return true;
566 }
567
568 for ( String CAUSE_METHOD_NAME : CAUSE_METHOD_NAMES )
569 {
570 try
571 {
572 Method method = throwable.getClass().getMethod( CAUSE_METHOD_NAME, null );
573 if ( method != null )
574 {
575 return true;
576 }
577 }
578 catch ( NoSuchMethodException ignored )
579 {
580 }
581 catch ( SecurityException ignored )
582 {
583 }
584 }
585
586 try
587 {
588 Field field = throwable.getClass().getField( "detail" );
589 if ( field != null )
590 {
591 return true;
592 }
593 }
594 catch ( NoSuchFieldException ignored )
595 {
596 }
597 catch ( SecurityException ignored )
598 {
599 }
600
601 return false;
602 }
603
604
605
606
607
608
609
610
611 public static String[] getStackFrames( Throwable t )
612 {
613 return getStackFrames( getStackTrace( t ) );
614 }
615
616
617
618
619 static String[] getStackFrames( String stackTrace )
620 {
621 String linebreak = System.getProperty( "line.separator" );
622 StringTokenizer frames = new StringTokenizer( stackTrace, linebreak );
623 List<String> list = new LinkedList<String>();
624 while ( frames.hasMoreTokens() )
625 {
626 list.add( frames.nextToken() );
627 }
628 return list.toArray( new String[0] );
629 }
630
631
632
633
634
635
636
637
638 static List<String> getStackFrameList( Throwable t )
639 {
640 String stackTrace = getStackTrace( t );
641 String linebreak = System.getProperty( "line.separator" );
642 StringTokenizer frames = new StringTokenizer( stackTrace, linebreak );
643 List<String> list = new LinkedList<String>();
644 boolean traceStarted = false;
645 while ( frames.hasMoreTokens() )
646 {
647 String token = frames.nextToken();
648
649 int at = token.indexOf( "at" );
650 if ( at != -1 && token.substring( 0, at ).trim().length() == 0 )
651 {
652 traceStarted = true;
653 list.add( token );
654 }
655 else if ( traceStarted )
656 {
657 break;
658 }
659 }
660 return list;
661 }
662 }