forked from getsentry/sentry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHintUtils.java
More file actions
112 lines (96 loc) · 3.2 KB
/
HintUtils.java
File metadata and controls
112 lines (96 loc) · 3.2 KB
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
package io.sentry.util;
import static io.sentry.TypeCheckHint.SENTRY_TYPE_CHECK_HINT;
import io.sentry.Hint;
import io.sentry.ILogger;
import io.sentry.hints.ApplyScopeData;
import io.sentry.hints.Cached;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** Util class dealing with Hint as not to pollute the Hint API with internal functionality */
@ApiStatus.Internal
public final class HintUtils {
private HintUtils() {}
@ApiStatus.Internal
public static Hint createWithTypeCheckHint(Object typeCheckHint) {
Hint hint = new Hint();
setTypeCheckHint(hint, typeCheckHint);
return hint;
}
@ApiStatus.Internal
public static void setTypeCheckHint(@NotNull Hint hint, Object typeCheckHint) {
hint.set(SENTRY_TYPE_CHECK_HINT, typeCheckHint);
}
@ApiStatus.Internal
public static @Nullable Object getSentrySdkHint(@NotNull Hint hint) {
return hint.get(SENTRY_TYPE_CHECK_HINT);
}
@ApiStatus.Internal
public static boolean hasType(@NotNull Hint hint, @NotNull Class<?> clazz) {
final Object sentrySdkHint = getSentrySdkHint(hint);
return clazz.isInstance(sentrySdkHint);
}
@ApiStatus.Internal
public static <T> void runIfDoesNotHaveType(
@NotNull Hint hint, @NotNull Class<T> clazz, SentryNullableConsumer<Object> lambda) {
runIfHasType(
hint,
clazz,
(ignored) -> {},
(value, clazz2) -> {
lambda.accept(value);
});
}
@ApiStatus.Internal
public static <T> void runIfHasType(
@NotNull Hint hint, @NotNull Class<T> clazz, SentryConsumer<T> lambda) {
runIfHasType(hint, clazz, lambda, (value, clazz2) -> {});
}
@ApiStatus.Internal
public static <T> void runIfHasTypeLogIfNot(
@NotNull Hint hint, @NotNull Class<T> clazz, ILogger logger, SentryConsumer<T> lambda) {
runIfHasType(
hint,
clazz,
lambda,
(sentrySdkHint, expectedClass) -> {
LogUtils.logNotInstanceOf(expectedClass, sentrySdkHint, logger);
});
}
@SuppressWarnings("unchecked")
@ApiStatus.Internal
public static <T> void runIfHasType(
@NotNull Hint hint,
@NotNull Class<T> clazz,
SentryConsumer<T> lambda,
SentryHintFallback fallbackLambda) {
Object sentrySdkHint = getSentrySdkHint(hint);
if (hasType(hint, clazz) && sentrySdkHint != null) {
lambda.accept((T) sentrySdkHint);
} else {
fallbackLambda.accept(sentrySdkHint, clazz);
}
}
/**
* Scope's data should be applied if: Hint is of the type ApplyScopeData or Hint is not Cached
* (this includes a null hint)
*
* @return true if it should apply scope's data or false otherwise
*/
@ApiStatus.Internal
public static boolean shouldApplyScopeData(@NotNull Hint hint) {
return !hasType(hint, Cached.class) || hasType(hint, ApplyScopeData.class);
}
@FunctionalInterface
public interface SentryConsumer<T> {
void accept(@NotNull T t);
}
@FunctionalInterface
public interface SentryNullableConsumer<T> {
void accept(@Nullable T t);
}
@FunctionalInterface
public interface SentryHintFallback {
void accept(@Nullable Object sentrySdkHint, @NotNull Class<?> clazz);
}
}