KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > filters > StackTraceElementFilter


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

15 package org.josql.filters;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19
20 import org.josql.Query;
21 import org.josql.QueryParseException;
22 import org.josql.QueryExecutionException;
23
24 /**
25  * This class allows you to filter the stack trace of a Throwable, so cutting down
26  * on the (sometimes) pointless long stack traces.
27  * <p>
28  * Usage:
29  * <pre>
30  * SELECT *
31  * FROM java.lang.StackTraceElement
32  * WHERE className LIKE 'org.josql.%'
33  * </pre>
34  * And then to use in code:
35  * <pre>
36  * StackTraceElementFilter f = new StackTraceElementFilter (sql);
37  * f.filter (new Throwable ());
38  * </pre>
39  * <p>
40  * Last Modified By: $Author: barrygently $<br />
41  * Last Modified On: $Date: 2005/01/07 17:10:40 $<br />
42  * Current Revision: $Revision: 1.1 $<br />
43  */

44 public class StackTraceElementFilter extends AbstractJoSQLFilter
45 {
46
47     /**
48      * Init this filter with the query.
49      *
50      * @param q The query.
51      * @throws QueryParseException If there is an issue with the parsing of the query,
52      * or if the FROM class is not equal to the expected class.
53      */

54     public StackTraceElementFilter (String JavaDoc q)
55                                 throws QueryParseException
56     {
57
58     super (q);
59
60     }
61
62     /**
63      * Get the expected class.
64      *
65      * @return {@link StackTraceElement}.
66      */

67     public Class JavaDoc getExpectedClass ()
68     {
69
70     return StackTraceElement JavaDoc.class;
71
72     }
73
74     /**
75      * Init this file filter with the query already built and parsed.
76      *
77      * @param q The query.
78      * @throws IllegalStateException If the Query object has not been parsed.
79      * @throws QueryParseException If the FROM class is not as expected.
80      */

81     public StackTraceElementFilter (Query q)
82                                 throws IllegalStateException JavaDoc,
83                                        QueryParseException
84     {
85
86     super (q);
87
88     }
89
90     /*
91      * Returns <code>true</code> if the where clause evaluates to true for the
92      * passed in StackTraceElement.
93      *
94      * @param o The object to evaluate the WHERE clause against.
95      * @return <code>true</code> if the WHERE clause evaluates to <code>true</code> for the specified
96      * object.
97      */

98     public boolean accept (Object JavaDoc o)
99     {
100
101     try
102     {
103
104         return this.accept ((StackTraceElement JavaDoc) o);
105
106     } catch (Exception JavaDoc e) {
107
108         this.exp = e;
109
110         return false;
111
112     }
113
114     }
115
116     /**
117      * Returns <code>true</code> if the where clause evaluates to true for the
118      * passed in StackTraceElement.
119      *
120      * @param s The object to evaluate the WHERE clause against.
121      * @return <code>true</code> if the WHERE clause evaluates to <code>true</code> for the specified
122      * object.
123      */

124     public boolean accept (StackTraceElement JavaDoc s)
125                        throws QueryExecutionException
126     {
127
128     return this.q.getWhereClause ().isTrue (s,
129                         this.q);
130
131     }
132     
133     /**
134      * Filter the specified stack trace and return the new stack trace
135      * that can then be set in the throwable.
136      *
137      * @param s The stack trace.
138      * @return The new stack trace, filtered.
139      * @throws QueryExecutionException If the where clause cannot be
140      * executed against a particular element.
141      */

142     public StackTraceElement JavaDoc[] filterStackTrace (StackTraceElement JavaDoc[] s)
143                                              throws QueryExecutionException
144     {
145
146     if (s == null)
147     {
148
149         return s;
150
151     }
152
153     List JavaDoc l = new ArrayList JavaDoc ();
154
155     for (int i = 0; i < s.length; i++)
156     {
157
158         if (this.accept (s[i]))
159         {
160
161         l.add (s[i]);
162
163         }
164
165     }
166
167     // Can't believe I have to fricking do this... can't just cast to
168
// StackTraceElement[]... grr...
169
StackTraceElement JavaDoc[] a = new StackTraceElement JavaDoc[l.size ()];
170
171     for (int i = 0; i < l.size (); i++)
172     {
173
174         a[i] = (StackTraceElement JavaDoc) l.get (i);
175
176     }
177
178     return a;
179
180     }
181
182     /**
183      * Filter the throwable, but will also filter any causes (right up the cause chain)
184      * as well. Equivalent to calling: {@link #filter(Throwable,boolean)} with the
185      * boolean set to <code>true</code>.
186      *
187      * @param t The throwable instance to filter.
188      * @throws QueryExecutionException If the where clause cannot be executed.
189      */

190     public void filter (Throwable JavaDoc t)
191                     throws QueryExecutionException
192     {
193
194     this.filter (t,
195              true);
196
197     }
198
199     /**
200      * Filter the throwable, but will also filter any causes (right up the cause chain)
201      * as well if the <b>filterCause</b> boolean is set to <code>true</code>.
202      *
203      * @param t The throwable instance to filter.
204      * @param filterCause When set to <code>true</code> will also filter the cause
205      * chain as well.
206      * @throws QueryExecutionException If the where clause cannot be executed.
207      */

208     public void filter (Throwable JavaDoc t,
209             boolean filterCause)
210                     throws QueryExecutionException
211     {
212
213     if (filterCause)
214     {
215
216         Throwable JavaDoc c = t.getCause ();
217
218         if (c != null)
219         {
220
221         this.filter (c,
222                  filterCause);
223
224         }
225
226     }
227
228     t.setStackTrace (this.filterStackTrace (t.getStackTrace ()));
229
230     }
231
232
233 }
234
Popular Tags