| 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 | 0 | { |
| 26 | 0 | } |
| 27 | |
|
| 28 | |
public static boolean containsInCausality( Throwable error, Class test ) |
| 29 | |
{ |
| 30 | 0 | Throwable cause = error; |
| 31 | |
|
| 32 | 0 | while ( cause != null ) |
| 33 | |
{ |
| 34 | 0 | if ( test.isInstance( cause ) ) |
| 35 | |
{ |
| 36 | 0 | return true; |
| 37 | |
} |
| 38 | |
|
| 39 | 0 | cause = cause.getCause(); |
| 40 | |
} |
| 41 | |
|
| 42 | 0 | return false; |
| 43 | |
} |
| 44 | |
|
| 45 | |
public static Throwable getRootCause( Throwable error ) |
| 46 | |
{ |
| 47 | 0 | Throwable cause = error; |
| 48 | |
|
| 49 | |
while ( true ) |
| 50 | |
{ |
| 51 | 0 | Throwable nextCause = cause.getCause(); |
| 52 | |
|
| 53 | 0 | if ( nextCause == null ) |
| 54 | |
{ |
| 55 | 0 | break; |
| 56 | |
} |
| 57 | |
else |
| 58 | |
{ |
| 59 | 0 | cause = nextCause; |
| 60 | |
} |
| 61 | 0 | } |
| 62 | |
|
| 63 | 0 | return cause; |
| 64 | |
} |
| 65 | |
|
| 66 | |
public static Throwable getFromCausality( Throwable error, Class targetClass ) |
| 67 | |
{ |
| 68 | 0 | Throwable cause = error; |
| 69 | |
|
| 70 | 0 | while ( cause != null ) |
| 71 | |
{ |
| 72 | 0 | if ( targetClass.isInstance( cause ) ) |
| 73 | |
{ |
| 74 | 0 | return cause; |
| 75 | |
} |
| 76 | |
|
| 77 | 0 | cause = cause.getCause(); |
| 78 | |
} |
| 79 | |
|
| 80 | 0 | return null; |
| 81 | |
} |
| 82 | |
|
| 83 | |
public static void appendRootCauseIfPresentAndUnique( Throwable error, StringBuffer message, |
| 84 | |
boolean includeTypeInfo ) |
| 85 | |
{ |
| 86 | 0 | if ( error == null ) |
| 87 | |
{ |
| 88 | 0 | return; |
| 89 | |
} |
| 90 | |
|
| 91 | 0 | Throwable root = getRootCause( error ); |
| 92 | |
|
| 93 | 0 | if ( root != null && !root.equals( error ) ) |
| 94 | |
{ |
| 95 | 0 | String rootMsg = root.getMessage(); |
| 96 | |
|
| 97 | 0 | if ( rootMsg != null && ( error.getMessage() == null || error.getMessage().indexOf( rootMsg ) < 0 ) ) |
| 98 | |
{ |
| 99 | 0 | message.append( "\n" ).append( rootMsg ); |
| 100 | |
|
| 101 | 0 | if ( includeTypeInfo ) |
| 102 | |
{ |
| 103 | 0 | message.append( "\nRoot error type: " ).append( root.getClass().getName() ); |
| 104 | |
} |
| 105 | |
} |
| 106 | |
} |
| 107 | 0 | } |
| 108 | |
} |