Skip to content

Commit 834c116

Browse files
authored
Deprecate and disable CollectorLegacy, simplify startup (#10185)
CollectorLegacy shouldn't be needed in any modern browser, though the Error.stack property is still non-standard. This patch leaves it in, so that it is at least possible for projects to restore it if they need it, but won't add an extra step to startup for most GWT applications. With only a single implementation, the static final field isn't needed and likewise doesn't need to be eagerly initialized. This will remove clinits for both Impl and StackTraceCreator everywhere they are used, and by default should still compile out the collector instance into just a set of static functions.
1 parent ec23cfa commit 834c116

File tree

4 files changed

+10
-62
lines changed

4 files changed

+10
-62
lines changed

user/src/com/google/gwt/core/client/impl/Impl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
*/
2929
public final class Impl {
3030

31-
static {
32-
if (GWT.isScript() && StackTraceCreator.collector != null) {
33-
// Just enforces loading of StackTraceCreator early on, nothing else to do here...
34-
}
35-
}
36-
3731
private static final int WATCHDOG_ENTRY_DEPTH_CHECK_INTERVAL_MS = 2000;
3832

3933
/**

user/src/com/google/gwt/core/client/impl/StackTraceCreator.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ abstract static class Collector {
6565
* This legacy {@link Collector} simply crawls <code>arguments.callee.caller</code> for browsers
6666
* that doesn't support {@code Error.stack} property.
6767
*/
68+
@Deprecated
6869
static class CollectorLegacy extends Collector {
6970

7071
@Override
@@ -306,11 +307,11 @@ public StackTraceElement[] getStackTrace(Object ignored) {
306307
* Collect necessary information to construct stack trace trace later in time.
307308
*/
308309
public static void captureStackTrace(Object error) {
309-
collector.collect(error);
310+
getCollector().collect(error);
310311
}
311312

312313
public static StackTraceElement[] constructJavaStackTrace(Throwable thrown) {
313-
StackTraceElement[] stackTrace = collector.getStackTrace(thrown);
314+
StackTraceElement[] stackTrace = getCollector().getStackTrace(thrown);
314315
return dropInternalFrames(stackTrace);
315316
}
316317

@@ -338,26 +339,10 @@ private static <T> void splice(Object[] arr, int length) {
338339
}
339340
}
340341

341-
// Visible for testing
342-
static final Collector collector;
343-
344-
static {
345-
// Ensure old Safari falls back to legacy Collector implementation.
346-
boolean enforceLegacy = !supportsErrorStack();
347-
Collector c = GWT.create(Collector.class);
348-
collector = (c instanceof CollectorModern && enforceLegacy) ? new CollectorLegacy() : c;
342+
static Collector getCollector() {
343+
return GWT.create(Collector.class);
349344
}
350345

351-
private static native boolean supportsErrorStack() /*-{
352-
// Error.stackTraceLimit is cheaper to check and available in both IE and Chrome
353-
if (Error.stackTraceLimit > 0) {
354-
$wnd.Error.stackTraceLimit = Error.stackTraceLimit = 64;
355-
return true;
356-
}
357-
358-
return "stack" in new Error();
359-
}-*/;
360-
361346
private static native JsArrayString getFnStack(Object e) /*-{
362347
return (e && e["fnStack"]) ? e["fnStack"] : [];
363348
}-*/;

user/test/com/google/gwt/core/client/impl/StackTraceEmulTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected String[] getTraceJse(Object thrown) {
3737

3838
@Override
3939
public void testCollectorType() {
40-
assertTrue(StackTraceCreator.collector instanceof StackTraceCreator.CollectorEmulated);
40+
assertTrue(StackTraceCreator.getCollector() instanceof StackTraceCreator.CollectorEmulated);
4141
}
4242

4343
/**

user/test/com/google/gwt/core/client/impl/StackTraceNativeTest.java

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import static com.google.gwt.core.client.impl.StackTraceExamples.TYPE_ERROR;
1919

20-
import com.google.gwt.core.client.impl.StackTraceCreator.CollectorLegacy;
2120
import com.google.gwt.core.client.impl.StackTraceCreator.CollectorModern;
2221
import com.google.gwt.junit.DoNotRunWith;
2322
import com.google.gwt.junit.Platform;
@@ -49,7 +48,7 @@ protected String[] getTraceJava() {
4948
@Override
5049
protected String[] getTraceRecursion() {
5150
// First two frames are optional as they are automatically stripped by EDGE.
52-
final String[] expectedModern = {
51+
return new String[]{
5352
"?" + Impl.getNameOf("@java.lang.Throwable::new(Ljava/lang/String;)"),
5453
"?" + Impl.getNameOf("@java.lang.Exception::new(Ljava/lang/String;)"),
5554
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::throwException2(*)"),
@@ -62,16 +61,6 @@ protected String[] getTraceRecursion() {
6261
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::getLiveException(*)"),
6362
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceTestBase::testTraceRecursion()"),
6463
};
65-
66-
final String[] expectedLegacy = {
67-
Impl.getNameOf("@java.lang.Throwable::new(Ljava/lang/String;)"),
68-
Impl.getNameOf("@java.lang.Exception::new(Ljava/lang/String;)"),
69-
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::throwException2(*)"),
70-
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::throwException1(*)"),
71-
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::throwRecursive(*)"),
72-
};
73-
74-
return isLegacyCollector() ? expectedLegacy : expectedModern;
7564
}
7665

7766
@Override
@@ -87,12 +76,6 @@ protected String[] getTraceJse(Object thrown) {
8776
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceTestBase::assertJse(*)"),
8877
};
8978

90-
final String[] limited_wrap = {
91-
Impl.getNameOf("@com.google.gwt.lang.Exceptions::toJava(*)"),
92-
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::getLiveException(*)"),
93-
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceTestBase::assertJse(*)"),
94-
};
95-
9679
final String[] limited_fillInStackTrace = {
9780
Impl.getNameOf("@java.lang.Throwable::fillInStackTrace()"),
9881
Impl.getNameOf("@com.google.gwt.core.client.impl.StackTraceExamples::getLiveException(*)"),
@@ -101,28 +84,14 @@ protected String[] getTraceJse(Object thrown) {
10184

10285
// For legacy browsers and non-error javascript exceptions (e.g. throw "string"), we can only
10386
// construct stack trace from the catch block and below.
104-
return isLegacyCollector()
105-
? limited_wrap : (thrown != TYPE_ERROR ? limited_fillInStackTrace : full);
87+
return thrown != TYPE_ERROR ? limited_fillInStackTrace : full;
10688
}
10789

10890
public void testCollectorType() {
109-
if (isSafari5()) {
110-
assertTrue(isLegacyCollector());
111-
} else {
112-
assertTrue(isModernCollector());
113-
}
114-
}
115-
116-
private static boolean isLegacyCollector() {
117-
return StackTraceCreator.collector instanceof CollectorLegacy;
91+
assertTrue(isModernCollector());
11892
}
11993

12094
private static boolean isModernCollector() {
121-
return StackTraceCreator.collector instanceof CollectorModern;
95+
return StackTraceCreator.getCollector() instanceof CollectorModern;
12296
}
123-
124-
private static native boolean isSafari5() /*-{
125-
return navigator.userAgent.match(' Safari/') && !navigator.userAgent.match(' Chrom')
126-
&& !!navigator.userAgent.match(' Version/5.');
127-
}-*/;
12897
}

0 commit comments

Comments
 (0)