|
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 |
| package edu.rice.cs.drjava.ui; |
|
38 |
| |
|
39 |
| import javax.swing.JButton; |
|
40 |
| import java.util.ArrayList; |
|
41 |
| import javax.swing.JFrame; |
|
42 |
| |
|
43 |
| import edu.rice.cs.drjava.DrJava; |
|
44 |
| import edu.rice.cs.util.UnexpectedException; |
|
45 |
| import edu.rice.cs.util.swing.Utilities; |
|
46 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
47 |
| |
|
48 |
| import edu.rice.cs.util.Log; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| public class DrJavaErrorHandler implements Thread.UncaughtExceptionHandler { |
|
58 |
| |
|
59 |
| public static final DrJavaErrorHandler INSTANCE = new DrJavaErrorHandler(); |
|
60 |
| |
|
61 |
6
| private DrJavaErrorHandler() { }
|
|
62 |
| |
|
63 |
| |
|
64 |
0
| public void uncaughtException(Thread t, Throwable thrown) {
|
|
65 |
0
| record(thrown);
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
| private static volatile ArrayList<Throwable> _errors = new ArrayList<Throwable>(); |
|
70 |
| |
|
71 |
| |
|
72 |
| private static volatile JButton _errorsButton; |
|
73 |
| |
|
74 |
| |
|
75 |
38
| public static void setButton(JButton b) { _errorsButton = b; }
|
|
76 |
| |
|
77 |
| |
|
78 |
0
| public static JButton getButton() { return _errorsButton; }
|
|
79 |
| |
|
80 |
| |
|
81 |
0
| public static int getErrorCount() { return _errors.size(); }
|
|
82 |
| |
|
83 |
| |
|
84 |
0
| public static Throwable getError(int index) {
|
|
85 |
| |
|
86 |
0
| if (index >= 0 && index < _errors.size()) return _errors.get(index);
|
|
87 |
0
| else return new UnexpectedException("Error in DrJavaErrorHandler");
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
0
| public static void clearErrors() { _errors.clear(); }
|
|
92 |
| |
|
93 |
| |
|
94 |
0
| public static void record(final Throwable thrown) {
|
|
95 |
0
| Utilities.invokeLater(new Runnable() {
|
|
96 |
0
| public void run() {
|
|
97 |
0
| try {
|
|
98 |
0
| if (thrown instanceof OutOfMemoryError) {
|
|
99 |
| |
|
100 |
0
| Runtime.getRuntime().gc();
|
|
101 |
0
| JFrame f = DrJavaErrorWindow.getFrame();
|
|
102 |
0
| if (f instanceof MainFrame) {
|
|
103 |
0
| MainFrame mf = (MainFrame)f;
|
|
104 |
0
| mf.askToIncreaseMasterMaxHeap();
|
|
105 |
| } |
|
106 |
| } |
|
107 |
0
| else if (thrown.toString().startsWith("com.sun.jdi.VMOutOfMemoryException")) {
|
|
108 |
| |
|
109 |
0
| JFrame f = DrJavaErrorWindow.getFrame();
|
|
110 |
0
| if (f instanceof MainFrame) {
|
|
111 |
0
| MainFrame mf = (MainFrame)f;
|
|
112 |
0
| mf.askToIncreaseSlaveMaxHeap();
|
|
113 |
| } |
|
114 |
| } |
|
115 |
0
| else if (isSwingBugArrayIndexOufOfBoundsExceptionInCharWidth(thrown)) {
|
|
116 |
| |
|
117 |
0
| return;
|
|
118 |
| } |
|
119 |
0
| _errors.add(thrown);
|
|
120 |
0
| if (_errorsButton != null) {
|
|
121 |
0
| _errorsButton.setVisible(true);
|
|
122 |
| } |
|
123 |
0
| if (_errors.size() == 1 && ! Utilities.TEST_MODE &&
|
|
124 |
| DrJava.getConfig().getSetting(OptionConstants.DIALOG_DRJAVA_ERROR_POPUP_ENABLED).booleanValue()) { |
|
125 |
0
| DrJavaErrorPopup popup = new DrJavaErrorPopup(DrJavaErrorWindow.getFrame(), thrown);
|
|
126 |
0
| Utilities.setPopupLoc(popup, popup.getOwner());
|
|
127 |
0
| popup.setVisible(true);
|
|
128 |
| } |
|
129 |
| } |
|
130 |
| catch(Throwable t) { } |
|
131 |
| } |
|
132 |
| }); |
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
0
| public static boolean isSwingBugArrayIndexOufOfBoundsExceptionInCharWidth(Throwable thrown) {
|
|
139 |
| |
|
140 |
0
| if (!edu.rice.cs.plt.reflect.JavaVersion.CURRENT_FULL.vendor().
|
|
141 |
0
| equals(edu.rice.cs.plt.reflect.JavaVersion.VendorType.ORACLE)) return false;
|
|
142 |
| |
|
143 |
| |
|
144 |
0
| if (edu.rice.cs.plt.reflect.JavaVersion.parseFullVersion("6.0_18","Sun","Sun").
|
|
145 |
0
| compareTo(edu.rice.cs.plt.reflect.JavaVersion.CURRENT_FULL)<=0) return false;
|
|
146 |
| |
|
147 |
0
| if (!(thrown instanceof ArrayIndexOutOfBoundsException)) return false;
|
|
148 |
| |
|
149 |
0
| StackTraceElement[] stes = new StackTraceElement[] {
|
|
150 |
| new StackTraceElement("sun.font.FontDesignMetrics","charsWidth",null,-1), |
|
151 |
| new StackTraceElement("javax.swing.text.Utilities","getTabbedTextOffset",null,-1), |
|
152 |
| new StackTraceElement("javax.swing.text.Utilities","getTabbedTextOffset",null,-1), |
|
153 |
| new StackTraceElement("javax.swing.text.Utilities","getTabbedTextOffset",null,-1), |
|
154 |
| new StackTraceElement("javax.swing.text.PlainView","viewToModel",null,-1), |
|
155 |
| new StackTraceElement("javax.swing.plaf.basic.BasicTextUI$RootView","viewToModel",null,-1), |
|
156 |
| new StackTraceElement("javax.swing.plaf.basic.BasicTextUI","viewToModel",null,-1) |
|
157 |
| }; |
|
158 |
| |
|
159 |
0
| StackTraceElement[] stesBottom = new StackTraceElement[] {
|
|
160 |
| new StackTraceElement("java.awt.EventQueue","dispatchEvent",null,-1), |
|
161 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpOneEventForFilters",null,-1), |
|
162 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEventsForFilter",null,-1), |
|
163 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEventsForHierarchy",null,-1), |
|
164 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEvents",null,-1), |
|
165 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEvents",null,-1), |
|
166 |
| new StackTraceElement("java.awt.EventDispatchThread","run",null,-1) |
|
167 |
| }; |
|
168 |
| |
|
169 |
0
| StackTraceElement[] tst = thrown.getStackTrace();
|
|
170 |
| |
|
171 |
0
| if (tst.length<stes.length+stesBottom.length) return false;
|
|
172 |
| |
|
173 |
0
| for(int i=0; i<stes.length; ++i) {
|
|
174 |
0
| if (!stes[i].equals(tst[i])) return false;
|
|
175 |
| } |
|
176 |
| |
|
177 |
0
| for(int i=0; i<stesBottom.length; ++i) {
|
|
178 |
0
| if (!stesBottom[stesBottom.length-i-1].equals(tst[tst.length-i-1])) return false;
|
|
179 |
| } |
|
180 |
| |
|
181 |
0
| return true;
|
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
0
| public static void simulateSwingBugArrayIndexOufOfBoundsExceptionInCharWidth() {
|
|
186 |
0
| StackTraceElement[] stes = new StackTraceElement[] {
|
|
187 |
| new StackTraceElement("sun.font.FontDesignMetrics","charsWidth",null,-1), |
|
188 |
| new StackTraceElement("javax.swing.text.Utilities","getTabbedTextOffset",null,-1), |
|
189 |
| new StackTraceElement("javax.swing.text.Utilities","getTabbedTextOffset",null,-1), |
|
190 |
| new StackTraceElement("javax.swing.text.Utilities","getTabbedTextOffset",null,-1), |
|
191 |
| new StackTraceElement("javax.swing.text.PlainView","viewToModel",null,-1), |
|
192 |
| new StackTraceElement("javax.swing.plaf.basic.BasicTextUI$RootView","viewToModel",null,-1), |
|
193 |
| new StackTraceElement("javax.swing.plaf.basic.BasicTextUI","viewToModel",null,-1), |
|
194 |
| |
|
195 |
| new StackTraceElement("foo","bar",null,-1), |
|
196 |
| new StackTraceElement("foo","bar",null,-1), |
|
197 |
| |
|
198 |
| new StackTraceElement("java.awt.EventQueue","dispatchEvent",null,-1), |
|
199 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpOneEventForFilters",null,-1), |
|
200 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEventsForFilter",null,-1), |
|
201 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEventsForHierarchy",null,-1), |
|
202 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEvents",null,-1), |
|
203 |
| new StackTraceElement("java.awt.EventDispatchThread","pumpEvents",null,-1), |
|
204 |
| new StackTraceElement("java.awt.EventDispatchThread","run",null,-1) |
|
205 |
| }; |
|
206 |
0
| ArrayIndexOutOfBoundsException t = new ArrayIndexOutOfBoundsException(63);
|
|
207 |
0
| t.setStackTrace(stes);
|
|
208 |
0
| t.printStackTrace(System.out);
|
|
209 |
0
| throw t;
|
|
210 |
| } |
|
211 |
| |
|
212 |
| |
|
213 |
0
| public static void log(String message) { record(new LoggedCondition(message)); }
|
|
214 |
| |
|
215 |
| |
|
216 |
| public static class LoggedCondition extends Throwable { |
|
217 |
0
| public LoggedCondition(String s) { super(s); }
|
|
218 |
| } |
|
219 |
| } |