|
6 | 6 | * published by the Free Software Foundation. Codename One designates this |
7 | 7 | * particular file as subject to the "Classpath" exception as provided |
8 | 8 | * by Oracle in the LICENSE file that accompanied this code. |
9 | | - * |
| 9 | + * |
10 | 10 | * This code is distributed in the hope that it will be useful, but WITHOUT |
11 | 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
12 | 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
13 | 13 | * version 2 for more details (a copy is included in the LICENSE file that |
14 | 14 | * accompanied this code). |
15 | | - * |
| 15 | + * |
16 | 16 | * You should have received a copy of the GNU General Public License version |
17 | 17 | * 2 along with this work; if not, write to the Free Software Foundation, |
18 | 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
19 | | - * |
20 | | - * Please contact Codename One through http://www.codenameone.com/ if you |
| 19 | + * |
| 20 | + * Please contact Codename One through http://www.codenameone.com/ if you |
21 | 21 | * need additional information or have any questions. |
22 | 22 | */ |
23 | 23 | package com.codename1.impl; |
24 | 24 |
|
25 | 25 | import com.codename1.io.Log; |
26 | | -import com.codename1.io.Preferences; |
27 | 26 | import com.codename1.io.Util; |
28 | 27 | import com.codename1.system.CrashReport; |
29 | 28 | import com.codename1.ui.Display; |
|
40 | 39 | * @author Shai Almog |
41 | 40 | */ |
42 | 41 | public class CodenameOneThread extends Thread { |
43 | | - private int[] stack = new int[500]; |
44 | | - private int stackPointer; |
45 | | - private Runnable r; |
46 | | - private static Class CODE = CodenameOneThread.class; |
47 | | - private Hashtable exceptionStack = new Hashtable(); |
48 | | - public static int STACK_FRAME_SIZE = 65536; |
49 | | - |
50 | | - /** |
51 | | - * Constructor accepting the runnable |
52 | | - */ |
53 | | - public CodenameOneThread(final Runnable r, String threadName) { |
54 | | - super(Thread.currentThread().getThreadGroup(), new Runnable() { |
55 | | - /** |
56 | | - * Catches exception |
57 | | - */ |
58 | | - public void run() { |
59 | | - try { |
60 | | - r.run(); |
61 | | - } catch(Throwable err) { |
62 | | - err.printStackTrace(); |
63 | | - handleException(err); |
64 | | - } |
| 42 | + private int[] stack = new int[500]; |
| 43 | + private int stackPointer; |
| 44 | + private Runnable r; |
| 45 | + private static Class CODE = CodenameOneThread.class; |
| 46 | + private Hashtable exceptionStack = new Hashtable(); |
| 47 | + public static int STACK_FRAME_SIZE = 65536; |
| 48 | + |
| 49 | + /** Constructor accepting the runnable */ |
| 50 | + public CodenameOneThread(final Runnable r, String threadName) { |
| 51 | + super( |
| 52 | + Thread.currentThread().getThreadGroup(), |
| 53 | + new Runnable() { |
| 54 | + /** Catches exception */ |
| 55 | + public void run() { |
| 56 | + try { |
| 57 | + r.run(); |
| 58 | + } catch (Throwable err) { |
| 59 | + err.printStackTrace(); |
| 60 | + handleException(err); |
65 | 61 | } |
| 62 | + } |
| 63 | + }, |
| 64 | + threadName, |
| 65 | + STACK_FRAME_SIZE); |
| 66 | + this.r = r; |
| 67 | + } |
66 | 68 |
|
67 | | - }, threadName, STACK_FRAME_SIZE); |
68 | | - this.r = r; |
69 | | - } |
70 | | - |
71 | | - public boolean hasStackFrame() { |
72 | | - return stackPointer > 0; |
73 | | - } |
74 | | - |
75 | | - /** |
76 | | - * Pushes a method id into the stack |
77 | | - * @param method the method id |
78 | | - */ |
79 | | - public void pushStack(int method) { |
80 | | - stack[stackPointer] = method; |
81 | | - stackPointer++; |
82 | | - } |
83 | | - |
84 | | - /** |
85 | | - * Pops the method stack pointer |
86 | | - */ |
87 | | - public void popStack() { |
88 | | - stackPointer--; |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Pushes the method to the current thread stack |
93 | | - * |
94 | | - * @param method the method id |
95 | | - */ |
96 | | - public static void push(int method) { |
97 | | - Thread t = Thread.currentThread(); |
98 | | - if(t.getClass() == CODE) { |
99 | | - CodenameOneThread c = (CodenameOneThread)t; |
100 | | - c.pushStack(method); |
101 | | - } |
| 69 | + public boolean hasStackFrame() { |
| 70 | + return stackPointer > 0; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Pushes a method id into the stack |
| 75 | + * |
| 76 | + * @param method the method id |
| 77 | + */ |
| 78 | + public void pushStack(int method) { |
| 79 | + stack[stackPointer] = method; |
| 80 | + stackPointer++; |
| 81 | + } |
| 82 | + |
| 83 | + /** Pops the method stack pointer */ |
| 84 | + public void popStack() { |
| 85 | + stackPointer--; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Pushes the method to the current thread stack |
| 90 | + * |
| 91 | + * @param method the method id |
| 92 | + */ |
| 93 | + public static void push(int method) { |
| 94 | + Thread t = Thread.currentThread(); |
| 95 | + if (t.getClass() == CODE) { |
| 96 | + CodenameOneThread c = (CodenameOneThread) t; |
| 97 | + c.pushStack(method); |
102 | 98 | } |
| 99 | + } |
103 | 100 |
|
104 | | - /** |
105 | | - * Pops the current method from the stack |
106 | | - */ |
107 | | - public static void pop() { |
108 | | - Thread t = Thread.currentThread(); |
109 | | - if(t.getClass() == CODE) { |
110 | | - CodenameOneThread c = (CodenameOneThread)t; |
111 | | - c.popStack(); |
112 | | - } |
| 101 | + /** Pops the current method from the stack */ |
| 102 | + public static void pop() { |
| 103 | + Thread t = Thread.currentThread(); |
| 104 | + if (t.getClass() == CODE) { |
| 105 | + CodenameOneThread c = (CodenameOneThread) t; |
| 106 | + c.popStack(); |
113 | 107 | } |
114 | | - |
115 | | - |
116 | | - /** |
117 | | - * Stores the stack for the given exception |
118 | | - * @param t the exception mapping to the given stack |
119 | | - */ |
120 | | - public void storeStackForException(Throwable t, int currentStackFrame) { |
121 | | - if(!exceptionStack.containsKey(t)) { |
122 | | - int[] s = new int[stackPointer + 1]; |
123 | | - System.arraycopy(stack, 0, s, 0, stackPointer); |
124 | | - s[stackPointer] = currentStackFrame; |
125 | | - exceptionStack.put(t, s); |
126 | | - } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Stores the stack for the given exception |
| 112 | + * |
| 113 | + * @param t the exception mapping to the given stack |
| 114 | + */ |
| 115 | + public void storeStackForException(Throwable t, int currentStackFrame) { |
| 116 | + if (!exceptionStack.containsKey(t)) { |
| 117 | + int[] s = new int[stackPointer + 1]; |
| 118 | + System.arraycopy(stack, 0, s, 0, stackPointer); |
| 119 | + s[stackPointer] = currentStackFrame; |
| 120 | + exceptionStack.put(t, s); |
127 | 121 | } |
128 | | - |
129 | | - /** |
130 | | - * Stores the stack for the given exception |
131 | | - * @param th the exception mapping to the given stack |
132 | | - */ |
133 | | - public static void storeStack(Throwable th, int currentStackFrame) { |
134 | | - Thread t = Thread.currentThread(); |
135 | | - if(t.getClass() == CODE) { |
136 | | - CodenameOneThread c = (CodenameOneThread)t; |
137 | | - c.storeStackForException(th, currentStackFrame); |
138 | | - } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Stores the stack for the given exception |
| 126 | + * |
| 127 | + * @param th the exception mapping to the given stack |
| 128 | + */ |
| 129 | + public static void storeStack(Throwable th, int currentStackFrame) { |
| 130 | + Thread t = Thread.currentThread(); |
| 131 | + if (t.getClass() == CODE) { |
| 132 | + CodenameOneThread c = (CodenameOneThread) t; |
| 133 | + c.storeStackForException(th, currentStackFrame); |
139 | 134 | } |
140 | | - |
141 | | - /** |
142 | | - * Prints the stack trace matching the given stack |
143 | | - */ |
144 | | - public String getStack(Throwable t) { |
145 | | - try { |
146 | | - StringBuffer b = new StringBuffer(); |
147 | | - int size; |
148 | | - int[] s = (int[])exceptionStack.get(t); |
149 | | - if(s == null) { |
150 | | - s = stack; |
151 | | - size = stackPointer; |
152 | | - } else { |
153 | | - size = s.length; |
154 | | - } |
155 | | - String[] stk = new String[size]; |
156 | | - |
157 | | - InputStream inp = Display.getInstance().getResourceAsStream(getClass(), "/methodData.dat"); |
158 | | - if(inp == null) { |
159 | | - StringWriter sw = new StringWriter(); |
160 | | - PrintWriter pw = new PrintWriter(sw); |
161 | | - t.printStackTrace(pw); |
162 | | - String str = sw.toString(); |
163 | | - Util.cleanup(sw); |
164 | | - return str; |
165 | | - } |
166 | | - DataInputStream di = new DataInputStream(inp); |
167 | | - int totalAmount = di.readInt(); |
168 | | - String lastClass = ""; |
169 | | - for(int x = 0 ; x < totalAmount ; x++) { |
170 | | - String current = di.readUTF(); |
171 | | - if(current.indexOf('.') > -1) { |
172 | | - lastClass = current; |
173 | | - } else { |
174 | | - for(int iter = 0 ; iter < size ; iter++) { |
175 | | - if(s[iter] == x + 1) { |
176 | | - stk[iter] = lastClass + "." + current; |
177 | | - } |
178 | | - } |
179 | | - } |
180 | | - } |
181 | | - for(int iter = size - 1 ; iter >= 0 ; iter--) { |
182 | | - b.append("at "); |
183 | | - b.append(stk[iter]); |
184 | | - b.append("\n"); |
| 135 | + } |
| 136 | + |
| 137 | + /** Prints the stack trace matching the given stack */ |
| 138 | + public String getStack(Throwable t) { |
| 139 | + try { |
| 140 | + StringBuffer b = new StringBuffer(); |
| 141 | + int size; |
| 142 | + int[] s = (int[]) exceptionStack.get(t); |
| 143 | + if (s == null) { |
| 144 | + s = stack; |
| 145 | + size = stackPointer; |
| 146 | + } else { |
| 147 | + size = s.length; |
| 148 | + } |
| 149 | + String[] stk = new String[size]; |
| 150 | + |
| 151 | + InputStream inp = Display.getInstance().getResourceAsStream(getClass(), "/methodData.dat"); |
| 152 | + if (inp == null) { |
| 153 | + StringWriter sw = new StringWriter(); |
| 154 | + PrintWriter pw = new PrintWriter(sw); |
| 155 | + t.printStackTrace(pw); |
| 156 | + String str = sw.toString(); |
| 157 | + Util.cleanup(sw); |
| 158 | + return str; |
| 159 | + } |
| 160 | + DataInputStream di = new DataInputStream(inp); |
| 161 | + int totalAmount = di.readInt(); |
| 162 | + String lastClass = ""; |
| 163 | + for (int x = 0; x < totalAmount; x++) { |
| 164 | + String current = di.readUTF(); |
| 165 | + if (current.indexOf('.') > -1) { |
| 166 | + lastClass = current; |
| 167 | + } else { |
| 168 | + for (int iter = 0; iter < size; iter++) { |
| 169 | + if (s[iter] == x + 1) { |
| 170 | + stk[iter] = lastClass + "." + current; |
185 | 171 | } |
186 | | - return b.toString(); |
187 | | - } catch (IOException ex) { |
188 | | - ex.printStackTrace(); |
| 172 | + } |
189 | 173 | } |
190 | | - return "Failed in stack generation for " + t; |
| 174 | + } |
| 175 | + for (int iter = size - 1; iter >= 0; iter--) { |
| 176 | + b.append("at "); |
| 177 | + b.append(stk[iter]); |
| 178 | + b.append("\n"); |
| 179 | + } |
| 180 | + return b.toString(); |
| 181 | + } catch (IOException ex) { |
| 182 | + ex.printStackTrace(); |
191 | 183 | } |
192 | | - |
193 | | - public static void handleException(Throwable err) { |
194 | | - Thread t = Thread.currentThread(); |
195 | | - if(t instanceof CodenameOneThread) { |
196 | | - Log.p(err.toString()); |
197 | | - Log.p(((CodenameOneThread)t).getStack(err)); |
198 | | - //Preferences.set("$CN1Uncaught", true); |
199 | | - CrashReport r = Display.getInstance().getCrashReporter(); |
200 | | - if(r != null) { |
201 | | - r.exception(err); |
202 | | - } |
203 | | - } |
| 184 | + return "Failed in stack generation for " + t; |
| 185 | + } |
| 186 | + |
| 187 | + public static void handleException(Throwable err) { |
| 188 | + Thread t = Thread.currentThread(); |
| 189 | + if (t instanceof CodenameOneThread) { |
| 190 | + Log.p(err.toString()); |
| 191 | + Log.p(((CodenameOneThread) t).getStack(err)); |
| 192 | + // Preferences.set("$CN1Uncaught", true); |
| 193 | + CrashReport r = Display.getInstance().getCrashReporter(); |
| 194 | + if (r != null) { |
| 195 | + r.exception(err); |
| 196 | + } |
204 | 197 | } |
| 198 | + } |
205 | 199 | } |
0 commit comments