Coverage Report - org.apache.maven.usability.diagnostics.DiagnosisUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
DiagnosisUtils
0%
0/31
0%
0/24
3.6
 
 1  
 package org.apache.maven.usability.diagnostics;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *  http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 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  
 }