• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

skuzzle / snapshot-tests / 1

12 Feb 2025 03:38PM UTC coverage: 86.61% (+0.03%) from 86.577%
1

Pull #109

jenkins

skuzzle
Remove old file after merge
Pull Request #109: 2.0 dev

1947 of 2248 relevant lines covered (86.61%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.77
/../snapshot-tests-common/src/main/java/de/skuzzle/test/snapshots/reflection/StackTraces.java
1
package de.skuzzle.test.snapshots.reflection;
2

3
import java.util.Arrays;
4
import java.util.function.Predicate;
5

6
import de.skuzzle.test.snapshots.validation.Arguments;
7

8
import org.apiguardian.api.API;
9
import org.apiguardian.api.API.Status;
10

11
/**
12
 * Utility for working with stacktraces.
13
 *
14
 * @author Simon Taddiken
15
 * @since 1.9.0
16
 */
17
@API(status = Status.INTERNAL, since = "1.9.0")
18
public final class StackTraces {
×
19

20
    /**
21
     * Deeply cleans the stack trace by removing all {@link StackTraceElement elements}
22
     * which are matched by the provided predicate.
23
     * <p>
24
     * This method recurses into both all causes and all suppressed exceptions of the
25
     * provided root exception.
26
     * <p>
27
     * It is allowed to pass null as throwable. In this case, nothing happens.
28
     *
29
     * @param throwable The throwable for which to clean the stack trace. Can be null.
30
     * @param elementsToRemove A predicate matching the elements to remove.
31
     * @since 1.9.0
32
     */
33
    public static void filterStackTrace(Throwable throwable, Predicate<StackTraceElement> elementsToRemove) {
34
        if (throwable == null) {
1✔
35
            return;
1✔
36
        }
37
        final StackTraceElement[] original = throwable.getStackTrace();
1✔
38
        if (original != null) {
1✔
39
            final StackTraceElement[] filtered = Arrays.stream(original)
1✔
40
                    .filter(elementsToRemove.negate())
1✔
41
                    .toArray(StackTraceElement[]::new);
1✔
42
            throwable.setStackTrace(filtered);
1✔
43
        }
44

45
        filterStackTrace(throwable.getCause(), elementsToRemove);
1✔
46
        for (final Throwable suppressed : throwable.getSuppressed()) {
1✔
47
            filterStackTrace(suppressed, elementsToRemove);
×
48
        }
49
    }
1✔
50

51
    /**
52
     * Finds the immediate caller of the method which calls this method. Useful for
53
     * logging purposes.
54
     *
55
     * @return An object describing the caller.
56
     * @since 1.9.0
57
     */
58
    public static Caller findImmediateCaller() {
59
        // Example stack trace
60
        // @formatter:off
61
        // 0: java.base/java.lang.Thread.getStackTrace(Thread.java:1602)
62
        // 1: de.skuzzle.test.snapshots.reflection.StackTraces.findImmediateCaller(StackTraces.java:12)
63
        // 2: de.skuzzle.test.snapshots.reflection.StackTracesTest.methodThatWantsToKnowItsCaller(StackTracesTest.java:16)
64
        // 3: de.skuzzle.test.snapshots.reflection.StackTracesTest.testGetCaller(StackTracesTest.java:11) <-- Element we are interested in
65
        // ...
66
        // @formatter:on
67
        final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
1✔
68
        if (stackTrace.length < 4) {
1✔
69
            return Caller.unknown();
×
70
        }
71
        return Caller.of(stackTrace[3]);
1✔
72
    }
73

74
    /**
75
     * Represents the call site of a method in which
76
     * {@link StackTraces#findImmediateCaller()} has been invoked.
77
     *
78
     * @author Simon Taddiken
79
     */
80
    @API(status = Status.INTERNAL, since = "1.9.0")
81
    public static final class Caller {
82
        private static final Caller UNKNOWN = new Caller(null);
1✔
83

84
        private final StackTraceElement callerElement;
85

86
        private Caller(StackTraceElement callerElement) {
1✔
87
            this.callerElement = callerElement;
1✔
88
        }
1✔
89

90
        public static Caller unknown() {
91
            return UNKNOWN;
×
92
        }
93

94
        public static Caller of(StackTraceElement callerElement) {
95
            return new Caller(Arguments.requireNonNull(callerElement));
1✔
96
        }
97

98
        @Override
99
        public String toString() {
100
            if (callerElement == null) {
1✔
101
                return "unknown";
×
102
            }
103
            return callerElement.toString();
1✔
104
        }
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc