Skip to content

Commit 112c46d

Browse files
Add nullability annotations to integrations modules (getsentry#1462)
1 parent b45944c commit 112c46d

29 files changed

Lines changed: 138 additions & 40 deletions

sentry-android-core/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import net.ltgt.gradle.errorprone.errorprone
12
import org.jetbrains.kotlin.config.KotlinCompilerVersion
23

34
plugins {
@@ -65,6 +66,13 @@ tasks.withType<Test> {
6566
}
6667
}
6768

69+
tasks.withType<JavaCompile>().configureEach {
70+
options.errorprone {
71+
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
72+
option("NullAway:AnnotatedPackages", "io.sentry")
73+
}
74+
}
75+
6876
dependencies {
6977
api(project(":sentry"))
7078

@@ -76,6 +84,7 @@ dependencies {
7684
errorprone(Config.CompileOnly.nopenChecker)
7785
errorprone(Config.CompileOnly.errorprone)
7886
errorproneJavac(Config.CompileOnly.errorProneJavac8)
87+
errorprone(Config.CompileOnly.errorProneNullAway)
7988
compileOnly(Config.CompileOnly.jetbrainsAnnotations)
8089

8190
// tests

sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public final class ActivityLifecycleIntegration
2727
implements Integration, Closeable, Application.ActivityLifecycleCallbacks {
2828

2929
private final @NotNull Application application;
30-
private @NotNull IHub hub;
31-
private @NotNull SentryAndroidOptions options;
30+
private @Nullable IHub hub;
31+
private @Nullable SentryAndroidOptions options;
3232

3333
private boolean performanceEnabled = false;
3434

@@ -81,11 +81,13 @@ private boolean isPerformanceEnabled(final @NotNull SentryAndroidOptions options
8181
public void close() throws IOException {
8282
application.unregisterActivityLifecycleCallbacks(this);
8383

84-
options.getLogger().log(SentryLevel.DEBUG, "ActivityLifecycleIntegration removed.");
84+
if (options != null) {
85+
options.getLogger().log(SentryLevel.DEBUG, "ActivityLifecycleIntegration removed.");
86+
}
8587
}
8688

8789
private void addBreadcrumb(final @NonNull Activity activity, final @NotNull String state) {
88-
if (options.isEnableActivityLifecycleBreadcrumbs()) {
90+
if (options != null && hub != null && options.isEnableActivityLifecycleBreadcrumbs()) {
8991
final Breadcrumb breadcrumb = new Breadcrumb();
9092
breadcrumb.setType("navigation");
9193
breadcrumb.setData("state", state);
@@ -109,7 +111,7 @@ private void stopPreviousTransactions() {
109111
}
110112

111113
private void startTracing(final @NonNull Activity activity) {
112-
if (performanceEnabled && !isRunningTransaction(activity)) {
114+
if (performanceEnabled && !isRunningTransaction(activity) && hub != null) {
113115
// as we allow a single transaction running on the bound Scope, we finish the previous ones
114116
stopPreviousTransactions();
115117

@@ -135,7 +137,7 @@ void applyScope(final @NotNull Scope scope, final @NotNull ITransaction transact
135137
// manually.
136138
if (scopeTransaction == null) {
137139
scope.setTransaction(transaction);
138-
} else {
140+
} else if (options != null) {
139141
options
140142
.getLogger()
141143
.log(
@@ -202,15 +204,15 @@ public synchronized void onActivityResumed(final @NonNull Activity activity) {
202204
addBreadcrumb(activity, "resumed");
203205

204206
// fallback call for API < 29 compatibility, otherwise it happens on onActivityPostResumed
205-
if (!isAllActivityCallbacksAvailable) {
207+
if (!isAllActivityCallbacksAvailable && options != null) {
206208
stopTracing(activity, options.isEnableActivityLifecycleTracingAutoFinish());
207209
}
208210
}
209211

210212
@Override
211213
public synchronized void onActivityPostResumed(final @NonNull Activity activity) {
212214
// only executed if API >= 29 otherwise it happens on onActivityResumed
213-
if (isAllActivityCallbacksAvailable) {
215+
if (isAllActivityCallbacksAvailable && options != null) {
214216
// this should be called only when onResume has been executed already, which means
215217
// the UI is responsive at this moment.
216218
stopTracing(activity, options.isEnableActivityLifecycleTracingAutoFinish());

sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
7979
}
8080

8181
private void addObserver(final @NotNull IHub hub) {
82+
// this should never happen, check added to avoid warnings from NullAway
83+
if (this.options == null) {
84+
return;
85+
}
8286
watcher =
8387
new LifecycleWatcher(
8488
hub,

sentry-android-core/src/main/java/io/sentry/android/core/IBuildInfoProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.sentry.android.core;
22

3+
import org.jetbrains.annotations.Nullable;
4+
35
/** To make SDK info classes testable */
46
public interface IBuildInfoProvider {
57

@@ -15,5 +17,6 @@ public interface IBuildInfoProvider {
1517
*
1618
* @return the Build tags
1719
*/
20+
@Nullable
1821
String getBuildTags();
1922
}

sentry-android-core/src/main/java/io/sentry/android/core/IDebugImagesLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import io.sentry.protocol.DebugImage;
44
import java.util.List;
55
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.Nullable;
67

78
/** Used for loading the list of debug images from sentry-native. */
89
@ApiStatus.Internal
910
public interface IDebugImagesLoader {
11+
@Nullable
1012
List<DebugImage> loadDebugImages();
1113

1214
void clearDebugImages();

sentry-android-core/src/main/java/io/sentry/android/core/ILoadClass.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.sentry.android.core;
22

3+
import org.jetbrains.annotations.NotNull;
4+
35
/** An Adapter for making Class.forName testable */
46
interface ILoadClass {
57

@@ -10,5 +12,6 @@ interface ILoadClass {
1012
* @return a Class<?>
1113
* @throws ClassNotFoundException if class is not found
1214
*/
15+
@NotNull
1316
Class<?> loadClass(String clazz) throws ClassNotFoundException;
1417
}

sentry-android-core/src/main/java/io/sentry/android/core/Installation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
import java.nio.charset.Charset;
1010
import java.util.UUID;
1111
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
1213
import org.jetbrains.annotations.TestOnly;
1314

1415
final class Installation {
15-
@TestOnly static String deviceId = null;
16+
@TestOnly static @Nullable String deviceId = null;
1617

1718
@TestOnly static final String INSTALLATION = "INSTALLATION";
1819

sentry-android-core/src/main/java/io/sentry/android/core/ManifestMetadataReader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ static void applyMetadata(
8080
logger,
8181
DEBUG_LEVEL,
8282
options.getDiagnosticLevel().name().toLowerCase(Locale.ROOT));
83-
options.setDiagnosticLevel(SentryLevel.valueOf(level.toUpperCase(Locale.ROOT)));
83+
if (level != null) {
84+
options.setDiagnosticLevel(SentryLevel.valueOf(level.toUpperCase(Locale.ROOT)));
85+
}
8486
}
8587

8688
options.setAnrEnabled(readBool(metadata, logger, ANR_ENABLE, options.isAnrEnabled()));

sentry-android-core/src/main/java/io/sentry/android/core/NdkIntegration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public final class NdkIntegration implements Integration, Closeable {
1919

2020
private final @Nullable Class<?> sentryNdkClass;
2121

22-
private @NotNull SentryAndroidOptions options;
22+
private @Nullable SentryAndroidOptions options;
2323

2424
public NdkIntegration(final @Nullable Class<?> sentryNdkClass) {
2525
this.sentryNdkClass = sentryNdkClass;

sentry-android-core/src/main/java/io/sentry/android/core/SentryInitProvider.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import io.sentry.Sentry;
1010
import io.sentry.SentryLevel;
1111
import org.jetbrains.annotations.ApiStatus;
12+
import org.jetbrains.annotations.NotNull;
13+
import org.jetbrains.annotations.Nullable;
1214

1315
@ApiStatus.Internal
1416
public final class SentryInitProvider extends ContentProvider {
@@ -33,7 +35,7 @@ public void shutdown() {
3335
}
3436

3537
@Override
36-
public void attachInfo(Context context, ProviderInfo info) {
38+
public void attachInfo(@NotNull Context context, @NotNull ProviderInfo info) {
3739
// applicationId is expected to be prepended. See AndroidManifest.xml
3840
if (SentryInitProvider.class.getName().equals(info.authority)) {
3941
throw new IllegalStateException(
@@ -43,27 +45,36 @@ public void attachInfo(Context context, ProviderInfo info) {
4345
}
4446

4547
@Override
46-
public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {
48+
public @Nullable Cursor query(
49+
@NotNull Uri uri,
50+
@Nullable String[] strings,
51+
@Nullable String s,
52+
@Nullable String[] strings1,
53+
@Nullable String s1) {
4754
return null;
4855
}
4956

5057
@Override
51-
public String getType(Uri uri) {
58+
public @Nullable String getType(@NotNull Uri uri) {
5259
return null;
5360
}
5461

5562
@Override
56-
public Uri insert(Uri uri, ContentValues contentValues) {
63+
public @Nullable Uri insert(@NotNull Uri uri, @Nullable ContentValues contentValues) {
5764
return null;
5865
}
5966

6067
@Override
61-
public int delete(Uri uri, String s, String[] strings) {
68+
public int delete(@NotNull Uri uri, @Nullable String s, @Nullable String[] strings) {
6269
return 0;
6370
}
6471

6572
@Override
66-
public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
73+
public int update(
74+
@NotNull Uri uri,
75+
@Nullable ContentValues contentValues,
76+
@Nullable String s,
77+
@Nullable String[] strings) {
6778
return 0;
6879
}
6980
}

0 commit comments

Comments
 (0)