Skip to content

Commit 836d5e6

Browse files
runningcodeclaude
andcommitted
fix(android): Resolve NullAway findings in AndroidConnectionStatusProvider
NullAway treats AtomicReference.get() as nullable, so the snapshot reads added in #6537 fail once NullAway runs on sentry-android-core. The snapshot is never set to null, so read it through one accessor that suppresses the finding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e80ff51 commit 836d5e6

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

‎sentry-android-core/src/main/java/io/sentry/android/core/internal/util/AndroidConnectionStatusProvider.java‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private void ensureNetworkCallbackRegistered() {
324324
public void onAvailable(final @NotNull Network network) {
325325
@NotNull ConnectionSnapshot current;
326326
do {
327-
current = snapshot.get();
327+
current = currentSnapshot();
328328
} while (!snapshot.compareAndSet(current, current.withNetwork(network)));
329329

330330
// Only forward the call on first registration + when the connection is
@@ -348,7 +348,7 @@ public void onUnavailable() {
348348

349349
@Override
350350
public void onLost(final @NotNull Network network) {
351-
if (!network.equals(snapshot.get().network)) {
351+
if (!network.equals(currentSnapshot().network)) {
352352
return;
353353
}
354354
clearCacheAndNotifyObservers();
@@ -376,7 +376,7 @@ private void clearCacheAndNotifyObservers() {
376376
@Override
377377
public void onCapabilitiesChanged(
378378
@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
379-
if (!network.equals(snapshot.get().network)) {
379+
if (!network.equals(currentSnapshot().network)) {
380380
return;
381381
}
382382
updateCacheAndNotifyObservers(networkCapabilities);
@@ -397,7 +397,7 @@ private void updateCacheAndNotifyObservers(
397397
@NotNull ConnectionSnapshot current;
398398
@NotNull ConnectionSnapshot updated;
399399
do {
400-
current = snapshot.get();
400+
current = currentSnapshot();
401401
if (!isSignificantChange(current.capabilities, networkCapabilities)) {
402402
return;
403403
}
@@ -482,9 +482,15 @@ private boolean hasSignificantTransportChanges(
482482
}
483483
}
484484

485+
// NullAway treats AtomicReference.get() as nullable, but the snapshot is never set to null.
486+
@SuppressWarnings("NullAway")
487+
private @NotNull ConnectionSnapshot currentSnapshot() {
488+
return snapshot.get();
489+
}
490+
485491
/** The current snapshot, refreshed first if it's stale. */
486492
private @NotNull ConnectionSnapshot freshSnapshot() {
487-
final @NotNull ConnectionSnapshot current = snapshot.get();
493+
final @NotNull ConnectionSnapshot current = currentSnapshot();
488494
return current.isFresh() ? current : refresh(current);
489495
}
490496

@@ -510,7 +516,7 @@ private boolean hasSignificantTransportChanges(
510516
if (snapshot.compareAndSet(current, refreshed)) {
511517
break;
512518
}
513-
current = snapshot.get();
519+
current = currentSnapshot();
514520
if (current.isFresh()) {
515521
return current;
516522
}
@@ -651,7 +657,7 @@ public void onForeground() {
651657

652658
// proactively update cache and notify observers on foreground to ensure connectivity
653659
// state is not stale
654-
final @NotNull ConnectionStatus status = statusOf(refresh(snapshot.get()));
660+
final @NotNull ConnectionStatus status = statusOf(refresh(currentSnapshot()));
655661
if (status == ConnectionStatus.DISCONNECTED) {
656662
// onLost is not called retroactively when we registerNetworkCallback (as opposed to
657663
// onAvailable), so we have to do it manually for the DISCONNECTED case
@@ -699,7 +705,7 @@ public void onBackground() {
699705
@TestOnly
700706
@Nullable
701707
public NetworkCapabilities getCachedNetworkCapabilities() {
702-
return snapshot.get().capabilities;
708+
return currentSnapshot().capabilities;
703709
}
704710

705711
/**

0 commit comments

Comments
 (0)