KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > ExceptionUtil


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import gcc.*;
22 import java.io.*;
23 import java.util.*;
24
25 public abstract class ExceptionUtil
26 {
27     public static List addException(List exceptions, Throwable ex)
28     {
29         if (exceptions == null)
30         {
31             exceptions = new ArrayList(1);
32         }
33         exceptions.add(ex);
34         return exceptions;
35     }
36
37     public static void checkExceptions(List exceptions)
38     {
39         if (exceptions != null)
40         {
41             int n = exceptions.size();
42             if (n == 1)
43             {
44                 Throwable ex = (Throwable)exceptions.get(0);
45                 if (ex instanceof Error)
46                 {
47                     throw (Error)ex;
48                 }
49                 if (ex instanceof RuntimeException)
50                 {
51                     throw (RuntimeException)ex;
52                 }
53                 throw new SystemException(ex);
54             }
55             else
56             {
57                 StringBuffer buffer = new StringBuffer();
58                 for (Iterator i = exceptions.iterator(); i.hasNext();)
59                 {
60                     Exception ex = (Exception)i.next();
61                     if (buffer.length() > 0)
62                     {
63                         buffer.append("\n______________________________________________________________\n\n");
64                     }
65                     buffer.append(ExceptionUtil.getStackTrace(ex));
66                 }
67                 throw new SystemException(buffer.toString());
68             }
69         }
70     }
71
72     public static String getDivider()
73     {
74         return "\n ______________________________________________________________\n";
75     }
76
77     /**
78      ** Construct a detail message for an exception which doesn't take a
79      ** cause parameter in its constructor.
80      **/

81     public static String causedBy(Throwable ex)
82     {
83         return "\nCaused by: " + getStackTrace(ex) + getDivider();
84     }
85
86     public static String getCauseChain(Throwable ex)
87     {
88         String stackTrace = getStackTrace(ex);
89         return getCauseChain(stackTrace);
90     }
91
92     public static String getCauseChain(String stackTrace)
93     {
94         try
95         {
96             BufferedReader input = new BufferedReader(new StringReader(stackTrace));
97             StringBuffer output = new StringBuffer(100);
98             String line;
99             while ((line = input.readLine()) != null)
100             {
101                 line = line.trim();
102                 if (! line.startsWith("at ") && ! line.startsWith("... "))
103                 {
104                     output.append(line);
105                     output.append('\n');
106                 }
107             }
108             return output.toString();
109         }
110         catch (Exception ex2)
111         {
112             ex2.printStackTrace();
113             return stackTrace;
114         }
115     }
116
117     public static String getStackTrace(Throwable ex)
118     {
119         java.io.StringWriter sw = new java.io.StringWriter();
120         java.io.PrintWriter pw = new java.io.PrintWriter(sw);
121         ex.printStackTrace(pw);
122         return sw.toString().trim();
123     }
124
125     public static String getCurrentStackTrace()
126     {
127         return StringUtil.removePrefix(getStackTrace(new Exception()), "java.lang.Exception:");
128     }
129
130     public static String indentLines(String lines)
131     {
132         return " " + StringUtil.replace(lines.trim(), "\n", "\n ");
133     }
134
135     public static boolean isApplicationException(Throwable ex)
136     {
137         return ! isSystemException(ex);
138     }
139
140     public static boolean isSystemException(Throwable ex)
141     {
142         Class exClass = ex.getClass();
143         return Error.class.isAssignableFrom(exClass)
144             || RuntimeException.class.isAssignableFrom(exClass);
145     }
146
147     public static boolean isUserException(Class exClass)
148     {
149         if (RuntimeException.class.isAssignableFrom(exClass)
150         || Error.class.isAssignableFrom(exClass))
151         {
152             return false;
153         }
154         return true;
155     }
156
157     public static RuntimeException getRuntimeException(Exception ex)
158     {
159         if (ex instanceof RuntimeException)
160         {
161             return (RuntimeException)ex;
162         }
163         else
164         {
165             return new SystemException(ex);
166         }
167     }
168
169     public static RuntimeException rethrow(Throwable ex)
170     {
171         if (ex instanceof Error)
172         {
173             throw (Error)ex;
174         }
175         else if (ex instanceof RuntimeException)
176         {
177             return (RuntimeException)ex;
178         }
179         else
180         {
181             return new CheckedException(ex);
182         }
183     }
184 }
185
Popular Tags