diff --git a/sentry-core/src/main/java/io/sentry/core/Scope.java b/sentry-core/src/main/java/io/sentry/core/Scope.java index 288491fd4..5c9d4c98f 100644 --- a/sentry-core/src/main/java/io/sentry/core/Scope.java +++ b/sentry-core/src/main/java/io/sentry/core/Scope.java @@ -1,8 +1,8 @@ package io.sentry.core; +import io.sentry.core.protocol.Contexts; import io.sentry.core.protocol.User; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -21,6 +21,8 @@ public final class Scope implements Cloneable { private @NotNull Map tags = new ConcurrentHashMap<>(); private @NotNull Map extra = new ConcurrentHashMap<>(); private @NotNull List eventProcessors = new CopyOnWriteArrayList<>(); + private Contexts contexts = new Contexts(); + private final @NotNull SentryOptions options; public Scope(final @NotNull SentryOptions options) { @@ -146,6 +148,14 @@ public void removeExtra(@NotNull String key) { this.extra.remove(key); } + public @NotNull Contexts getContexts() { + return contexts; + } + + public void setContexts(@NotNull Contexts contexts) { + this.contexts = contexts; + } + private @NotNull Queue createBreadcrumbsList(final int maxBreadcrumb) { return SynchronizedQueue.synchronizedQueue(new CircularFifoQueue<>(maxBreadcrumb)); } @@ -188,7 +198,7 @@ public Scope clone() throws CloneNotSupportedException { final Map extraRef = extra; - Map extraClone = new HashMap<>(); + Map extraClone = new ConcurrentHashMap<>(); for (Map.Entry item : extraRef.entrySet()) { if (item != null) { @@ -198,6 +208,8 @@ public Scope clone() throws CloneNotSupportedException { clone.extra = extraClone; + clone.contexts = new Contexts(contexts); + return clone; } diff --git a/sentry-core/src/main/java/io/sentry/core/protocol/Contexts.java b/sentry-core/src/main/java/io/sentry/core/protocol/Contexts.java index f495edbdb..8ae7f3f15 100644 --- a/sentry-core/src/main/java/io/sentry/core/protocol/Contexts.java +++ b/sentry-core/src/main/java/io/sentry/core/protocol/Contexts.java @@ -1,10 +1,15 @@ package io.sentry.core.protocol; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public final class Contexts extends ConcurrentHashMap { private static final long serialVersionUID = 252445813254943011L; + public Contexts(Map initialValues) { + super(initialValues); + } + private T toContextType(String key, Class clazz) { Object item = get(key); return clazz.isInstance(item) ? clazz.cast(item) : null; diff --git a/sentry-core/src/test/java/io/sentry/core/ScopeTest.kt b/sentry-core/src/test/java/io/sentry/core/ScopeTest.kt index 8058e19c9..4a9fda870 100644 --- a/sentry-core/src/test/java/io/sentry/core/ScopeTest.kt +++ b/sentry-core/src/test/java/io/sentry/core/ScopeTest.kt @@ -1,5 +1,6 @@ package io.sentry.core +import io.sentry.core.protocol.App import io.sentry.core.protocol.User import kotlin.test.Test import kotlin.test.assertEquals @@ -23,6 +24,7 @@ class ScopeTest { scope.user = user scope.transaction = "transaction" + scope.contexts.app = App() val fingerprints = mutableListOf("abc", "def") scope.fingerprint = fingerprints @@ -47,6 +49,8 @@ class ScopeTest { assertNotNull(clone) assertNotSame(scope, clone) assertNotSame(scope.user, clone.user) + assertNotSame(scope.contexts, clone.contexts) + assertNotSame(scope.contexts.app, clone.contexts.app) assertNotSame(scope.fingerprint, clone.fingerprint) assertNotSame(scope.breadcrumbs, clone.breadcrumbs) assertNotSame(scope.tags, clone.tags) @@ -66,6 +70,8 @@ class ScopeTest { scope.user = user scope.transaction = "transaction" + scope.contexts.app = App() + val fingerprints = mutableListOf("abc") scope.fingerprint = fingerprints