1 package org.apache.maven.usability.diagnostics;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public final class DiagnosisUtils
23 {
24 private DiagnosisUtils()
25 {
26 }
27
28 public static boolean containsInCausality( Throwable error, Class test )
29 {
30 Throwable cause = error;
31
32 while ( cause != null )
33 {
34 if ( test.isInstance( cause ) )
35 {
36 return true;
37 }
38
39 cause = cause.getCause();
40 }
41
42 return false;
43 }
44
45 public static Throwable getRootCause( Throwable error )
46 {
47 Throwable cause = error;
48
49 while ( true )
50 {
51 Throwable nextCause = cause.getCause();
52
53 if ( nextCause == null )
54 {
55 break;
56 }
57 else
58 {
59 cause = nextCause;
60 }
61 }
62
63 return cause;
64 }
65
66 public static Throwable getFromCausality( Throwable error, Class targetClass )
67 {
68 Throwable cause = error;
69
70 while ( cause != null )
71 {
72 if ( targetClass.isInstance( cause ) )
73 {
74 return cause;
75 }
76
77 cause = cause.getCause();
78 }
79
80 return null;
81 }
82
83 public static void appendRootCauseIfPresentAndUnique( Throwable error, StringBuffer message,
84 boolean includeTypeInfo )
85 {
86 if ( error == null )
87 {
88 return;
89 }
90
91 Throwable root = getRootCause( error );
92
93 if ( root != null && !root.equals( error ) )
94 {
95 String rootMsg = root.getMessage();
96
97 if ( rootMsg != null && ( error.getMessage() == null || error.getMessage().indexOf( rootMsg ) < 0 ) )
98 {
99 message.append( "\n" ).append( rootMsg );
100
101 if ( includeTypeInfo )
102 {
103 message.append( "\nRoot error type: " ).append( root.getClass().getName() );
104 }
105 }
106 }
107 }
108 }