Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -1836,15 +1836,6 @@ public final class io/sentry/exception/SentryEnvelopeException : java/lang/Excep
public abstract interface class io/sentry/hints/ApplyScopeData {
}

public final class io/sentry/hints/Attachments {
public fun <init> ()V
public fun add (Lio/sentry/Attachment;)V
public fun addAll (Ljava/util/List;)V
public fun clear ()V
public fun getAll ()Ljava/util/List;
public fun replaceAll (Ljava/util/List;)V
}

public abstract interface class io/sentry/hints/Cached {
}

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

public final class io/sentry/hints/Hints {
public fun <init> ()V
public fun addAttachment (Lio/sentry/Attachment;)V
public fun addAttachments (Ljava/util/List;)V
public fun clearAttachments ()V
public fun get (Ljava/lang/String;)Ljava/lang/Object;
public fun getAs (Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Object;
public fun getAttachments ()Lio/sentry/hints/Attachments;
public fun getAttachments ()Ljava/util/List;
public fun remove (Ljava/lang/String;)V
public fun replaceAttachments (Ljava/util/List;)V
public fun set (Ljava/lang/String;Ljava/lang/Object;)V
public static fun withAttachment (Lio/sentry/Attachment;)Lio/sentry/hints/Hints;
public static fun withAttachments (Ljava/util/List;)Lio/sentry/hints/Hints;
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/main/java/io/sentry/SentryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private boolean shouldApplyScopeData(

private void addScopeAttachmentsToHints(@Nullable Scope scope, @NotNull Hints hints) {
if (scope != null) {
hints.getAttachments().addAll(scope.getAttachments());
hints.addAttachments(scope.getAttachments());
}
}

Expand Down Expand Up @@ -227,7 +227,7 @@ private boolean shouldSendSessionUpdateForDroppedEvent(
}

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

@Nullable final Attachment screenshot = hints.getAs(SENTRY_SCREENSHOT, Attachment.class);
if (screenshot != null) {
Expand Down
39 changes: 0 additions & 39 deletions sentry/src/main/java/io/sentry/hints/Attachments.java

This file was deleted.

59 changes: 49 additions & 10 deletions sentry/src/main/java/io/sentry/hints/Hints.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

public static @NotNull Hints withAttachment(@Nullable Attachment attachment) {
@NotNull final Hints hints = new Hints();
hints.getAttachments().add(attachment);
hints.addAttachment(attachment);
return hints;
}

public static @NotNull Hints withAttachments(@Nullable List<Attachment> attachments) {
@NotNull final Hints hints = new Hints();
hints.getAttachments().addAll(attachments);
hints.addAttachments(attachments);
return hints;
}

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

public @NotNull Attachments getAttachments() {
if (internalStorage.containsKey(SENTRY_ATTACHMENTS)) {
Attachments container = getAs(SENTRY_ATTACHMENTS, Attachments.class);
if (container != null) {
return container;
}
@SuppressWarnings("unchecked")
public @NotNull List<Attachment> getAttachments() {
List<Attachment> attachments = getAs(SENTRY_ATTACHMENTS, List.class);
if (attachments != null) {
return new CopyOnWriteArrayList<>(attachments);
}

return new CopyOnWriteArrayList<>();
}

public void replaceAttachments(@Nullable List<Attachment> attachments) {
clearAttachments();
addAttachments(attachments);
}

public void clearAttachments() {
internalStorage.put(SENTRY_ATTACHMENTS, new CopyOnWriteArrayList<Attachment>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be a list instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update

}

@SuppressWarnings("unchecked")
public void addAttachment(@Nullable Attachment attachment) {
if (attachment == null) {
return;
}

Attachments attachments = new Attachments();
List<Attachment> existingAttachments = getAs(SENTRY_ATTACHMENTS, List.class);
if (existingAttachments != null) {
existingAttachments.add(attachment);
return;
}

List<Attachment> attachments = new CopyOnWriteArrayList<>();
attachments.add(attachment);
internalStorage.put(SENTRY_ATTACHMENTS, attachments);
return attachments;
}

@SuppressWarnings("unchecked")
public void addAttachments(@Nullable List<Attachment> attachments) {
if (attachments == null) {
return;
}

List<Attachment> existingAttachments = getAs(SENTRY_ATTACHMENTS, List.class);
if (existingAttachments != null) {
existingAttachments.addAll(attachments);
return;
}

internalStorage.put(SENTRY_ATTACHMENTS, new CopyOnWriteArrayList<>(attachments));
}
}
20 changes: 10 additions & 10 deletions sentry/src/test/java/io/sentry/SentryClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1618,8 +1618,8 @@ class SentryClientTest {
fun `can add to attachments in beforeSend`() {
val sut = fixture.getSut { options ->
options.setBeforeSend { event, hints ->
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments.all)
hints.attachments.add(fixture.attachment3)
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments)
hints.addAttachment(fixture.attachment3)
event
}
}
Expand All @@ -1635,7 +1635,7 @@ class SentryClientTest {
fun `can replace attachments in beforeSend`() {
val sut = fixture.getSut { options ->
options.setBeforeSend { event, hints ->
hints.attachments.replaceAll(listOf(fixture.attachment))
hints.replaceAttachments(listOf(fixture.attachment))
event
}
}
Expand All @@ -1652,8 +1652,8 @@ class SentryClientTest {
val sut = fixture.getSut { options ->
options.addEventProcessor(object : EventProcessor {
override fun process(event: SentryEvent, hints: Hints): SentryEvent? {
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments.all)
hints.attachments.add(fixture.attachment3)
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments)
hints.addAttachment(fixture.attachment3)
return event
}

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

Expand Down Expand Up @@ -1740,8 +1740,8 @@ class SentryClientTest {
transaction: SentryTransaction,
hints: Hints
): SentryTransaction? {
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments.all)
hints.attachments.add(fixture.attachment3)
assertEquals(listOf(fixture.attachment, fixture.attachment2), hints.attachments)
hints.addAttachment(fixture.attachment3)
return transaction
}
})
Expand Down Expand Up @@ -1771,7 +1771,7 @@ class SentryClientTest {
transaction: SentryTransaction,
hints: Hints
): SentryTransaction? {
hints.attachments.replaceAll(listOf(fixture.attachment))
hints.replaceAttachments(listOf(fixture.attachment))
return transaction
}
})
Expand Down Expand Up @@ -1809,7 +1809,7 @@ class SentryClientTest {
fun `adding attachments in beforeBreadcrumb ignores them`() {
val sut = fixture.getSut { options ->
options.setBeforeBreadcrumb { breadcrumb, hints ->
hints.attachments.add(fixture.attachment)
hints.addAttachment(fixture.attachment)
breadcrumb
}
}
Expand Down
34 changes: 17 additions & 17 deletions sentry/src/test/java/io/sentry/hints/AttachmentsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,50 @@ class AttachmentsTest {

@Test
fun `can add an attachment`() {
val container = Attachments()
val hints = Hints()
val attachment = newAttachment("test1")
container.add(attachment)
hints.addAttachment(attachment)

assertEquals(listOf(attachment), container.all)
assertEquals(listOf(attachment), hints.attachments)
}

@Test
fun `can add multiple attachments`() {
val container = Attachments()
val attachments = Hints()
val attachment1 = newAttachment("test1")
val attachment2 = newAttachment("test2")
container.add(attachment1)
container.add(attachment2)
attachments.addAttachment(attachment1)
attachments.addAttachment(attachment2)

assertEquals(listOf(attachment1, attachment2), container.all)
assertEquals(listOf(attachment1, attachment2), attachments.attachments)
}

@Test
fun `after reset list is empty`() {
val container = Attachments()
val hints = Hints()
val attachment1 = newAttachment("test1")
val attachment2 = newAttachment("test2")
container.add(attachment1)
container.add(attachment2)
hints.addAttachment(attachment1)
hints.addAttachment(attachment2)

container.clear()
hints.clearAttachments()

assertEquals(emptyList(), container.all)
assertEquals(emptyList(), hints.attachments)
}

@Test
fun `after replace list contains only new item`() {
val container = Attachments()
val container = Hints()
val attachment1 = newAttachment("test1")
val attachment2 = newAttachment("test2")
val attachment3 = newAttachment("test2")
val attachment4 = newAttachment("test2")
container.add(attachment1)
container.add(attachment2)
container.addAttachment(attachment1)
container.addAttachment(attachment2)

container.replaceAll(listOf(attachment3, attachment4))
container.replaceAttachments(listOf(attachment3, attachment4))

assertEquals(listOf(attachment3, attachment4), container.all)
assertEquals(listOf(attachment3, attachment4), container.attachments)
}

companion object {
Expand Down
4 changes: 2 additions & 2 deletions sentry/src/test/java/io/sentry/hints/HintsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ class HintsTest {
fun `can create hints with attachment`() {
val attachment = newAttachment("test1")
val hints = Hints.withAttachment(attachment)
assertEquals(listOf(attachment), hints.attachments.all)
assertEquals(listOf(attachment), hints.attachments)
}

@Test
fun `can create hints with attachments`() {
val attachment1 = newAttachment("test1")
val attachment2 = newAttachment("test1")
val hints = Hints.withAttachments(listOf(attachment1, attachment2))
assertEquals(listOf(attachment1, attachment2), hints.attachments.all)
assertEquals(listOf(attachment1, attachment2), hints.attachments)
}
}