forked from erlang/erlide_eclipse
/
ExceptionUtils.java
120 lines (108 loc) · 4.13 KB
/
ExceptionUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Original code from:
* http://www.dzone.com/links/r/some_junit_tricks_for_easier_and_better_test_cases.html
*/
package org.erlide.test.support;
import static com.google.common.collect.Lists.*;
import static com.google.common.collect.Sets.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Set;
public class ExceptionUtils {
private static final String INDENT = "\t";
private static List<String> _suppressedPackages = newArrayList("$Proxy",
"org.junit", "java.lang.reflect.Method", "sun.", "org.eclipse",
"junit.framework");
public static String getFilteredStackTrace(final Throwable t) {
return getFilteredStackTrace(t, true);
}
public static String getFilteredStackTrace(final Throwable t,
final boolean shouldFilter) {
try {
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
writeCleanStackTrace(t, pw, shouldFilter);
return sw.getBuffer().toString();
} catch (final Exception e) {
e.printStackTrace();
return e.toString();
}
}
private static void writeCleanStackTrace(final Throwable t,
final PrintWriter s, final boolean wantsFilter) {
s.print("Exception: ");
printExceptionChain(t, s);
final Set<String> skippedPackages = newHashSet();
int skippedLines = 0;
final boolean shouldFilter = wantsFilter && filtersEnabled();
for (final StackTraceElement traceElement : getBottomThrowable(t)
.getStackTrace()) {
String forbiddenPackageName = null;
if (shouldFilter) {
forbiddenPackageName = tryGetForbiddenPackageName(traceElement);
}
if (forbiddenPackageName == null) {
if (skippedPackages.size() > 0) {
// 37 lines skipped for [org.h2, org.hibernate, sun.,
// java.lang.reflect.Method, $Proxy]
s.println(getSkippedPackagesMessage(skippedPackages,
skippedLines));
}
// at hib.HibExample.test(HibExample.java:18)
s.println(INDENT + "at " + traceElement);
skippedPackages.clear();
skippedLines = 0;
} else {
skippedLines++;
skippedPackages.add(forbiddenPackageName);
}
}
if (skippedLines > 0) {
s.println(getSkippedPackagesMessage(skippedPackages, skippedLines));
}
}
// 37 lines skipped for [org.h2, org.hibernate, sun.,
// java.lang.reflect.Method, $Proxy]
private static String getSkippedPackagesMessage(
final Set<String> skippedPackages, final int skippedLines) {
return INDENT + skippedLines + " line" + (skippedLines == 1 ? "" : "s")
+ " skipped for " + skippedPackages;
}
private static Throwable getBottomThrowable(Throwable t) {
while (t.getCause() != null) {
t = t.getCause();
}
return t;
}
/**
* Check configuration to see if filtering is enabled system-wide
*/
private static boolean filtersEnabled() {
return true;
}
private static void printExceptionChain(final Throwable t,
final PrintWriter s) {
s.println(t);
if (t.getCause() != null) {
s.print("Caused by: ");
printExceptionChain(t.getCause(), s);
}
}
/**
* Checks to see if the class is part of a forbidden package. If so, it
* returns the package name from the list of suppressed packages that
* matches, otherwise it returns null.
*/
private static String tryGetForbiddenPackageName(
final StackTraceElement traceElement) {
final String classAndMethod = traceElement.getClassName() + "."
+ traceElement.getMethodName();
for (final String pkg : _suppressedPackages) {
if (classAndMethod.startsWith(pkg)) {
return pkg;
}
}
return null;
}
}