Skip to content

Commit 90bfee5

Browse files
committed
Add attachment methods to Hints directly
1 parent 04edda0 commit 90bfee5

File tree

7 files changed

+85
-90
lines changed

7 files changed

+85
-90
lines changed

sentry/api/sentry.api

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,15 +1836,6 @@ public final class io/sentry/exception/SentryEnvelopeException : java/lang/Excep
18361836
public abstract interface class io/sentry/hints/ApplyScopeData {
18371837
}
18381838

1839-
public final class io/sentry/hints/Attachments {
1840-
public fun <init> ()V
1841-
public fun add (Lio/sentry/Attachment;)V
1842-
public fun addAll (Ljava/util/List;)V
1843-
public fun clear ()V
1844-
public fun getAll ()Ljava/util/List;
1845-
public fun replaceAll (Ljava/util/List;)V
1846-
}
1847-
18481839
public abstract interface class io/sentry/hints/Cached {
18491840
}
18501841

@@ -1858,10 +1849,14 @@ public abstract interface class io/sentry/hints/Flushable {
18581849

18591850
public final class io/sentry/hints/Hints {
18601851
public fun <init> ()V
1852+
public fun addAttachment (Lio/sentry/Attachment;)V
1853+
public fun addAttachments (Ljava/util/List;)V
1854+
public fun clearAttachments ()V
18611855
public fun get (Ljava/lang/String;)Ljava/lang/Object;
18621856
public fun getAs (Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object;
1863-
public fun getAttachments ()Lio/sentry/hints/Attachments;
1857+
public fun getAttachments ()Ljava/util/List;
18641858
public fun remove (Ljava/lang/String;)V
1859+
public fun replaceAttachments (Ljava/util/List;)V
18651860
public fun set (Ljava/lang/String;Ljava/lang/Object;)V
18661861
public static fun withAttachment (Lio/sentry/Attachment;)Lio/sentry/hints/Hints;
18671862
public static fun withAttachments (Ljava/util/List;)Lio/sentry/hints/Hints;

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private boolean shouldApplyScopeData(
196196

197197
private void addScopeAttachmentsToHints(@Nullable Scope scope, @NotNull Hints hints) {
198198
if (scope != null) {
199-
hints.getAttachments().addAll(scope.getAttachments());
199+
hints.addAttachments(scope.getAttachments());
200200
}
201201
}
202202

@@ -227,7 +227,7 @@ private boolean shouldSendSessionUpdateForDroppedEvent(
227227
}
228228

229229
private @Nullable List<Attachment> getAttachments(final @NotNull Hints hints) {
230-
@NotNull final List<Attachment> attachments = hints.getAttachments().getAll();
230+
@NotNull final List<Attachment> attachments = hints.getAttachments();
231231

232232
@Nullable final Attachment screenshot = hints.getAs(SENTRY_SCREENSHOT, Attachment.class);
233233
if (screenshot != null) {

sentry/src/main/java/io/sentry/hints/Attachments.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

sentry/src/main/java/io/sentry/hints/Hints.java

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.HashMap;
77
import java.util.List;
88
import java.util.Map;
9+
import java.util.concurrent.CopyOnWriteArrayList;
910
import org.jetbrains.annotations.NotNull;
1011
import org.jetbrains.annotations.Nullable;
1112

@@ -15,13 +16,13 @@ public final class Hints {
1516

1617
public static @NotNull Hints withAttachment(@Nullable Attachment attachment) {
1718
@NotNull final Hints hints = new Hints();
18-
hints.getAttachments().add(attachment);
19+
hints.addAttachment(attachment);
1920
return hints;
2021
}
2122

2223
public static @NotNull Hints withAttachments(@Nullable List<Attachment> attachments) {
2324
@NotNull final Hints hints = new Hints();
24-
hints.getAttachments().addAll(attachments);
25+
hints.addAttachments(attachments);
2526
return hints;
2627
}
2728

@@ -48,16 +49,54 @@ public void remove(@NotNull String hintName) {
4849
internalStorage.remove(hintName);
4950
}
5051

51-
public @NotNull Attachments getAttachments() {
52-
if (internalStorage.containsKey(SENTRY_ATTACHMENTS)) {
53-
Attachments container = getAs(SENTRY_ATTACHMENTS, Attachments.class);
54-
if (container != null) {
55-
return container;
56-
}
52+
@SuppressWarnings("unchecked")
53+
public @NotNull List<Attachment> getAttachments() {
54+
List<Attachment> attachments = getAs(SENTRY_ATTACHMENTS, List.class);
55+
if (attachments != null) {
56+
return new CopyOnWriteArrayList<>(attachments);
57+
}
58+
59+
return new CopyOnWriteArrayList<>();
60+
}
61+
62+
public void replaceAttachments(@Nullable List<Attachment> attachments) {
63+
clearAttachments();
64+
addAttachments(attachments);
65+
}
66+
67+
public void clearAttachments() {
68+
internalStorage.put(SENTRY_ATTACHMENTS, new CopyOnWriteArrayList<Attachment>());
69+
}
70+
71+
@SuppressWarnings("unchecked")
72+
public void addAttachment(@Nullable Attachment attachment) {
73+
if (attachment == null) {
74+
return;
5775
}
5876

59-
Attachments attachments = new Attachments();
77+
List<Attachment> existingAttachments = getAs(SENTRY_ATTACHMENTS, List.class);
78+
if (existingAttachments != null) {
79+
existingAttachments.add(attachment);
80+
return;
81+
}
82+
83+
List<Attachment> attachments = new CopyOnWriteArrayList<>();
84+
attachments.add(attachment);
6085
internalStorage.put(SENTRY_ATTACHMENTS, attachments);
61-
return attachments;
86+
}
87+
88+
@SuppressWarnings("unchecked")
89+
public void addAttachments(@Nullable List<Attachment> attachments) {
90+
if (attachments == null) {
91+
return;
92+
}
93+
94+
List<Attachment> existingAttachments = getAs(SENTRY_ATTACHMENTS, List.class);
95+
if (existingAttachments != null) {
96+
existingAttachments.addAll(attachments);
97+
return;
98+
}
99+
100+
internalStorage.put(SENTRY_ATTACHMENTS, new CopyOnWriteArrayList<>(attachments));
62101
}
63102
}

sentry/src/test/java/io/sentry/SentryClientTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,8 +1618,8 @@ class SentryClientTest {
16181618
fun `can add to attachments in beforeSend`() {
16191619
val sut = fixture.getSut { options ->
16201620
options.setBeforeSend { event, hints ->
1621-
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments.all)
1622-
hints.attachments.add(fixture.attachment3)
1621+
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments)
1622+
hints.addAttachment(fixture.attachment3)
16231623
event
16241624
}
16251625
}
@@ -1635,7 +1635,7 @@ class SentryClientTest {
16351635
fun `can replace attachments in beforeSend`() {
16361636
val sut = fixture.getSut { options ->
16371637
options.setBeforeSend { event, hints ->
1638-
hints.attachments.replaceAll(listOf(fixture.attachment))
1638+
hints.replaceAttachments(listOf(fixture.attachment))
16391639
event
16401640
}
16411641
}
@@ -1652,8 +1652,8 @@ class SentryClientTest {
16521652
val sut = fixture.getSut { options ->
16531653
options.addEventProcessor(object : EventProcessor {
16541654
override fun process(event: SentryEvent, hints: Hints): SentryEvent? {
1655-
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments.all)
1656-
hints.attachments.add(fixture.attachment3)
1655+
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments)
1656+
hints.addAttachment(fixture.attachment3)
16571657
return event
16581658
}
16591659

@@ -1678,7 +1678,7 @@ class SentryClientTest {
16781678
val sut = fixture.getSut { options ->
16791679
options.addEventProcessor(object : EventProcessor {
16801680
override fun process(event: SentryEvent, hints: Hints): SentryEvent? {
1681-
hints.attachments.replaceAll(listOf(fixture.attachment))
1681+
hints.replaceAttachments(listOf(fixture.attachment))
16821682
return event
16831683
}
16841684

@@ -1740,8 +1740,8 @@ class SentryClientTest {
17401740
transaction: SentryTransaction,
17411741
hints: Hints
17421742
): SentryTransaction? {
1743-
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments.all)
1744-
hints.attachments.add(fixture.attachment3)
1743+
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments)
1744+
hints.addAttachment(fixture.attachment3)
17451745
return transaction
17461746
}
17471747
})
@@ -1771,7 +1771,7 @@ class SentryClientTest {
17711771
transaction: SentryTransaction,
17721772
hints: Hints
17731773
): SentryTransaction? {
1774-
hints.attachments.replaceAll(listOf(fixture.attachment))
1774+
hints.replaceAttachments(listOf(fixture.attachment))
17751775
return transaction
17761776
}
17771777
})
@@ -1809,7 +1809,7 @@ class SentryClientTest {
18091809
fun `adding attachments in beforeBreadcrumb ignores them`() {
18101810
val sut = fixture.getSut { options ->
18111811
options.setBeforeBreadcrumb { breadcrumb, hints ->
1812-
hints.attachments.add(fixture.attachment)
1812+
hints.addAttachment(fixture.attachment)
18131813
breadcrumb
18141814
}
18151815
}

sentry/src/test/java/io/sentry/hints/AttachmentsTest.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,50 @@ class AttachmentsTest {
88

99
@Test
1010
fun `can add an attachment`() {
11-
val container = Attachments()
11+
val hints = Hints()
1212
val attachment = newAttachment("test1")
13-
container.add(attachment)
13+
hints.addAttachment(attachment)
1414

15-
assertEquals(listOf(attachment), container.all)
15+
assertEquals(listOf(attachment), hints.attachments)
1616
}
1717

1818
@Test
1919
fun `can add multiple attachments`() {
20-
val container = Attachments()
20+
val attachments = Hints()
2121
val attachment1 = newAttachment("test1")
2222
val attachment2 = newAttachment("test2")
23-
container.add(attachment1)
24-
container.add(attachment2)
23+
attachments.addAttachment(attachment1)
24+
attachments.addAttachment(attachment2)
2525

26-
assertEquals(listOf(attachment1, attachment2), container.all)
26+
assertEquals(listOf(attachment1, attachment2), attachments.attachments)
2727
}
2828

2929
@Test
3030
fun `after reset list is empty`() {
31-
val container = Attachments()
31+
val hints = Hints()
3232
val attachment1 = newAttachment("test1")
3333
val attachment2 = newAttachment("test2")
34-
container.add(attachment1)
35-
container.add(attachment2)
34+
hints.addAttachment(attachment1)
35+
hints.addAttachment(attachment2)
3636

37-
container.clear()
37+
hints.clearAttachments()
3838

39-
assertEquals(emptyList(), container.all)
39+
assertEquals(emptyList(), hints.attachments)
4040
}
4141

4242
@Test
4343
fun `after replace list contains only new item`() {
44-
val container = Attachments()
44+
val container = Hints()
4545
val attachment1 = newAttachment("test1")
4646
val attachment2 = newAttachment("test2")
4747
val attachment3 = newAttachment("test2")
4848
val attachment4 = newAttachment("test2")
49-
container.add(attachment1)
50-
container.add(attachment2)
49+
container.addAttachment(attachment1)
50+
container.addAttachment(attachment2)
5151

52-
container.replaceAll(listOf(attachment3, attachment4))
52+
container.replaceAttachments(listOf(attachment3, attachment4))
5353

54-
assertEquals(listOf(attachment3, attachment4), container.all)
54+
assertEquals(listOf(attachment3, attachment4), container.attachments)
5555
}
5656

5757
companion object {

sentry/src/test/java/io/sentry/hints/HintsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ class HintsTest {
8888
fun `can create hints with attachment`() {
8989
val attachment = newAttachment("test1")
9090
val hints = Hints.withAttachment(attachment)
91-
assertEquals(listOf(attachment), hints.attachments.all)
91+
assertEquals(listOf(attachment), hints.attachments)
9292
}
9393

9494
@Test
9595
fun `can create hints with attachments`() {
9696
val attachment1 = newAttachment("test1")
9797
val attachment2 = newAttachment("test1")
9898
val hints = Hints.withAttachments(listOf(attachment1, attachment2))
99-
assertEquals(listOf(attachment1, attachment2), hints.attachments.all)
99+
assertEquals(listOf(attachment1, attachment2), hints.attachments)
100100
}
101101
}

0 commit comments

Comments
 (0)