View Javadoc

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      {
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 }