aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/com/google/common/truth/StackTraceCleaner.java
blob: 5efc43a0fad4b50d990d5454c45bf4bbfcea0bb1 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Copyright (c) 2017 Google, 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.google.common.truth;

import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.lang.Thread.currentThread;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.Nullable;

/** Utility that cleans stack traces to remove noise from common frameworks. */
@GwtIncompatible
@J2ktIncompatible
final class StackTraceCleaner {

  static final String CLEANER_LINK = "https://goo.gl/aH3UyP";

  /**
   * <b>Call {@link Platform#cleanStackTrace} rather than calling this directly.</b>
   *
   * <p>Cleans the stack trace on the given {@link Throwable}, replacing the original stack trace
   * stored on the instance (see {@link Throwable#setStackTrace(StackTraceElement[])}).
   *
   * <p>Removes Truth stack frames from the top and JUnit framework and reflective call frames from
   * the bottom. Collapses the frames for various frameworks in the middle of the trace as well.
   */
  static void cleanStackTrace(Throwable throwable) {
    new StackTraceCleaner(throwable).clean(Sets.<Throwable>newIdentityHashSet());
  }

  private final Throwable throwable;
  private final List<StackTraceElementWrapper> cleanedStackTrace = new ArrayList<>();
  private @Nullable StackTraceElementWrapper lastStackFrameElementWrapper = null;
  private @Nullable StackFrameType currentStreakType = null;
  private int currentStreakLength = 0;

  /**
   * A new instance is instantiated for each throwable to be cleaned. This is so that helper methods
   * can make use of instance variables describing the state of the cleaning process.
   */
  private StackTraceCleaner(Throwable throwable) {
    this.throwable = throwable;
  }

  // TODO(b/135924708): Add this to the test runners so that we clean all stack traces, not just
  // those of exceptions originating in Truth.
  /** Cleans the stack trace on {@code throwable}, replacing the trace that was originally on it. */
  @SuppressWarnings("SetAll") // not available under old versions of Android
  private void clean(Set<Throwable> seenThrowables) {
    // Stack trace cleaning can be disabled using a system property.
    if (isStackTraceCleaningDisabled()) {
      return;
    }

    /*
     * TODO(cpovirk): Consider wrapping this whole method in a try-catch in case there are any bugs.
     * It would be a shame for us to fail to report the "real" assertion failure because we're
     * instead reporting a bug in Truth's cosmetic stack cleaning.
     */

    // Prevent infinite recursion if there is a reference cycle between Throwables.
    if (seenThrowables.contains(throwable)) {
      return;
    }
    seenThrowables.add(throwable);

    StackTraceElement[] stackFrames = throwable.getStackTrace();

    int stackIndex = stackFrames.length - 1;
    for (; stackIndex >= 0 && !isTruthEntrance(stackFrames[stackIndex]); stackIndex--) {
      // Find first frame that enters Truth's world, and remove all above.
    }
    stackIndex += 1;

    int endIndex = 0;
    for (;
        endIndex < stackFrames.length && !isJUnitIntrastructure(stackFrames[endIndex]);
        endIndex++) {
      // Find last frame of setup frames, and remove from there down.
    }
    /*
     * If the stack trace would be empty, the error was probably thrown from "JUnit infrastructure"
     * frames. Keep those frames around (though much of JUnit itself and related startup frames will
     * still be removed by the remainder of this method) so that the user sees a useful stack.
     */
    if (stackIndex >= endIndex) {
      endIndex = stackFrames.length;
    }

    for (; stackIndex < endIndex; stackIndex++) {
      StackTraceElementWrapper stackTraceElementWrapper =
          new StackTraceElementWrapper(stackFrames[stackIndex]);
      // Always keep frames that might be useful.
      if (stackTraceElementWrapper.getStackFrameType() == StackFrameType.NEVER_REMOVE) {
        endStreak();
        cleanedStackTrace.add(stackTraceElementWrapper);
        continue;
      }

      // Otherwise, process the current frame for collapsing
      addToStreak(stackTraceElementWrapper);

      lastStackFrameElementWrapper = stackTraceElementWrapper;
    }

    // Close out the streak on the bottom of the stack.
    endStreak();

    // Filter out testing framework and reflective calls from the bottom of the stack
    ListIterator<StackTraceElementWrapper> iterator =
        cleanedStackTrace.listIterator(cleanedStackTrace.size());
    while (iterator.hasPrevious()) {
      StackTraceElementWrapper stackTraceElementWrapper = iterator.previous();
      if (stackTraceElementWrapper.getStackFrameType() == StackFrameType.TEST_FRAMEWORK
          || stackTraceElementWrapper.getStackFrameType() == StackFrameType.REFLECTION) {
        iterator.remove();
      } else {
        break;
      }
    }

    // Replace the stack trace on the Throwable with the cleaned one.
    StackTraceElement[] result = new StackTraceElement[cleanedStackTrace.size()];
    for (int i = 0; i < result.length; i++) {
      result[i] = cleanedStackTrace.get(i).getStackTraceElement();
    }
    throwable.setStackTrace(result);

    // Recurse on any related Throwables that are attached to this one
    if (throwable.getCause() != null) {
      new StackTraceCleaner(throwable.getCause()).clean(seenThrowables);
    }
    for (Throwable suppressed : Platform.getSuppressed(throwable)) {
      new StackTraceCleaner(suppressed).clean(seenThrowables);
    }
  }

  /**
   * Either adds the given frame to the running streak or closes out the running streak and starts a
   * new one.
   */
  private void addToStreak(StackTraceElementWrapper stackTraceElementWrapper) {
    if (stackTraceElementWrapper.getStackFrameType() != currentStreakType) {
      endStreak();
      currentStreakType = stackTraceElementWrapper.getStackFrameType();
      currentStreakLength = 1;
    } else {
      currentStreakLength++;
    }
  }

  /** Ends the current streak, adding a summary frame to the result. Resets the streak counter. */
  private void endStreak() {
    if (currentStreakLength == 0) {
      return;
    }

    if (currentStreakLength == 1) {
      // A single frame isn't a streak. Just include the frame as-is in the result.
      cleanedStackTrace.add(checkNotNull(lastStackFrameElementWrapper));
    } else {
      // Add a single frame to the result summarizing the streak of framework frames
      cleanedStackTrace.add(
          createStreakReplacementFrame(checkNotNull(currentStreakType), currentStreakLength));
    }

    clearStreak();
  }

  /** Resets the streak counter. */
  private void clearStreak() {
    currentStreakType = null;
    currentStreakLength = 0;
  }

  private static final ImmutableSet<String> SUBJECT_CLASS =
      ImmutableSet.of(
          Subject.class.getCanonicalName());

  private static final ImmutableSet<String> STANDARD_SUBJECT_BUILDER_CLASS =
      ImmutableSet.of(StandardSubjectBuilder.class.getCanonicalName());

  private static boolean isTruthEntrance(StackTraceElement stackTraceElement) {
    return isFromClassOrClassNestedInside(stackTraceElement, SUBJECT_CLASS)
        /*
         * Don't match classes _nested inside_ StandardSubjectBuilder because that would match
         * Expect's Statement implementation. While we want to strip everything from there _down_, we
         * don't want to strip everything from there _up_ (which would strip the test class itself!).
         *
         * (StandardSubjectBuilder is listed here only for its fail() methods, anyway, so we don't
         * have to worry about nested classes like we do with Subject.)
         */
        || isFromClassDirectly(stackTraceElement, STANDARD_SUBJECT_BUILDER_CLASS);
  }

  private static final ImmutableSet<String> JUNIT_INFRASTRUCTURE_CLASSES =
      ImmutableSet.of("org.junit.runner.Runner", "org.junit.runners.model.Statement");

  private static boolean isJUnitIntrastructure(StackTraceElement stackTraceElement) {
    // It's not clear whether looking at nested classes here is useful, harmful, or neutral.
    return isFromClassOrClassNestedInside(stackTraceElement, JUNIT_INFRASTRUCTURE_CLASSES);
  }

  private static boolean isFromClassOrClassNestedInside(
      StackTraceElement stackTraceElement, ImmutableSet<String> recognizedClasses) {
    Class<?> stackClass;
    try {
      stackClass = loadClass(stackTraceElement.getClassName());
    } catch (ClassNotFoundException e) {
      return false;
    }
    try {
      for (; stackClass != null; stackClass = stackClass.getEnclosingClass()) {
        for (String recognizedClass : recognizedClasses) {
          if (isSubtypeOf(stackClass, recognizedClass)) {
            return true;
          }
        }
      }
    } catch (Error e) {
      if (e.getClass().getName().equals("com.google.j2objc.ReflectionStrippedError")) {
        /*
         * We're running under j2objc without reflection. Skip testing the enclosing classes. At
         * least we tested the class itself against all the recognized classes.
         *
         * TODO(cpovirk): The smarter thing might be to guess the name of the enclosing classes by
         * removing "$Foo" from the end of the name. But this should be good enough for now.
         */
        return false;
      }
      if (e instanceof IncompatibleClassChangeError) {
        // OEM class-loading bug? https://issuetracker.google.com/issues/37045084
        return false;
      }
      throw e;
    }
    return false;
  }

  private static boolean isSubtypeOf(@Nullable Class<?> subclass, String superclass) {
    for (; subclass != null; subclass = checkNotNull(subclass).getSuperclass()) {
      if (subclass.getCanonicalName() != null && subclass.getCanonicalName().equals(superclass)) {
        return true;
      }
    }
    return false;
  }

  private static boolean isFromClassDirectly(
      StackTraceElement stackTraceElement, ImmutableSet<String> recognizedClasses) {
    Class<?> stackClass;
    try {
      stackClass = loadClass(stackTraceElement.getClassName());
    } catch (ClassNotFoundException e) {
      return false;
    }
    for (String recognizedClass : recognizedClasses) {
      if (isSubtypeOf(stackClass, recognizedClass)) {
        return true;
      }
    }
    return false;
  }

  // Using plain Class.forName can cause problems.
  /*
   * TODO(cpovirk): Consider avoiding classloading entirely by reading classes with ASM. But that
   * won't work on Android, so we might ultimately need classloading as a fallback. Another
   * possibility is to load classes in a fresh, isolated classloader. However, that requires
   * creating a list of jars to load from, which is fragile and would also require special handling
   * under Android. If we're lucky, this new solution will just work: The classes should already be
   * loaded, anyway, since they appear on the stack, so we just have to hope that we have the right
   * classloader.
   */
  private static Class<?> loadClass(String name) throws ClassNotFoundException {
    ClassLoader loader =
        firstNonNull(
            currentThread().getContextClassLoader(), StackTraceCleaner.class.getClassLoader());
    return loader.loadClass(name);
  }

  /**
   * Wrapper around a {@link StackTraceElement} for calculating and holding the metadata used to
   * clean the stack trace.
   */
  private static class StackTraceElementWrapper {

    private final StackTraceElement stackTraceElement;
    private final StackFrameType stackFrameType;

    /** Creates a wrapper with the given frame with frame type inferred from frame's class name. */
    StackTraceElementWrapper(StackTraceElement stackTraceElement) {
      this(stackTraceElement, StackFrameType.forClassName(stackTraceElement.getClassName()));
    }

    /** Creates a wrapper with the given frame and the given frame type. */
    StackTraceElementWrapper(StackTraceElement stackTraceElement, StackFrameType stackFrameType) {
      this.stackTraceElement = stackTraceElement;
      this.stackFrameType = stackFrameType;
    }

    /** Returns the type of this frame. */
    StackFrameType getStackFrameType() {
      return stackFrameType;
    }

    /** Returns the wrapped {@link StackTraceElement}. */
    StackTraceElement getStackTraceElement() {
      return stackTraceElement;
    }
  }

  private static StackTraceElementWrapper createStreakReplacementFrame(
      StackFrameType stackFrameType, int length) {
    return new StackTraceElementWrapper(
        new StackTraceElement(
            "[["
                + stackFrameType.getName()
                + ": "
                + length
                + " frames collapsed ("
                + CLEANER_LINK
                + ")]]",
            "",
            "",
            0),
        stackFrameType);
  }

  /**
   * Enum of the package or class-name based categories of stack frames that might be removed or
   * collapsed by the cleaner.
   */
  private enum StackFrameType {
    NEVER_REMOVE("N/A"),
    TEST_FRAMEWORK(
        "Testing framework",
        "junit",
        "org.junit",
        "androidx.test.internal.runner",
        "com.github.bazel_contrib.contrib_rules_jvm.junit5",
        "com.google.testing.junit",
        "com.google.testing.testsize",
        "com.google.testing.util"),
    REFLECTION("Reflective call", "java.lang.reflect", "jdk.internal.reflect", "sun.reflect"),
    CONCURRENT_FRAMEWORK(
        "Concurrent framework",
        "com.google.tracing.CurrentContext",
        "com.google.common.util.concurrent",
        "java.util.concurrent.ForkJoin");

    /** Helper method to determine the frame type from the fully qualified class name. */
    private static StackFrameType forClassName(String fullyQualifiedClassName) {
      // Never remove the frames from a test class. These will probably be the frame of a failing
      // assertion.
      // TODO(cpovirk): This is really only for tests in Truth itself, so this doesn't matter yet,
      // but.... If the Truth tests someday start calling into nested classes, we may want to add:
      // || fullyQualifiedClassName.contains("Test$")
      if (fullyQualifiedClassName.endsWith("Test")
          && !fullyQualifiedClassName.equals(
              "androidx.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest")) {
        return StackFrameType.NEVER_REMOVE;
      }

      for (StackFrameType stackFrameType : StackFrameType.values()) {
        if (stackFrameType.belongsToType(fullyQualifiedClassName)) {
          return stackFrameType;
        }
      }

      return StackFrameType.NEVER_REMOVE;
    }

    private final String name;
    private final ImmutableList<String> prefixes;

    /**
     * Each type of stack frame has a name of the summary displayed in the cleaned trace.
     *
     * <p>Most also have a set of fully qualified class name prefixes that identify when a frame
     * belongs to this type.
     */
    StackFrameType(String name, String... prefixes) {
      this.name = name;
      this.prefixes = ImmutableList.copyOf(prefixes);
    }

    /** Returns the name of this frame type to display in the cleaned trace */
    String getName() {
      return name;
    }

    /**
     * Returns true if the given frame belongs to this frame type based on the package and/or class
     * name of the frame.
     */
    boolean belongsToType(String fullyQualifiedClassName) {
      for (String prefix : prefixes) {
        // TODO(cpovirk): Should we also check prefix + "$"?
        if (fullyQualifiedClassName.equals(prefix)
            || fullyQualifiedClassName.startsWith(prefix + ".")) {
          return true;
        }
      }
      return false;
    }
  }

  /**
   * Returns true if stack trace cleaning is explicitly disabled in a system property. This switch
   * is intended to be used when attempting to debug the frameworks which are collapsed or filtered
   * out of stack traces by the cleaner.
   */
  private static boolean isStackTraceCleaningDisabled() {
    // Reading system properties might be forbidden.
    try {
      return Boolean.parseBoolean(
          System.getProperty("com.google.common.truth.disable_stack_trace_cleaning"));
    } catch (SecurityException e) {
      // Hope for the best.
      return false;
      // TODO(cpovirk): Log a warning? Or is that likely to trigger other violations?
    }
  }
}