1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.util.internal;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.regex.Pattern;
21
22 import org.jboss.netty.channel.ChannelPipeline;
23 import org.jboss.netty.channel.SimpleChannelHandler;
24 import org.jboss.netty.util.DebugUtil;
25 import org.jboss.netty.util.ThreadRenamingRunnable;
26
27
28
29
30
31
32 public final class StackTraceSimplifier {
33
34 private static final boolean SIMPLIFY_STACK_TRACE = !DebugUtil.isDebugEnabled();
35 private static final Pattern EXCLUDED_STACK_TRACE =
36 Pattern.compile(
37 "^org\\.jboss\\.netty\\." +
38 "(util\\.(ThreadRenamingRunnable|internal\\.DeadLockProofWorker)" +
39 "|channel\\.(SimpleChannel(Upstream|Downstream)?Handler|(Default|Static)ChannelPipeline.*))(\\$.*)?$");
40
41
42
43
44
45
46
47 public static void simplify(Throwable e) {
48 if (!SIMPLIFY_STACK_TRACE) {
49 return;
50 }
51
52 if (e.getCause() != null) {
53 simplify(e.getCause());
54 }
55
56 StackTraceElement[] trace = e.getStackTrace();
57 if (trace == null || trace.length == 0) {
58 return;
59 }
60
61
62 if (EXCLUDED_STACK_TRACE.matcher(trace[0].getClassName()).matches()) {
63 return;
64 }
65
66 List<StackTraceElement> simpleTrace =
67 new ArrayList<StackTraceElement>(trace.length);
68
69 simpleTrace.add(trace[0]);
70
71
72 for (int i = 1; i < trace.length; i ++) {
73 if (EXCLUDED_STACK_TRACE.matcher(trace[i].getClassName()).matches()) {
74 continue;
75 }
76 simpleTrace.add(trace[i]);
77 }
78
79 e.setStackTrace(
80 simpleTrace.toArray(new StackTraceElement[simpleTrace.size()]));
81 }
82
83 private StackTraceSimplifier() {
84
85 }
86 }