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