forked from SquareSquash/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquashBacktrace.java
More file actions
142 lines (128 loc) · 4.93 KB
/
SquashBacktrace.java
File metadata and controls
142 lines (128 loc) · 4.93 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2012 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.squareup.squash;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/** Creates the Squash stacktrace format for serialization by gson. */
public final class SquashBacktrace {
private SquashBacktrace() {
// Should not be instantiated: this is a utility class.
}
public static List<SquashException> getBacktraces(Throwable error) {
if (error == null) {
return null;
}
final List<SquashException> threadList = new ArrayList<SquashException>();
final SquashException currentThread =
new SquashException(Thread.currentThread().getName(), true, getStacktraceArray(error));
threadList.add(currentThread);
return threadList;
}
private static List<StackElement> getStacktraceArray(Throwable error) {
List<StackElement> stackElems = new ArrayList<StackElement>();
for (StackTraceElement element : error.getStackTrace()) {
StackElement elementList =
new StackElement(element.getClassName(), element.getFileName(), element.getLineNumber(),
element.getMethodName());
stackElems.add(elementList);
}
return stackElems;
}
public static Map<String, Object> getIvars(Throwable error) {
if (error == null) {
return null;
}
Map<String, Object> ivars = new HashMap<String, Object>();
final Field[] fields = error.getClass().getDeclaredFields();
for (Field field : fields) {
try {
if (!Modifier.isStatic(field.getModifiers()) // Ignore static fields.
&& !field.getName().startsWith("CGLIB")) { // Ignore mockito stuff in tests.
if (!field.isAccessible()) {
field.setAccessible(true);
}
Object val = field.get(error);
ivars.put(field.getName(), val);
}
} catch (IllegalAccessException e) {
ivars.put(field.getName(), "Exception accessing field: " + e);
}
}
return ivars;
}
/**
* Recursive method that follows the "cause" exceptions all the way down the stack, adding them to
* the passed-in list.
*/
public static void populateNestedExceptions(List<NestedException> nestedExceptions,
Throwable error) {
// Only keep processing if the "cause" exception is set and != the "parent" exception.
if (error == null || error.getCause() == null || error.getCause() == error) {
return;
}
final Throwable cause = error.getCause();
NestedException doc =
new NestedException(cause.getClass().getName(), cause.getMessage(), getBacktraces(cause),
getIvars(cause));
nestedExceptions.add(doc);
// Exceptions all the way down!
populateNestedExceptions(nestedExceptions, cause);
}
/** Wrapper object for top-level exceptions. */
static final class SquashException {
final String name;
final boolean faulted;
final List<StackElement> backtrace;
public SquashException(String name, boolean faulted, List<StackElement> backtrace) {
this.backtrace = backtrace;
this.name = name;
this.faulted = faulted;
}
}
/** Wrapper object for nested exceptions. */
static final class NestedException {
final String class_name;
final String message;
final List<SquashException> backtraces;
final Map<String, Object> ivars;
public NestedException(String className, String message, List<SquashException> backtraces,
Map<String, Object> ivars) {
this.class_name = className;
this.message = message;
this.backtraces = backtraces;
this.ivars = ivars;
}
}
/** Wrapper object for a stacktrace entry. */
static final class StackElement {
// This field is necessary so Squash knows that this is a java stacktrace that might need
// obfuscation lookup and git filename lookup. Our stacktrace elements don't give us the full
// path to the java file, so Squash has to do a SCM lookup to try and do its cause analysis.
@SuppressWarnings("UnusedDeclaration") final String type = "obfuscated";
final String file;
final int line;
final String symbol;
final String class_name;
private StackElement(String className, String file, int line, String methodName) {
this.class_name = className;
this.file = file;
this.line = line;
this.symbol = methodName;
}
}
}