question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Applying immutable map in `SentryEvent#setExtras` causes `UnsupportedOperationException`

See original GitHub issue

Platform:

  • Android -> If yes, which Device API (and compileSdkVersion/targetSdkVersion/Build tools) version? Device API: 26 compileSdkVersion: 30 targetSdkVersion: 30 buildTools: applied by Android Gradle Plugin

IDE:

  • Android Studio -> If yes, which version? 4.2 beta 3

Build system:

  • Gradle -> If yes, which version? 6.7.1

Android Gradle Plugin:

  • Yes -> If yes, which version? 4.2.0-beta03

Sentry Android Gradle Plugin:

  • No

Proguard/R8:

  • Disabled

Platform installed with:

  • Maven Central

The version of the SDK: 4.3.0


I have the following issue:

When applying extra data by Sentry#setExtra and then applying extra data for a single event by SentryEvent#setExtras with immutable map (from Kotlin), I’m experiencing UnsupportedOperationException as SDK tries to edit the immutable map that is passed.

It crashes on the runtime as Java’s method signatures will accept immutable Kotlin’s map.

Steps to reproduce: I’ve prepared a reproduction repository.

To check it, please go to SentryProxyTest. Both of the use cases are tested there.

You can also check the results of those tests on CI. The test with mutable map passes while test with immutable map throws an exception

Actual result: When applying extra with MutableMap everything works fine, the exception is not thrown. When applying extra with Map (which is immutable), UnsupportedOperationException is thrown.

Expected result: Sentry SDK should accept both the mutable and immutable types. For immutable types, if editing is required, it should map a Map to mutable type.


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
marandanetocommented, Mar 29, 2021

@mateuszkwiecinski I don’t disagree, although some of the problems you’ve mentioned are already known and in the backlog.

eg for better null-safety using Kotlin, https://github.com/getsentry/sentry-java/pull/1137 adding @Nullable and @NotNullable annotations eg for adding proguard, https://github.com/getsentry/sentry-java/issues/1236

ideally, you’d not need to fiddle with these things, the SDK should do it automatically, in our protocol pretty much everything is nullable by default, we want to be as cheap as possible (memory footprint and JSON ser/deser, payload size over the wire etc).

thanks for the feedback.

3reactions
mateuszkwiecinskicommented, Mar 29, 2021

I’ll add my 2 cents: In general calling sentry-java classes from Kotlin seems to be a bit inconvenient and rather error prone. There are a lot of cases where it is not clear whether a field can be null, issues resulting in runtime crashes caused by immutability (as the one described in this ticket), or simply invonvenient constructor calls.

In my case I tried to write custom EventProcessor, and I had to struggle with multiple runtime crashes, before I reached a working solution. I wish I could write

sentryOptions.addEventProcessor { event, _ ->
    event.debugMeta.images += DebugImage(type = "proguard", uuid = "xxx")
    event
}

instead of guessing what should I do if event is null, how to create valid DebugMeta and what’s the difference between empty and null images list, always remembering I sometimes can’t use immutable objects. I always have to look into source code and unterstand the data flow and infer nullability and mutability.

I don’t know what’s the proper way to address all concenrs, I just wanted to share you may want to treat this issue as making sentry-java more Kotlin friendly in general, not focusing on immutability in mentione setExtras case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Fix the Unsupported Operation Exception in Java
UnsupportedOperationException is a Java runtime exception that occurs when an unsupported operation is requested but could not be performed.
Read more >
Map.of, Unmodifiable and UnsupportedOperationException
The convenience Map.of method creates an unmodifiable Map : ... A modification of an umodifiable Map causes: java.lang.
Read more >
UnsupportedOperation exception while using Map in Java
Caused by: java.lang.UnsupportedOperationException at java.util. ... operation on immutable map results in UnsupportedOperation exception.
Read more >
Why do I get an UnsupportedOperationException when trying ...
asList creates an unmodifiable list. From the Javadoc: Returns a fixed-size list backed by the specified array.
Read more >
Binding to immutable Map fails with ... - GitHub
1. java.lang.UnsupportedOperationException: null at java.util.Collections$UnmodifiableMap.put(Collections.java:1459). I' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found